Paulo Dos Santos Paulo Dos Santos - 1 month ago 19
Javascript Question

JavaScript - splitting window.location.href returns undefined

I have the following code JavaScript:

var url = window.location.href;

var link = url.split('?link=');

link[1] = "http://goo.gl/" + link[1];
link[2] = "http://goo.gl/" + link[2];

function ad(){
window.location.href = link[1];
}

function ac(){
window.open(link[2], '_blank');
}


And there is a link:

<a href="javascript:ac();" onclick="ad();">ACCESS</a>


The problem is that in some computers, the split is not working.

For exemple: If the link is
mySite.com/link.html?link=wfijOp?link=atGdj
.
It should give me
goo.gl/wfijOp
and
goo.gl/atGdj
instead of
goo.gl/undefined
and
goo.gl/undefined
.

What is the problem with those computers?

Answer

Thanks, @arcyqwerty! I did what you suggested.

Usually ? is used for separating the query string from the path (see comment above). Try using another separator like link=abcd,efgh,ijkl. You can use this to get the query string variable. – @arcyqwerty

Go to the answer