Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

windows.location.href not working on Firefox3

Tags:

We have a JavaScript function named "move" which does just "windows.location.href = any given anchor".
This function works on IE, Opera and Safari, but somehow is ignored in Firefox. Researching on Google doesn't produce a satisfactory answer why it doesn't work.
Does any JavaScript guru knows about this behavior, and what would be the best practice to jump to an anchor via JavaScript?

like image 732
julio.g Avatar asked Nov 08 '08 19:11

julio.g


People also ask

Does window location href work on mobile?

location. href and click() methods doesn't work on mobile phone. Bookmark this question.

Does window location work in all browsers?

All modern browsers map document. location to the window. location but you can prefer window.

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.


2 Answers

Have you tried just using

window.location = 'url';

In some browsers, window.location.href is a read-only property and is not the best way to set the location (even though technically it should allow you to). If you use the location property on its own, that should redirect for you in all browsers.

Mozilla's documentation has a pretty detailed explanation of how to use the window.location object.

https://developer.mozilla.org/en/DOM/window.location

like image 111
Dan Herbert Avatar answered Feb 06 '23 18:02

Dan Herbert


If you are trying to call this javascript code after an event that is followed by a callback then you must add another line to your function:

function JSNavSomewhere()
{
    window.location.href = myUrl;
    return false;
}

in your markup for the page, the control that calls this function on click must return this function's value

<asp:button ........ onclick="return JSNavSomewhere();" />

The false return value will cancel the callback and the redirection will now work. Why this works in IE? Well I guess they were thinking differently on the issue when they prioritized the redirection over the callback.

Hope this helps!

like image 33
sprite Avatar answered Feb 06 '23 18:02

sprite