tags:

views:

78

answers:

4

I want using javascript to see if there is history or not, I mean if the back button is available on the browser or not.

Thanks

A: 

I'm not sure if this works and it is completely untested, but try this:

<script type="text/javascript">

    function goBack() {
        history.back();
    }

    if (history.length > 0) { //if there is a history...
        document.getElementsByTagName('button')[].onclick="goBack()"; //assign function "goBack()" to all buttons onClick
    } else {
        die();
    }
</script>

And somewhere in HTML:

<button value="Button1"> //These buttons have no action
<button value="Button2">

EDIT:

What you can also do is to research what browsers support the back function (I think they all do) and use the standard JavaScript browser detection object found, and described thoroughly, on this page. Then you can have 2 different pages: one for the "good browsers" compatible with the back button and one for the "bad browsers" telling them to go update their browser

Latze
`history.back` is a function. You want to check if `history.length > 0` then if it is go back with `history.back()`
pixl coder
oh, thanks for the feedback
Latze
Also `[]` on a NodeList makes no sense, and you can't assign a string to an event handler.
bobince
+2  A: 

Check if window.history.length is equal to 0.

CD Sanchez
depends on the browser
McAden
+1  A: 

You can't directly check whether the back button is usable. You can look at history.length>0, but that will hold true if there are pages ahead of the current page as well. You can only be sure that the back button is unusable when history.length===0.

If that's not good enough, about all you can do is call history.back() and, if your page is still loaded afterwards, the back button is unavailable! Of course that means if the back button is available, you've just navigated away from the page. You aren't allowed to cancel the navigation in onunload, so about all you can do to stop the back actually happening is to return something from onbeforeunload, which will result in a big annoying prompt appearing. It's not worth it.

In fact it's normally a Really Bad Idea to be doing anything with the history. History navigation is for browser chrome, not web pages. Adding “go back” links typically causes more user confusion than it's worth.

bobince
regarding length - not even then in all browsers. Some browsers count the current page as a history item and start at 1. You'd have to include browser detection.
McAden
Actually I thought it was always `1`! The `0` was a brainfart, I thought browsers would always respond with `1` or more. Turns out Opera and IE think otherwise—good catch.
bobince
+4  A: 

The only accurate way would be checking the property:

history.previous

However, the problem with this is that in most browsers this is considered a security violation and usually just returns undefined.

history.length

Is a property that others have suggested...
However, the length doesn't work completely because it doesn't indicate where in the history you are. Additionally, it doesn't always start at the same number. IE, for example, starts at 1 while Google Chrome starts at 0.

alt text

Most of the time a link is added that calls:

history.back();

or

 history.go(-1);

and it's just expected that if you can't go back then clicking the link does nothing.

McAden