Javascript how to narrow down suitable URL in the if statment

I am trying to develop a Javascript code which will suit my website. Here is my code:

jQuery('.hestia-title').click(function() {
  var link;
  link = location.href;
  if (link = "http://www.puslapioguru.eu" || "https://www.puslapioguru.eu" || "www.puslapioguru.eu") {
    var element_to_scroll_to = jQuery('.second-title')[0];
    console.log("Viskas veikia");
    element_to_scroll_to.scrollIntoView();
  } else {
    window.location.href = "http://www.puslapioguru.eu/";
  }
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

This code should determine on what open page it is run, but I encounter a problem with the if statement:

if (link="http://www.puslapioguru.eu" || "https://www.puslapioguru.eu" || "www.puslapioguru.eu")

I want that the if statement would run only if the specific URL would be opened, but now it even runs if the opened page URL is "http://www.puslapioguru.eu/temu-portfolio/". Can someone please help me with this problem?

2 answers

  • answered 2018-01-14 11:32 Paul S.

    Either use a Regular Expression

    /^https?:\/\/(?:www\.)?puslapioguru\.eu$/i.test(link); // Exactly on this
    // OR
    /^https?:\/\/(?:www\.)?puslapioguru\.eu(?:$|\/)/i.test(link); // Any path on this
    

    Or use Array methods to test for your options

    const permittedSiteList = [
        'http://www.puslapioguru.eu',
        'https://www.puslapioguru.eu',
        'www.puslapioguru.eu'
    ];
    permittedSiteList.includes(link); // eactly one of these
    // OR
    permittedSiteList.some(
        domain => (link + '/').toLowerCase().startsWith(domain + '/')
    ); // Any path
    

    If you choose one of these, I also recommend abstracting the RegExp or the Array outside the condition so it is more readable; i.e. the if's condition looks like

    if (permittedSiteRegExp.test(link)) {
        // ...
    }
    // OR
    if (permittedSiteList.includes(link)) {
        // ...
    }
    // OR in the Array + any path case, also the test function
    const isThisDomain = domain => (link + '/').toLowerCase().startsWith(domain + '/');
    if (permittedSiteList.some(isThisDomain)) {
        // ...
    }
    

    Currently, you're performing an assignment in your if statement so you'll get unexpected behaviour

  • answered 2018-01-14 11:32 freedomn-m

    Each part of || (or &&) has to equate to true/false by itself, ie:

     if ((true|false) || (true||false))
    

    so, rather than just use a string, you need to provide something to compare with, in each of the parts around the ||

    Secondly, in javascript, if you are comparing a value you need to use == or ===, not =. This gives,

    link = location.href;
    if (link == "http://www.puslapioguru.eu" 
          || link == "https://www.puslapioguru.eu" 
          || link == "www.puslapioguru.eu") {
    

    You can make this more flexible, but these are the essential issues with your if.


    In this case you might want just location.host instead of location.href as .href is the entire address including any page path or parameters while .host already removes the https:// etc parts for you:

    link = location.host;
    if (link == "www.puslapioguru.eu") {