Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens to code after a javascript redirect (setting window.location.href)?

I have the following javascript redirect code followed by some more code.

window.location.href = '/someurl'; alert('hello'); alert('hello again'); 

This causes a browser inconsistency.

In firefox, the first alert is visible for a split second right before getting redirected. The second alert is not visible at all.

In chrome, both alerts will pop up, and the redirect happens only after clicking ok for both alerts.

Is there some concept of what happens to code after the redirect that can resolve these differences? If the redirect is asynchronous, then what is chrome doing? I would like to understand what is happening so that I can determine what to do in a more complicated scenario where a redirect is buried deep within some callbacks and other logic.

like image 276
user1385729 Avatar asked May 09 '12 23:05

user1385729


People also ask

What happens after window location href?

window.location.href returns the href (URL) of the current page. window.location.hostname returns the domain name of the web host. window.location.pathname returns the path and filename of the current page.

What does location href do in JavaScript?

The location. href property sets or returns the entire URL of the current page.

Does window location href reload the page?

window. location. href method returns/loads the current url. So we can use it to reload/refresh the page in javascript.

What is window top location href?

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.


1 Answers

The Javascript session will struggle to continue it's mission in the face of impending doom. Coding in this manner is considered unpredictable. ...and cruel.

like image 162
Billbad Avatar answered Sep 29 '22 09:09

Billbad