我的主页 URL 是 //localhost:8090 它将使用 main.js 运行 index.html
如果 URL 更改为 //localhost:8090/icons/sample.png。我必须将用户重定向到上一页 //localhost:8090。
我尝试过:
main.js
if(window.location.href.indexOf("icons") > -1)
{
window.history.back();
}
这不起作用。该页面仅显示图像。
最佳答案
在现代浏览器(IE8+、FF3.6+、Chrome)中,您可以只在窗口上监听 hashchange 事件。
$(window).bind('hashchange', function() {
window.history.back();
/* things */
});
在某些旧浏览器中,您需要一个计时器来持续检查 location.hash。如果您使用 jQuery,则有一个插件可以完全执行此操作。
function hashHandler(){
this.oldHash = window.location.hash;
this.Check;
var that = this;
var detect = function(){
if(that.oldHash!=window.location.hash){
window.history.back();
}
};
this.Check = setInterval(function(){ detect() }, 100);
}
var hashDetection = new hashHandler();
您从事哈希更改事件吗?
关于javascript - 如果 URL 更改,则重定向页面返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24955333/