javascript window.location.href 不适用于 IE
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18278777/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
window.location.href not working on IE
提问by Vito
I have a problem with window.location.href.
我有 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:
它在 Firefox 和 Chrome 上完美运行,但是在 IE10 中,浏览器死机,我必须重新启动它。有时它重定向到所需的页面,但参数不通过。我一直在寻找解决方案,例如这个:
Window.Location Not Working In IE?
But the proposed solution do not work for me.
但是提议的解决方案对我不起作用。
Do somebody know how to deal with this?
有人知道如何处理吗?
采纳答案by André Dion
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
.
问题很可能是由于您的变量的值。如果它们包含特殊字符或无效字符,则encodeURIComponent
在分配给window.location.href
.
回答by user3012702
For some reason IE only like full url.
出于某种原因,IE 只喜欢完整的 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;
回答by Mahesh Padekar
Try window.location.replace(...)
instead.
试试吧window.location.replace(...)
。
Refer this question for information:
请参阅此问题以获取信息:
回答by losnir
Use encodeURIComponent()
to escape your url:
使用encodeURIComponent()
逃脱你的网址:
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.
在 Firefox 23.0、Chrome 28.0.1500.95 和 Internet Explorer 10 上运行良好。