Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check to see if a forward history exists in Javascript? (Disabling back button)

Tags:

javascript

Got a window.history.go(1) to stop the user hitting the back button, but was wondering if I could check to see if a forward history exists before doing it, so I can display a popup warning the user not to press the back button.

I know you can get the history length, but is there a way to get the current position in the history list? Or some other way of doing this...

like image 577
NibblyPig Avatar asked Nov 11 '09 10:11

NibblyPig


People also ask

How do I check stack history?

history object allows you to access the history stack of the browser. To navigate to a URL in the history, you use the back() , forward() , and go() methods. The history. length returns the number of URLs in the history stack.

How do you forward History in JavaScript?

forward() method causes the browser to move forward one page in the session history. It has the same effect as calling history.go(1) . This method is asynchronous. Add a listener for the popstate event in order to determine when the navigation has completed.

What is the syntax and use of history back() method?

The History back() method in HTML is used to load the previous URL in the history list. It has the same practical application as the back button in our web browsers. This method will not work if the previous page does not exist. This method does not contain any parameter.


1 Answers

AFAIK you won't be able to access the history from JavaScript. It will be a security hole and most of the browsers won't allow that. Probably there might be a workaround in IE by using ActiveX.

Found this entry which might be useful to you

window.history

There is a next property.

Returns the URL of the next item in the session history This property is not available to web content and is not supported by other browsers.

For security reasons the History object doesn't allow the non-privileged code to access the URLs of other pages in the session history, but it does allow it to navigate the session history.

There is no way to clear the session history or to disable the back/forward navigation from unprivileged code. The closest available solution is the location.replace() method, which replaces the current item of the session history with the provided URL.

like image 127
rahul Avatar answered Oct 22 '22 13:10

rahul