Sign in to follow this  
Followers 0
Mr.XXIV

JavaScript Array Match

7 posts in this topic

I'm creating a toolbar for my soon coming redesigned site Illing Spree and I want to make sure only the sites listed in the array "ill_members" will only load the bar, that way there's such things as an authorization. But I'm not exactly sure how to make these if statements. Can anyone help? :)

Also: This is all being written inside a JavaScript file, hence making a bootstrap so the other sites wouldn't go through the trouble of installing or working with upgrades.




var jQueryScriptOutputted=false;function initJQuery(){if(typeof jQuery=="undefined"){if(!jQueryScriptOutputted){jQueryScriptOutputted=true;document.write("<scr"+"ipt type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js'></scr"+"ipt>")}}else $(function(){})}initJQuery();


var ill_members = [

	{name: "Illing Spree", link: "illingspree.com"},

	{name: "WeAreRap.com", link: "wearerap.com"},

{name: "Gossip On Time", link: "gossipontime.com"}

];


var ill_music_members = [

	{name: "WeAreRap.com", link: "wearerap.com"},

];


var dyno = "http://dyno.illingspree.com/toolbar/v0.1/";

var illBar = "<div id='illbar'><a id='illbar-logo' href='http://illingspree.com'></a></div>";


$(document).ready(function () {

$('head').append("<link rel='stylesheet' type='text/css' href='" + dyno + "tb.css'/>");

$('body').append(illBar);

});


Share this post


Link to post
Share on other sites

Following checks if certain text is part of the url, this won't make a difference between .com, .org, .net, etc.

JS:

if(document.location.href.search(illingspree) != -1)

     /*load bar*/

else if(document.location.href.search(wearerap) != -1)

     /*load bar*/

else if(document.location.href.search(gossipontime) != -1)

     /*load bar*/

else 

     /*do nonting*/

Share this post


Link to post
Share on other sites

Something along these lines should do it.

Ignore the wacky indenting, Neowin's text editor screws it up and I'm on a laptop so can't be bothered fixing it :p

function load_bar ()

{

  var dyno = "http://dyno.illingspree.com/toolbar/v0.1/",

	  illBar = "<div id='illbar'><a id='illbar-logo' href='http://illingspree.com'></a></div>";


  $('head').append("<link rel='stylesheet' type='text/css' href='" + dyno + "tb.css'/>");

  $('body').append(illBar);

}


$(document).ready(function ()

{

  var current_domain = location.href.substr( location.protocol.length + 2 ).split('/')[0].replace(/^www\./i, '').toLowerCase(),

	  i = 0,

	  ill_members_length = ill_members.length;


  for (i; i < ill_members_length; i++)

  {

	if (ill_members[i].link.toLowerCase() === current_domain)

	{

	  load_bar();

	  break;

	}

  }

});

1 person likes this

Share this post


Link to post
Share on other sites

Ahh JoeC, your sample was perfect to work with! :D It's a huge step for me.

Here's what I have down. :) I can now tell others that they can't use the bar if they're not listed on the network.



var ill_members = [

    {name: "Illing Spree", link: "illingspree.com"},

    {name: "WeAreRap.com", link: "wearerap.com"},

{name: "Gossip On Time", link: "gossipontime.com"}

];



  var dyno = "http://dyno.illingspree.com/toolbar/v0.1/",

          illBar = "<div id='illbar'><a id='illbar-logo' href='http://illingspree.com'></a></div>";


  $('head').append("<link rel='stylesheet' type='text/css' href='" + dyno + "tb.css'/>");

  $('body').append(illBar);



function load_bar ()

{

var dyno = "http://dyno.illingspree.com/toolbar/v0.1/";

var illBar = ("<div id='illbar'><a id='illbar-logo' href='http://illingspree.com'></a><div id='illbar-feed'></div></div>");

$(document).ready(function () {

$('head').append("<link rel='stylesheet' type='text/css' href='" + dyno + "tb.css'/>");

$('body').append(illBar);

$('#illbar-feed').rssfeed('http://illingspree.com/feed',{

header: true,

titletag: 'div',

date: false,

content: false,

limit: 5

}, function(e) {

$.zazar.rotate({selector: '#illbar-feed ul'});

});

});

}


$(document).ready(function () {

  var current_domain = location.href.substr( location.protocol.length + 2 ).split('/')[0].replace(/^www\./i, '').toLowerCase(),

          i = 0,

          ill_members_length = ill_members.length;


  for (i; i < ill_members_length; i++)

  {

        if (ill_members[i].link.toLowerCase() === current_domain)

        {

          load_bar();

          break;

        } else {

$('body').prepend("<p>Disclaimer: This site is not a part of the Illing Spree network. Please remove this script.</p>");

break;

}

  }

});

Share this post


Link to post
Share on other sites

One slight change - instead of making that else put your disclaimer in (because you'll find it fires 2 times on valid sites, and 3 times on invalid sites), you'll need to add a "valid domain" flag -

function invalid_site ()

{

  $('body').prepend("<p>Disclaimer: This site is not a part of the Illing Spree network. Please remove this script.</p>");

}


$(document).ready(function ()

{

  var current_domain = location.href.substr( location.protocol.length + 2 ).split('/')[0].replace(/^www\./i, '').toLowerCase(),

    i = 0,

    ill_members_length = ill_members.length,

    valid_site = false;


  for (i; i < ill_members_length; i++)

  {

    if (ill_members[i].link.toLowerCase() === current_domain)

    {

      valid_site = true;

      load_bar();

      break;

    }

  }


  ! valid_site && invalid_site();

}

Share this post


Link to post
Share on other sites

I noticed that, I thought using the break for the else statement would help. That works perfectly, broski, thanks!

There's just one more thing. Besides appending the CSS file in the head, I tried doing the same for these files (the ones that load the RSS feed), but instead, prepend the toolbar file since it's jQuery and a function can't be placed before a library. I even gave the <script> an ID but it never helped. I'm basically trying to load all the files in order.



<script src='http://www.zazar.net/developers/jquery/zrssfeed/jquery.zrssfeed.min.js' type='text/javascript'></script>

<script src="http://admin.zazar.net/__system/_js/zframework/jquery.zframework.js" type="text/javascript"></script>


<script id="ill-toolbar" type="text/javascript" src="http://dyno.illingspree.com/toolbar/v0.1/illbar.js"></script>

Share this post


Link to post
Share on other sites

EDIT: Nevermind, I got it! Sorry!

I don't think I'm doing it right as far as the domain matching, because all of them load the same bar instead of making Sexes Battle a chicks bar itself.

link




$(document).ready(function () {

var ill_members = [

{name: "Illing Spree", link: "illingspree.com"}

],


ill_chicks_members = [

{name: "Sexes Battle", link: "sexesbattle.com"}

],


ill_music_members = [

{name: "WeAreRap.com", link: "wearerap.com"}

],


ill_pop_culture_members = [

{name: "Gossip On Time", link: "gossipontime.com"}

],


ill_author_members = [

{name: "Mr.XXIV", link: "mrxxiv.com"}

];


	var current_domain = location.href.substr(location.protocol.length + 2).split('/')[0].replace(/^www\./i, '').toLowerCase(),

		i = 0,

		ill_members_length = ill_members.length,

		valid_site = false;


	for (i; i < ill_members_length; i++) {

		if (ill_members[i].link.toLowerCase() === current_domain) {

			valid_site = true;

			load_bar('');

			break;

		} else if (ill_music_members[i].link.toLowerCase() === current_domain) {

			valid_site = true;

			load_bar('-music');

			break;

		} else if (ill_pop_culture_members[i].link.toLowerCase() === current_domain) {

			valid_site = true;

			load_bar('-pop-culture');

			break;

		} else if (ill_author_members[i].link.toLowerCase() === current_domain) {

			valid_site = true;

			load_bar('-author');

			break;

		} else if (ill_chicks_members[i].link.toLowerCase() === current_domain) {

			valid_site = true;

			load_bar('-chicks');

			break;

		}

	}!valid_site && invalid_site();

});

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.