views:

49

answers:

4

As the title says, will this code work and will it work in major browsers?

I ask because currently I have no resources to test it, so I would appreciate some help on this.

Here is what I have (not tested):

setTimeout(window.location.history.go(-2), 5000);

Thanks

A: 

as you can see here this is supported by all browsers for a long time (since ff1.0 / opera 5 / ie 3).

oezi
Should I have double-quotes around the window.location?
Camran
A: 

It works on Netscape 2.0+, IE3+, Opera 5.12+, Firefox 1+, Konquerer 3.1+, Safari 1+. You just have to be sure, there are at least so many sites in the history, you want to go back.

German reference on SELFHTML

Keenora Fluffball
A: 

It has been around since the first version of JavaScript, so it's universally supported. Please note, though, that your code will not work as it currently is because you're calling go now, and passing the result of the function as the function reference. Also, it's just history, not location.history. Try this instead:

setTimeout(function() { history.go(-2); }, 5000);
Delan Azabani
Have tested it just now, didn't happen anything. Hmm
Camran
Try now; I've edited the post to fix an error.
Delan Azabani
+4  A: 
setTimeout(window.location.history.go(-2), 5000);

history is a property of window, not location. Also if you want it to trigger after a delay you will need to make a delayed-call function—currently you are calling go() immediately, and passing the return value of the function to setTimeout, which clearly won't work. You probably mean:

setTimeout(function() {
    history.go(-2);
}, 5000);

As for ‘go back two pages’, yes, it'll work in pretty much all JS-supporting browsers, but it's the kind of thing users are likely to find incredibly confusing. Are you sure you want to do that?

bobince
It works... Thanks
Camran