6

I have a problem with window.location.href.

I'm trying to redirect to a page with the following code:

window.location.href = "juego.html"+'?modoJuego='+modoJuego+"&etapa="+etapa+"&rango="+rango;

It works perfectly on Firefox and Chrome, however in IE10 the browser freezes and I have to restart it. Sometimes it redirect to the desired page, but the parameters do not pass through. I have been looking for a solution, for example this one:

Window.Location Not Working In IE?

But the proposed solution do not work for me.

Do somebody know how to deal with this?

Community
  • 1
  • 1
Vito
  • 718
  • 4
  • 16
  • 37
  • 1
    Just use `window.location`. – user1477388 Aug 16 '13 at 17:14
  • http://stackoverflow.com/questions/10201809/ie-incompatability-with-window-location-href ? – twinlakes Aug 16 '13 at 17:16
  • For one, don't mix `"` and `'`. For two, there is no need to add strings that are hardcoded(`juego.html"+'?modoJuego`). `window.location.href = "juego.html?modoJuego="+modoJuego+"&etapa="+etapa+"&rango="+rango;` – David Starkey Aug 16 '13 at 17:16
  • What does your URL expand to? If any of your variables contain special or invalid characters you may need to wrap the right-hand side of your assignment in [`encodeURIComponent`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent). – André Dion Aug 16 '13 at 17:27
  • windows.location produce the same result. The link of the second comment procduce the same result. I changed the string as suggested by David Starkey with the same result. Any other idea? – Vito Aug 16 '13 at 17:34
  • @André Dion: You were right, one of those parameters had some blank spaces. I removed them and now it work perfectly on IE10. Please post this as an answer so I can vote it up. – Vito Aug 16 '13 at 17:56

4 Answers4

5

The problem is likely due to the value of your variables. If they contain special or invalid characters, those needs to be passed through encodeURIComponent before being assigned to window.location.href.

André Dion
  • 21,269
  • 7
  • 56
  • 60
  • Thanks André, just a little observation: the encodeURIComponent didn't worked to me, however, you were right about the thing of the special characters, I had some spaces on a variable, I remove them and now its working fine. – Vito Aug 16 '13 at 18:51
4

For some reason IE only like full url.

I have te same problem and fix it adding the full url like this:

var baseURL = 'http://www.your_url.com/';

window.location.href = baseURL + "juego.html"+'?modoJuego='+modoJuego+"&etapa="+etapa+"&rango="+rango;
1

Use encodeURIComponent() to escape your url:

window.location.href = encodeURIComponent("juego.html?modoJuego=" + modoJuego + "&etapa=" + etapa + "&rango=" + rango);

Works fine on Firefox 23.0, Chrome 28.0.1500.95 and Internet Explorer 10.

losnir
  • 1,141
  • 9
  • 14
  • Thank you, but it didn't worked. The problem was some spaces on the parameters, I removed them and now work fine. – Vito Aug 16 '13 at 17:57
  • No doubt that was the problem, the solution is to use `encodeURIComponent` in order to escape those illegal characters. Removing them altogether is avoiding the problem. – losnir Aug 16 '13 at 19:19
  • It didn't worked. I tried and that did not solve the problem. What solved the problem was to remove the characters manually (blank spaces on the parameters). – Vito Aug 16 '13 at 20:29
  • When encoding more than just parameters, you should use [`encodeURI`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURI) instead so reserved characters such as `=` aren't also encoded. – André Dion Aug 17 '13 at 18:40
1

Try window.location.replace(...) instead.

Refer this question for information:

How to redirect to another webpage in JavaScript/jQuery?

Community
  • 1
  • 1