jQuery window.location.replace不断循环 [英] jQuery window.location.replace keeps looping
问题描述
如果由于我的html5布局在Internet Explorer或Firefox中使用了较旧的浏览器,则我将jQuery与window.location.replace()一起使用,以重定向到wordpres中的新页面.
I'm using window.location.replace() with jQuery to redirect to a new page in wordpres if you got a older browser in either Internet explorer or Firefox due to my html5 layout.
var browser = jQuery.browser;
var version = browser.version.slice(0.3);
if ( browser.msie && version != "10.0" && version != '9.0' ) {
window.location.replace("http://crosscom.dk/update-browser");
}
if ( browser.mozilla && version != "17.0" && version != '16.0' && version != '15.0' ) {
window.location.replace("http://crosscom.dk/update-browser/");
}
重定向有效,但由于它已加载到wordpres的标头中,因此每次都会循环播放,因此您正坐在:IE 8示例中.
The redirects works but since its loaded in the header in wordpres it keeps on looping everytime, you are sitting on: example IE 8.
所以我的问题在后面...
So my question is following...
如何重定向浏览器一次后,如何终止脚本或在代码周围设置不同的参数以阻止脚本循环?
How can i kill the script or setup different parameters around my code to stop the script from looping after the browser got redirected once?
推荐答案
之所以引起循环,是因为无论您在哪个页面(包括重定向目标)上都进行重定向.为了避免循环,请测试您是否已经在重定向位置.
The loop is caused because the redirection occurs no matter what page you are on, including the redirection target. To avoid a loop, test whether you are already in the redirect location.
有多种方法可以实现-最有效的方法可能是服务器端检查和重定向.但是,假设您的知识水平或部署要求意味着JavaScript替代品更好,那么您可以使用以下代码:
There are a number of ways this could be achieved - the most efficient probably being a server side check and redirection. However, on the assumption that your knowledge level or deployment requirements mean that a JavaScript alternative is better, you could use the following code:
var redirectLocation = "http://example.com/example.html"; // Redirect destination
// Page location and redirectLocation should not be the same
if (window.location.href !== redirectLocation) {
// Redirect logic
}
将此直接应用于您自己的代码示例将产生:
Applying this directly to your own code example produces:
var browser = jQuery.browser;
var version = browser.version.slice(0.3);
var redirectLocation = "http://crosscom.dk/update-browser/"; // Redirect destination
// Page location and redirectLocation should not be the same
if (window.location.href !== redirectLocation) {
// Redirect logic
if ( browser.msie && version != "10.0" && version != '9.0' ) {
window.location.replace(redirectLocation);
}
if ( browser.mozilla && version != "17.0" && version != '16.0' && version != '15.0' ) {
window.location.replace(redirectLocation);
}
}
这篇关于jQuery window.location.replace不断循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!