Should I Use Window.Navigate or Document.Location in JavaScript

Should I use window.navigate or document.location in JavaScript?

window.location.href = 'URL';

is the standard implementation for changing the current window's location.

What's the difference between window.location and document.location?

According to the W3C, they are the same. In reality, for cross browser safety, you should use window.location rather than document.location.

See: http://www.w3.org/TR/html/browsers.html#dom-location

window.navigate works only in Internet Explorer

window.navigate is a non-standard Internet Explorer feature. Other browsers simply don't provide the function.

You could shim it with:

if (! window.navigate) {
window.navigate = function (arg) {
location.assign(arg);
}
}

… but your code would be better if you just rewrote it to use standard methods (i.e. the location object) in the first place.

What's the difference between window.location= and window.location.replace()?

window.location adds an item to your history in that you can (or should be able to) click "Back" and go back to the current page.

window.location.replace replaces the current history item so you can't go back to it.

See window.location:

assign(url): Load the document at
the provided URL.

replace(url):Replace the current
document 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.

Oh and generally speaking:

window.location.href = url;

is favoured over:

window.location = url;

How to get the browser to navigate to URL in JavaScript

This works in all browsers:

window.location.href = '...';

If you wanted to change the page without it reflecting in the browser back history, you can do:

window.location.replace('...');

window.location.assign() navigate to user input of url?

First, instead of putting the function in "onclick" in the button, I suggest putting it on the form element's "onsubmit" handler. That way, a simple "Enter" key can also cause the navigation.

Second, since we're putting the callback on the form, the form's action should changed to 'javascript', like this:

<form style="padding-top 20px;" action="javascript://#" onsubmit="newUrl(this.elements['url'].value)">
URL: <input type="text" name="url">
<input type="submit" value="GO!">
</form>

I've put the url in the first parameter of the "newUrl" function, for ease of writing.

Finally, your "newUrl" function:

function newUrl(url) {
window.location.assign(url);
}

window.location.href and window.open () methods in JavaScript

window.location.href is not a method, it's a property that will tell you the current URL location of the browser. Changing the value of the property will redirect the page.

window.open() is a method that you can pass a URL to that you want to open in a new window. For example:

window.location.href example:

window.location.href = 'http://www.google.com'; //Will take you to Google.

window.open() example:

window.open('http://www.google.com'); //This will open Google in a new window.


Additional Information:

window.open() can be passed additional parameters. See: window.open tutorial

What is the difference between document.location.href and document.location?

document.location is a synonym for window.location that has been deprecated for almost as long as JavaScript has existed. Don't use it.

location is a structured object, with properties corresponding to the parts of the URL. location.href is the whole URL in a single string. Assigning a string to either is defined to cause the same kind of navigation, so take your pick.

I consider writing to location.href = something to be marginally better as it's slightly more explicit about what it's doing. You generally want to avoid just location = something as it looks misleadingly like a variable assignment. window.location = something is fine though.

window.navigate error

Please use

window.location.href = "feltoltott_kepek_elozmeny.php?show=" + to;


Related Topics



Leave a reply



Submit