Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between window.history.back & parent.history.back

Tags:

jquery

I was working on a website, where I need to go back to the page which I visited. I was using parent.history.back. And one of my friend suggested rather use window.history.back instead of parent.history.back. I'm still confused the difference. I hope I can get the exact difference between them.

Thanks in advance

like image 613
Nizam Ali Avatar asked Apr 02 '14 03:04

Nizam Ali


People also ask

What does window history back do?

Window History Back back() method loads the previous URL in the history list. This is the same as clicking the Back button in the browser.

Is history back safe?

So using javascript:history. back() introduces a small risk that can be exploited by phishing attacks. Just use explicit links to pages instead of relying on browser history.

What is window history go?

The history.go() method loads a URL (page) from the history list. The history.go() method only works if the page exist in the history list.

What is window history length?

The length property returns the number of URLs in the history list of the current browser window. The property returns at least 1, because the list includes the current page. This property is useful to find out how many pages the user has visited in the current browsing session.


2 Answers

window refers to the current frame, while parent refers the parent of the current frame.

So, use window in normal context. Use parent when dealing with iframes

If you don't have any <iframe> then go with window.history.back()

like image 180
Amit Joki Avatar answered Sep 17 '22 23:09

Amit Joki


window.history.go has the parameter can either be a number which goes to the URL within the specific position (-1 goes back one page, 1 goes forward one page), or a string. The string must be a partial or full URL, and the function will go to the first URL that matches the string.

For example:

window.history.go(-2) -  go back two pages:
window.history.go(1)  - go forward one page

parent.history.back() acts like a simple back button. The result is equal as if you clicked on the back browser button. There is have no parameter to be passed. it never forward like window.history

like image 37
Sudharsan S Avatar answered Sep 21 '22 23:09

Sudharsan S