Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between window.location.href and top.location.href

Can Anyone tell me the difference between window.location.href and top.location.href ?

And also where to use which one.

And which one will be better when redirecting after an ajax call in mvc?

like image 547
Egalitarian Avatar asked Jul 26 '10 06:07

Egalitarian


People also ask

What is the difference between window location and document location?

window. location is read/write on all compliant browsers. document. location is read-only in Internet Explorer (at least), but read/write in Gecko-based browsers (Firefox, SeaMonkey).

What is window location href used for?

Window Location Href href property returns the URL of the current page.

What is the difference between location reload () and window location reload ()?

location. reload(true); reloads the page from the server instead of from the cache, window. location. reload(); would do the same thing as location.


1 Answers

window.location.href returns the location of the current page.

top.location.href (which is an alias of window.top.location.href) returns the location of the topmost window in the window hierarchy. If a window has no parent, top is a reference to itself (in other words, window === window.top).

top is useful both when you're dealing with frames and when dealing with windows which have been opened by other pages. For example, if you have a page called test.html with the following script:

var newWin=window.open('about:blank','test','width=100,height=100'); newWin.document.write('<script>alert(top.location.href);</script>'); 

The resulting alert will have the full path to test.html – not about:blank, which is what window.location.href would return.

To answer your question about redirecting, go with window.location.assign(url);

like image 165
josh3736 Avatar answered Oct 14 '22 22:10

josh3736