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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
jvancojrAuthor 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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.