• 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

    • My Mac will run it, not sure if I will bother for a while. Nothing really there that will make a difference for me.
    • It's literally a local account with the ability to do a password reset, anything beyond that is optional. Doesn't mean you can't login without a live internet connection, doesn't mean you have to use OneDrive, doesn't mean you have to use Bitlocker, doesn't mean the telemetry is more or less, doesn't mean you have to use O365 subscriptions. Sure you *can* do all those things, but they're optional. But like I say, it's entirely up to you Not picking on you specifically, but I've asked this before out of genuine interest into what everyone gets so up in arms about and it always comes back to OneDrive or Bitlocker which aren't directly related to Microsoft Accounts anyway, or Microsoft 'spying on me' and I wonder how much of the outrange is based on internet hearsay vs actual fact.
    • Does your Mac support the latest macOS? Here is the macOS 27 Golden Gate compatibility list by Aditya Tiwari Apple's official keynote for WWDC 2026 was a bit different this time. The iPhone-maker prioritized Siri AI and new Apple Intelligence features so much that the keynote didn't have dedicated sections for macOS 27 and other software updates. That said, macOS 27 Golden Gate is coming later this year. The freshly-baked update is now available in developer beta for the following Mac models: MacBook Pro (2020 and later) MacBook Air (2020 and later) MacBook Neo (2026) iMac (2021 and later) Mac mini (2020 and later) Mac Studio (2022 and later) Mac Pro (2023) It's interesting to note that macOS 27 Golden Gate has dropped support for Intel-based Macs altogether. All of the supported Mac models in the list are powered by different Apple Silicon chips. MacBook Neo, which is the most repairable MacBook in years, is the latest of them and draws its power from an iPhone chip. Apple supported some Intel-based Macs on last year's macOS 26, but noted that not all features were available on these devices. The list of devices not getting the latest update includes the Mac Pro (2019), 16-inch MacBook Pro (2019), 27-inch iMac (2020), and 13-inch MacBook Pro (2020). To get the developer beta on your supported Mac, go to Settings > General > Software Updates. Here, click on the "i" button next to Beta Updates, then select "macOS Golden Gate 27 Developer Beta" from the drop-down menu. Make sure that you back up your data before starting the update, just in case things go south. Also, it goes without saying that you know what you're doing because developer betas can be unstable and behave in unexpected ways. Apple will release the public beta sometime next month, which will be comparatively stable. If you have an appetite for unstable test builds, you can try out the new features Apple has been baking for macOS 27. For starters, an upgraded version of Siri, with a dedicated Siri AI app coming later this year, enables cross-platform AI chat within the Apple ecosystem. Apple's changes to the Liquid Glass design language have trickled down to macOS as well. Now, there is a slider to change the intensity from ultra-clear to fully tinted. The system animations are smoother, content loads faster, and the window corner radius is now the same across all apps. In addition to macOS 27, you can try the developer beta versions of iOS 27, iPadOS 27, watchOS 27, tvOS 27, and HomePod software 27 on your supported devices.
  • Recent Achievements

    • Week One Done
      rubentuben8 earned a badge
      Week One Done
    • Week One Done
      ARaclen earned a badge
      Week One Done
    • One Year In
      jojodbn earned a badge
      One Year In
    • One Month Later
      jojodbn earned a badge
      One Month Later
    • Week One Done
      jojodbn earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      525
    2. 2
      PsYcHoKiLLa
      232
    3. 3
      +Edouard
      132
    4. 4
      ATLien_0
      88
    5. 5
      Steven P.
      81
  • Tell a friend

    Love Neowin? Tell a friend!