Using location.href in a referenced js file

I am trying to move a block of code from the header to a linked JS file.
The code includes a location.href that works perfectly when on the page but simply makes the page blink when on a linked JS file.
Is there something more i have to do to make it work or does it only work on page?

function linkToEcommerceWithZip()
   {
       var href = 'https://.......'
  		var sourceCd='';
  		var cshift='';
  		
  		var zipCode=document.getElementById('zip').value;
  		
  		if(zipCode!='')
  		{
  		    if (navigator.userAgent.indexOf("MSIE") > -1)
  		    {
  		        sourceCd = document.getElementById("sourceCodeValue").innerText;
  		        if(document.getElementById("cshiftValue"))
  		        {
  		            cshift = document.getElementById("cshiftValue").innerText;
  		        }
             }
             else
             {
                sourceCd = document.getElementById("sourceCodeValue").innerHTML;
  		        if(document.getElementById("cshiftValue"))
  		        {
  		            cshift = document.getElementById("cshiftValue").innerHTML;
  		         }
              }
  		//alert(cshift);
  		
  		  if(cshift!='')
  		  {
  		   
  		        window.location.href = href + "?SourceCd=" + sourceCd + "&cshift_ck=" + cshift;
  		  }
  		  else
  		  {
  		        
  		        window.location.href = href + "?SourceCd=" + sourceCd ;
  		  }
  	}

Open in new window

jvancojrAsked:
Who is Participating?
 
jvancojrConnect With a Mentor Author Commented:
This was the code that ended up working
function linkToEcommerce()
  {
  		var href = 'https://......../'
  		var sourceCd='';
  		//var dnis='';
  		var cshift='';
  		
  		if (navigator.userAgent.indexOf("MSIE") > -1){
  		    sourceCd = document.getElementById("sourceCodeValue").innerText;
  		    //phone = document.getElementById("phoneValue").innerText;
  		    if(document.getElementById("cshiftValue"))
  		    {
  		        cshift = document.getElementById("cshiftValue").innerText;
  		    }
        }
        else{
            sourceCd = document.getElementById("sourceCodeValue").innerHTML;
  		    //dnis = document.getElementById("phoneValue").innerHTML;
  		    if(document.getElementById("cshiftValue"))
  		    {
  		        cshift = document.getElementById("cshiftValue").innerHTML;
  		     }
        }
  		//alert(cshift);
  		
  		if(cshift!='')
  		{
  		    
  		    location.href = href + "?SourceCd=" + sourceCd + "&cshift_ck=" + cshift;
  		}
  		else
  		{
  		    location.href = href + "?SourceCd=" + sourceCd ;
  		}
  }

Open in new window

0
 
RobinSoftware EngineerCommented:
How do you call the function linkToEcommerceWithZip ??
0
 
jvancojrAuthor Commented:
An onClick event on the page. The 'zip' the code references is a textbox where the user would have entered their zip code.
0
 
RobinSoftware EngineerCommented:
Can you provide the html? I have it working here with changing the location in a referenced js file.
0
All Courses

From novice to tech pro — start learning today.