如果URL更改,则将页面重定向到后面

时间:2014-07-25 12:01:57

标签: javascript jquery html5 url

我的主页网址 // localhost:8090 它将 main.js

运行 index.html

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

我试过了:

main.js

if(window.location.href.indexOf("icons") > -1) 
 {
     window.history.back();
 }

它不起作用。该页面只显示图像。

2 个答案:

答案 0 :(得分:4)

在现代浏览器(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();

您是否参与哈希更改事件。

答案 1 :(得分:0)

请尝试以下操作:

RewriteEngine on 
RewriteCond %{HTTP_REFERER} !^http://(www\.)?localhost [NC] 
RewriteCond %{HTTP_REFERER} !^http://(www\.)?localhost.*$ [NC] 
RewriteRule \.(gif|jpg)$ - [F]

如果直接访问图像,则返回403,但允许它们在现场显示。

注意:当您打开带有图像的页面并将该图像的路径复制到地址栏中时,您可以看到该图像,这可能只是因为浏览器的缓存,实际上该图像尚未从服务器。

Credits to Ruslan Osipov