简体   繁体   English

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

[英]Redirect the page to back if URL changed

My Homepage URL is //localhost:8090 it will run index.html with main.js 我的主页URL为// localhost:8090 ,它将与main.js运行index.html

If the URL is changed to //localhost:8090/icons/sample.png . 如果URL更改为//localhost:8090/icons/sample.png I have to redirect the user to previous page as //localhost:8090 . 我必须将用户重定向为// localhost:8090到上一页。

I tried: 我试过了:

main.js main.js

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

It doesn't work. 没用 The page simply display the image. 该页面仅显示图像。

In modern browsers(IE8+, FF3.6+, Chrome), you can just listen to the hashchange event on window. 在现代浏览器(IE8 +,FF3.6 +,Chrome)中,您只能在窗口上监听hashchange事件。

$(window).bind('hashchange', function() {
  window.history.back();
 /* things */
});

In some old browsers, you need a timer that continually checks location.hash. 在某些旧的浏览器中,您需要一个计时器来连续检查location.hash。 If you're using jQuery, there is a plugin that does exactly that. 如果您使用的是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();

Do you work on hash change event. 您是否处理哈希更改事件。

Try following: 请尝试以下操作:

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

Returns 403 if you access image directly, but allows them to be displayed on site. 如果直接访问图像,则返回403,但允许它们在现场显示。

NOTE: it is possible that when you open some page with image and than copy that image's path into the address bar you can see that image, it is only because of the browser's cache, in fact that image has not been loaded from the server. 注意:打开带有图像的页面并将其路径复制到地址栏中后,您可能会看到该图像,这可能仅是由于浏览器的缓存,实际上该图像尚未从服务器加载。

Credits to Ruslan Osipov 归功于Ruslan Osipov

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 返回或重定向到网址 - Go back or redirect to url 将页面重定向到另一页面10秒钟,然后再次重定向回原始URL - Redirect a Page to Another Page for 10 Seconds Then Redirect Back Again to Original URL 如果有人手动尝试访问angularjs中网站的不同网址,如何重定向回同一页面(他已经在的页面) - How to redirect back to same page (the page he is already in) if someone manually try to visit different url of the site in angularjs 仅当用户来自该页面时,如何在提交表单后将 CodeIgniter 中的 URL 重定向回上一页? - How to redirect URL in CodeIgniter after form submission back to the previous page only when the user comes from that page? URL重定向HTML页面 - URL Redirect HTML Page 返回单击时重定向到某个页面 - redirect to a certain page on back click JavaScript重定向回引用页面 - JavaScript redirect back to refer page javascript 重定向回父页面 - javascript redirect back to parent page 当用户从页面注销时……如果用户单击浏览器后退按钮,它将重定向到先前的URL并显示HTML页面几秒钟 - When user logout from page … if user click browser back button ,which will redirect to previous URL and display HTML page for few seconds onClick浏览器重定向到不同的URL - Reactjs - Redirect to different URL onClick browser back - Reactjs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM