简体   繁体   中英

what's wrong with using 'javascript:history.go(-1)'?

came across some twitter banter about how terrible it is to use

javascript:history.go(-1)

of course without any hints as to why.

If it's so terrible, why? What's the alternative? (besides history.back(), which seems to do exact same thing). Is it a matter of cross browser compatibility?

For example, I've seen it used on an error page to let users try to "go back" using the above.

I've tried google, but an no avail, in regards to why it'd be so horrible to use. Any pointers/explanations would be appreciated.

Thanks.

They may have been referring to use of the pseudo protocol javascript: , which generally means this is an anchor's href attribute, which is ugly. It is not a real link, and with JavaScript disabled, it'll do nothing whilst looking like a functional link. I think the only valid use for this protocol is for bookmarklets.

Also, as Russell Dias points out , it does exactly the same as the back button, so what is the point of having it? Should we have a reload button too that calls window.location.reload() ?

Off the top of my head. I believe that this javascript simulates the browser back button. One can never be too sure as to how a user got to a certain page, that is to say, they could have got to the page via an external site. Therfore this implementation would simply, send them back, which might not be a desirable result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM