【问题标题】:Redirect the page to back if URL changed如果 URL 更改,将页面重定向到后面
【发布时间】:2014-09-17 06:50:06
【问题描述】:

我的主页 URL 是 //localhost:8090 它将运行 index.htmlma​​in.js

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

我试过了:

ma​​in.js

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

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

【问题讨论】:

  • 图片无法运行 Javascript。这个脚本在哪里运行?
  • 请求命中图片资源所以没有js,放一个htaccess文件拒绝所有
  • 你想要达到什么目的?是否要阻止对图标目录的访问?
  • 您是否尝试过从试图导航到图像的页面中的onunload
  • @Emma 是的,我想阻止图标目录

标签: javascript jquery html url


【解决方案1】:

在现代浏览器(IE8+、FF3.6+、Chrome)中,你可以只听window上的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();

您是否在处理哈希更改事件。

【讨论】:

  • 只有当 url 具有“图标”时,我才需要重定向。因为上面的代码限制了所有的API调用。
【解决方案2】:

尝试以下操作:

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

【讨论】:

    猜你喜欢
    • 2018-07-24
    • 2017-02-24
    • 1970-01-01
    • 2014-06-10
    • 2016-04-06
    • 2023-03-13
    • 2019-03-09
    • 2021-05-03
    • 1970-01-01
    相关资源
    最近更新 更多