gpt4 book ai didi

javascript - 如果 URL 更改,则重定向页面返回

转载 作者:行者123 更新时间:2023-11-28 15:38:06 25 4
gpt4 key购买 nike

我的主页 URL 是 //localhost:8090 它将使用 ma​​in.js 运行 index.html

如果 URL 更改为 //localhost:8090/icons/sample.png。我必须将用户重定向到上一页 //localhost:8090

我尝试过:

ma​​in.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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com