Jump to content



Photo

JavaScript Array Match


  • Please log in to reply
6 replies to this topic

#1 Mr.XXIV

Mr.XXIV

    Shine bright like Iron Man.

  • Tech Issues Solved: 1
  • Joined: 30-April 11
  • Location: Durham, North Carolina
  • OS: OS X Yosemite
  • Phone: iPhone 5s

Posted 07 October 2012 - 10:49

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);
});




#2 +Seahorsepip

Seahorsepip

    http://seapip.com

  • Tech Issues Solved: 26
  • Joined: 23-January 11
  • Location: Netherlands
  • OS: Windows 8.1 Pro
  • Phone: Nexus 5

Posted 07 October 2012 - 15:10

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*/


#3 JoeC

JoeC

    Ooh, shiny stars

  • Joined: 30-November 05
  • Location: England

Posted 07 October 2012 - 16:03

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;
	}
  }
});


#4 OP Mr.XXIV

Mr.XXIV

    Shine bright like Iron Man.

  • Tech Issues Solved: 1
  • Joined: 30-April 11
  • Location: Durham, North Carolina
  • OS: OS X Yosemite
  • Phone: iPhone 5s

Posted 07 October 2012 - 20:48

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;
}
  }
});


#5 JoeC

JoeC

    Ooh, shiny stars

  • Joined: 30-November 05
  • Location: England

Posted 07 October 2012 - 23:59

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();
}


#6 OP Mr.XXIV

Mr.XXIV

    Shine bright like Iron Man.

  • Tech Issues Solved: 1
  • Joined: 30-April 11
  • Location: Durham, North Carolina
  • OS: OS X Yosemite
  • Phone: iPhone 5s

Posted 08 October 2012 - 01:11

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>


#7 OP Mr.XXIV

Mr.XXIV

    Shine bright like Iron Man.

  • Tech Issues Solved: 1
  • Joined: 30-April 11
  • Location: Durham, North Carolina
  • OS: OS X Yosemite
  • Phone: iPhone 5s

Posted 27 October 2012 - 22:20

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();
});