Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript browser navbar event

I want to prevent users to navigate to URL´s that are not accessed through html element. Example:

Actually navigating on: myweb.com/news

And I want to navigate to myweb.com/news?article_id=10 by writing this in the browser navigation bar to avoid pressing any element (like <a>).

When the user writes myweb.com/news?article_id=10 in the browser url, at the moment he presses enter, the browser should not allow him to navigate to the url.

I have tried:

//This wont work since jquery does not support it
$(window.location.href).on('change', function() {
    //Here check if href contains '?'
    alert("Not allowed");
});

//Neither works, doesnt do anything
$(window).on('change', function() {
    alert("Not allowed");
});

References: there is something similar asked here On - window.location.hash - Change?, but im interested in the 'parameter' version of that question.

like image 303
Alpha2k Avatar asked Oct 20 '22 11:10

Alpha2k


1 Answers

There are some known solutions :

  • ) Each time a user click a link - you save the page value to a cookie. Later , at the server- you check that interval ( value-1 ... value+1).

  • ) You can also save to a hidden field and check that value in the server.

So let's say a user is on page 3. ( the server serve that page - so a cookie/hidden value with value 3 is exists)

now he tries to go to page 10 :

you - in the server side - reads the cookie + requested Page number. if the interval is bigger than 1 - then you deny that request.

like image 143
Royi Namir Avatar answered Oct 21 '22 23:10

Royi Namir