Javascript code required for refering a JS file depends on the environment Prod, Dev, stage

262 Views Asked by At

I want to check Prod, dev and QA URL based on the URL i need to refer a .js file

Production URL: domain.com and www.domain.com

i should refer: src=prod.js file if the URL is production url

Dev and QA URLs: dev.domain.com and staging.com

i should refer: src= staging.js file if the URL is dev or QA or both,

I wish to use this window.location.href.search using if else condition.

Please help me with the if else condition using javascript.


I tried the with the below code but it is not working

if (window.location.href.search("domaing.com")!=-1 && window.location.href.search("staging.vicodin.com"|"dev.domain.com)==-1) { 
        src=prod.js
} 
else{
src=staging.js
}

Please help me to have the correct code

1

There are 1 best solutions below

0
On

You have several problems with your condition:

  1. You haven't closed a string (missing " after dev.domain.com)
  2. You use the bitwise or operator instead of the logical or (| instead of ||)
  3. You expect f(a||b) to do the same as f(a) || f(b)
  4. You haven't set the values of src as strings (prod.js instead of "prod.js")

Here is a correct solution:

if (window.location.href.search("domaing.com") !== -1 && 
    (window.location.href.search("staging.vicodin.com") === -1 ||
    window.location.href.search("dev.domain.com") === -1)) { 
    src = "prod.js";
} else{
    src = "staging.js";
}