• 0

JavaScript Array Match


Question

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

Link to comment
https://www.neowin.net/forum/topic/1110673-javascript-array-match/
Share on other sites

6 answers to this question

Recommended Posts

  • 0

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*/[/CODE]

  • 0

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

  • 0

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

  • 0

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

  • 0

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.


&lt;script src='http://www.zazar.net/developers/jquery/zrssfeed/jquery.zrssfeed.min.js' type='text/javascript'&gt;&lt;/script&gt;
&lt;script src="https://admin.zazar.net/__system/_js/zframework/jquery.zframework.js" type="text/javascript"&gt;&lt;/script&gt;

&lt;script id="ill-toolbar" type="text/javascript" src="https://dyno.illingspree.com/toolbar/v0.1/illbar.js"&gt;&lt;/script&gt;

  • 0

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 &lt; 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 &amp;&amp; invalid_site();
});
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • I do trust Apple probably more than these other companies with certain data, but I also do think (and it has been demonstrated that) Apple pulls a lot of shenanigans and always has for a long time.
    • Does your iPhone support the latest iOS version? Here's the iOS 27 compatibility list by Aditya Tiwari It's that time of year when we get to know about the latest operating system updates for Apple devices. For iPhone, Apple previewed the iOS 27 update at WWDC 2026, where the company finally introduced an upgraded version of Siri. Apple typically supports iPhone models for up to five years. But it has been making exceptions in recent years (read iPhone 11). If you're wondering whether your iPhone is compatible with the iOS 27 update, here is the official list of devices: iPhone 17 Pro Max, iPhone 17 Pro, iPhone 17, iPhone 17e, iPhone Air iPhone 16 Pro Max, iPhone 16 Pro, iPhone 16, iPhone 16 Plus, iPhone 16e iPhone 15 Pro Max, iPhone 15 Pro, iPhone 15 Plus, iPhone 15 iPhone 14 Pro Max, iPhone 14 Pro, iPhone 14 Plus, iPhone 14 iPhone 13 Pro Max, iPhone 13 Pro, iPhone 13, iPhone 13 mini iPhone 12 Pro Max, iPhone 12 Pro, iPhone 12, iPhone 12 mini iPhone 11 Pro Max, iPhone 11 Pro, iPhone 11 iPhone SE (2nd generation), iPhone SE (3rd generation) So, you can download the iOS 27 developer beta on up to 31 different iPhone models. There has been no change to the list of supported iPhones since iOS 26. However, it will expand to include more devices when the iPhone 18 series arrives later this year. To download the developer beta on your iPhone, go to Settings > General > Software Update > Beta Updates. Here, select "iOS 27 Developer Beta" from the list of choices to get the new update. In addition to iOS 27, you can try the developer beta versions of macOS 27, iPadOS 27, watchOS 27, tvOS 27, and HomePod software 27 on your supported devices. iOS 27 comes with improved Liquid Glass, which you can adjust using a new transparency slider. Apple said during the keynote that iPhone apps now launch up to 30% faster, new photos appear in the Photos app up to 70% faster, and AirDrop transfers work up to 80% faster. The new update promises to improve performance on older iPhones by introducing a new CPU Scheduler that supports devices all the way back to the iPhone 11. While iOS 27 is supported on older iPhones, it goes without saying that they'll lack several features due to hardware differences. For instance, iPhone 14/14 Plus and older models come with a notch instead of the Dynamic Island. Similarly, Apple Intelligence features are supported on iPhone 15 Pro/Pro Max and later models.
    • The Radeon RX 9070 XT is right up there with the GeForce RTX 5070 Ti
    • I don't know why someone said useless, but it does have that pesky kernel driver bundled, and it's in perennial turmoil. When it goes bad, it goes very bad, and it's impossible to predict when it will due to system differences. I know that they're in the middle of development for a major new version that will include a completely new driver, one that they expect will largely solve the problem, but that's a ways out and it's unproven at this point.
    • doesn't AdGuard let ads through that pay to be let through?
  • Recent Achievements

    • Experienced
      JayZJay went up a rank
      Experienced
    • Reacting Well
      Sir_Timbit earned a badge
      Reacting Well
    • Week One Done
      rubentuben8 earned a badge
      Week One Done
    • Week One Done
      ARaclen earned a badge
      Week One Done
    • Week One Done
      jojodbn earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      524
    2. 2
      PsYcHoKiLLa
      231
    3. 3
      +Edouard
      132
    4. 4
      ATLien_0
      88
    5. 5
      Steven P.
      83
  • Tell a friend

    Love Neowin? Tell a friend!