1

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?

arcyqwerty
  • 10,325
  • 4
  • 47
  • 84
Paulo Dos Santos
  • 511
  • 1
  • 4
  • 16
  • It is probably because you are trying to access arrays element that does not exist. Arrays are zero based, so if you want to capture second element in an array you should write link[1]. – coder Nov 23 '14 at 01:28
  • @black123 I think he has the desired behavior. `link[0]` would give the `http://..../link.html` part, no? – arcyqwerty Nov 23 '14 at 01:30
  • 2
    as a general note though, query string parameters should be separated with `&param=value` not `?`. `?` is only used to separate query string from the path – arcyqwerty Nov 23 '14 at 01:31
  • What kind kind of computers are having issues? Are you noticing that it's a problem in particular browsers or OS'es? – arcyqwerty Nov 23 '14 at 01:32
  • @arcyqwerty they are using windows 8.1, google chrome v. 37. I have no idea why they are having this issue. Everyone else are okay! – Paulo Dos Santos Nov 23 '14 at 01:40
  • @black123 link[0] give me the mySite.com/link.html part. – Paulo Dos Santos Nov 23 '14 at 01:43
  • @PauloDosSantos Dos Santos. What gives you `link[1]` and `link[2]`? – coder Nov 23 '14 at 01:45
  • Can you try a `console.log(url)` and `console.log(link)` and tell us what you get? It's _extremely_ unlikely that the `.split()` function itself is failing; it seems more likely that `window.location.href` isn't what you think it is. – nnnnnn Nov 23 '14 at 01:45
  • Can you see what the value of `window.location.href` is before the split? Maybe the second '?' is being url encoded by the browser or is being stripped off? – rdubya Nov 23 '14 at 01:45
  • The wierdest thing here is that the code does not work for few computer's people. It is bizar because the link goes to the page goog.gl/undefined. The code actually works partially. – Paulo Dos Santos Nov 23 '14 at 01:51
  • 1
    Wait you've use `javascript:` in an href and then an inline `onclick=` attribute, my inside coder just died a little. http://stackoverflow.com/a/6348597/227176 – Sukima Nov 23 '14 at 02:18
  • I'm unable to reproduce using Chrome 37 (I don't have win 8.1 but it should be running same JS interpreter). Perhaps the URL got messed up for those users? (or they manually modified it?) Do you know the exact url's that they are having issues with? – arcyqwerty Nov 23 '14 at 02:54
  • I found the problem. Some of the browsers were converting the ?link= into %3Flink%3D. Then, the split was not getting to find the ?link=. Is there any way to fix it? Thanks! – Paulo Dos Santos Nov 23 '14 at 02:55
  • 1
    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](http://stackoverflow.com/a/901144/1059070) to get the query string variable. – arcyqwerty Nov 23 '14 at 03:05
  • I got it! I am using ...link.html?separador1233545separadorFHEIBVE. And I get split by "separador". Thanks! – Paulo Dos Santos Nov 23 '14 at 03:21

1 Answers1

0

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

Community
  • 1
  • 1
Paulo Dos Santos
  • 511
  • 1
  • 4
  • 16