Javascript window.location.replace()未在Google Chrome中启动
我试图使用URL GET字符串中的不同参数重新加载网页(当条件合适时)。下面的代码适用于除Google Chrome之外的所有浏览器。在Chrome中,它在第一次出现时不起作用,但如果我重新加载页面,它就会一直起作用Javascript window.location.replace()未在Google Chrome中启动,javascript,jquery,html,google-chrome,Javascript,Jquery,Html,Google Chrome,我试图使用URL GET字符串中的不同参数重新加载网页(当条件合适时)。下面的代码适用于除Google Chrome之外的所有浏览器。在Chrome中,它在第一次出现时不起作用,但如果我重新加载页面,它就会一直起作用 // Grab the current URL and put it into a string for evaluation. var url = window.location.toString(); // See if the sort parameter is in t
// Grab the current URL and put it into a string for evaluation.
var url = window.location.toString();
// See if the sort parameter is in the URL. If so, then hide the page,
// change the sort value, and replace this page with the right one.
if (url.indexOf("sort=rc") == -1 && (localStorage.getItem("sortValue") != "Room" || localStorage.getItem("sortValue") == null)) {
Bootstrapper.MVT.injectCSS("body { display:none; }");
var newUrl= url.replace("sort=rt", "sort=rc");
window.location.replace(newUrl);
}
当我使用Chrome调试器逐步处理代码时,它会正确地进入if语句,隐藏标记,更改URL以具有新的排序值,并且似乎会运行window.location.replace调用,但不会重新加载页面。页面完成加载,但由于display none调用而为空。如果单击“重新加载”按钮,页面将以所需的排序值重新加载,所有内容都将显示,在清除缓存之前,我再也不会出现此问题
你知道为什么Chrome会调用window.location.replace但不会强制重新加载页面吗?你是否在等待执行前发生
window.onload
事件?window.location
是一个对象,而不是字符串。@WhiteHat-我试过你的建议,但是,在尝试调用window.location.replace()之前,是否等待onload
事件并不重要。它似乎仍然忽略了替换调用。我从未发现这个错误。我最终从另一个方向解决了这个问题,避免了使用window.location.replace()。又花了3天时间重写所有内容并彻底测试,但现在我有了一个可行的解决方案。