Hulk1991
Hulk1991

Reputation: 3163

Redirect the page to back if URL changed

My Homepage URL is //localhost:8090 it will run index.html with main.js

If the URL is changed to //localhost:8090/icons/sample.png. I have to redirect the user to previous page as //localhost:8090.

I tried:

main.js

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

It doesn't work. The page simply display the image.

Upvotes: 0

Views: 186

Answers (2)

lovre
lovre

Reputation: 113

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.

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

Upvotes: 0

rajeshpanwar
rajeshpanwar

Reputation: 1233

In modern browsers(IE8+, FF3.6+, Chrome), you can just listen to the hashchange event on window.

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

In some old browsers, you need a timer that continually checks location.hash. If you're using jQuery, there is a plugin that does exactly that.

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.

Upvotes: 4

Related Questions