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

标签 javascript jquery html url

我的主页 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/

相关文章:

javascript - 浏览器如何处理 <template> 标签 VS <div> 标签

javascript - Socket.io/ARI 错误 : Emit Alert

javascript - 在 DIV 上隐藏/显示并显示选定 block 的脚本

javascript - JQuery 从动态生成的下拉列表中获取选定的值

javascript 参数得到错误的数字

javascript - 如何使用 React 显示图像上传预览?

javascript - 使用 JQuery 添加平板电脑检测

html - 为什么当容器的图片内容高度发生变化时,容器的高度不适应?

javascript - 我应该为 &lt;textarea&gt; 使用 .innerHTML 还是 .value?

html - 使用 CSS3 的简单旋转木马