views:

12076

answers:

14

We have a javascript funtion 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?

+10  A: 

Have you tried just using

window.location = 'url';

In Firefox, window.location.href is a read-only property (from what I understand) and is not really used 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.

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

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

Dan Herbert
it is readonly in the sense that you can't change it, but setting a location.href is totally valid and works in all browsers (except in a cornercase scenario in IE6)
scunliffe
+1  A: 

I am not sure to follow you.
I just tried: going with FF3 to Lua 5.1 Reference Manual (long and with lot of anchors).
Pasting javascript:window.location.href="#2.5"; alert(window.location.href); in the address bar, I went to the right anchor and it displayed the right URL. Works also with a full URL, of course.
Alternative code: javascript:(function () { window.location.href="#2.5"; })();

Perhaps you forgot the #. Common problem, also with image maps.

PhiLho
Thanks for your post. I set the # char too. Can you try without the "alert" sentence? If I put an alert, somehow it works, if I remove it, it doesn't.
julio.g
If you remove it in the bookmarklet, you have to replace it with void(0) or put the whole stuff in a function call: the bookmarklet must not have a final value.
PhiLho
A: 

You could also use window.location.replace to jump to an anchor without register it in the browser history:

This article illustrates how to jump to an anchor and uses href as read-only property.

function navigateNext() 
{
    if (!window.location.hash) 
    {
        window.location.replace(window.location.href + unescape("#2"))
    } 
    else 
    {
        newItem = nextItem(window.location.hash)
        if (document.getElementById(newItem)) 
        {
            window.location.replace(stripHash(window.location) + "#" + newItem)
        } 
        else 
        {
            window.location.replace(stripHash(window.location) + "#1")
        }
    }
}
VonC
Why not just `window.location.hash = "1"`? All the replacing and stripping seems unnecessary.
Justin Johnson
+1  A: 

window.location.href works fine in all versions of Firefox, as does document.location.href I think that there is something else in your code that is breaking things.

drop this in a blank page, if it works, it indicates there is something else wrong on your page.

<script>
  window.location.href = 'http://www.google.com/';
</script>
scunliffe
A: 

Sorry for being a bit of topic here, but beware of a problem with IE where the referer is not reported when using document.locaton, document.replace etc.

Not sure if it is a bug but I have not found any documentation on this yet. All other browsers reports the referer using the mentioned methods/properties

My solution was to create a hidden A element in the code and used

var aElement = document.getElementById('my-a-element');
if (isIE)
  aElement.click();
else
  // document.location = aElement.href;

The click method is available in IE

@foo - yeah its a bug (#421)http://webbugtrack.blogspot.com/2008/11/bug-421-ie-fails-to-pass-http-referer.html
scunliffe
A: 

Have you tried this?

Response.Write("<script type='text/javaScript'> window.location = '#myAnchor'; </script>";);
Mr. Muskrat
+2  A: 

Maybe it's just a typo in your post and not in your code, but it's window and not window*s*

vincent
A: 

I have the same problem and I guess this is related to a click event.

I have a function that moves the browser to a specific page. I attach that function to some click events: in a button and in a image. AlsoI execute the function when the user press escape (document onkeypress event).

The results are that in all cases the function is called and executed, but only when there is a click the browser goes to the address I want.

Update I got it working! with a

setTimeout( "location.replace('whatever.html');", 0 );

I don't know why the location.replace wasn't working when the event was a keypress, but with the settimeout it works :)

Update Returning false after the event when you press escape makes the redirection works. If you return true or nothing the browser will not follow

graffic
A: 

Another option:

document.location.href ="..."
serg
A: 
window.location.hash = "#gallery";
A: 

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()
{
    windows.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 ..... onlclick="return JSNavSomewhere();" />

The false return value will cancell 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!

sprite1g
A: 

please add full javascript script tag

<script type="text/javascript" language="javascript"></script>
Ketan Mayangar
A: 

You've got to add return false; after the window.location.href as mentioned above.

function thisWorks()
{
    window.location.href = "http://www.google.com";
    return false;
}

function thisDoesNotWork()
{
    window.location.href = "http://www.google.com";
}
szabogi
A: 

thank you for your asnwers guys, i also had that issue

Omar
Hi, welcome at Stackoverflow! Please don't post comments as answers. They only adds noise. If you earn 50 reputation, then you'll be able to post a comment on other's answers. However, normally just upvoting the helpful answer is enough to say "thank you". Press the up arrow on the left hand side of the answer to give it an upvote. Also see http://stackoverflow.com/faq
BalusC