MartyIX
MartyIX

Reputation: 28648

window.location alternatives in JavaScript

What is a standardized alternative to window.location.href property? I was checking on w3schools.com that it is implemented in all major browsers so there's no problem but I'm rather curious how to do that properly.

From here is the statement that the function is not standardized: https://developer.mozilla.org/en/DOM/window.location

Thanks!

Upvotes: 6

Views: 17317

Answers (4)

brasofilo
brasofilo

Reputation: 26065

Using location.href is having a collateral effect on WordPress dashboard.
If I add buttons that use location.href for navigation, it prompts an alert pop up asking if I want to leave the page without saving.

Using location.replace works great though. It's description explains its difference to the other alternative, assign():

The replace() method of the Location interface replaces the current resource with the one at the provided URL. The difference from the assign() method is that after using replace() the current page will not be saved in session History, meaning the user won't be able to use the back button to navigate to it.

Upvotes: 0

bobince
bobince

Reputation: 536379

The window object goes back as far as the original JavaScript implementation in Netscape 2.0, and location has existed in every browser since then.

The Browser Object Model outside of the DOM has tradionally not been standardised, but that changes with HTML5, which documents the window object and its location property explicitly.

Upvotes: 8

Tim Down
Tim Down

Reputation: 324567

There's no standardized way of doing it. The most well-supported way of redirecting the browser is using the href property of the location object:

window.location.href = "http://www.google.com/";

Upvotes: 1

SLaks
SLaks

Reputation: 887433

There is nothing wrong with setting window.location.

Upvotes: 8

Related Questions