windows.location.href not working on Firefox3

前端 未结 17 989
囚心锁ツ
囚心锁ツ 2020-11-30 07:31

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 so

相关标签:
17条回答
  • 2020-11-30 08:06

    One observation to ensure in such a scenario

    Following will work in IE, but neither in Chrome nor in Firefox (the versions I tested)

     window.location.href("http://stackoverflow.com");
    

    Following will work all the three

    window.location.href = "http://stackoverflow.com";
    
    0 讨论(0)
  • 2020-11-30 08:08

    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>
    
    0 讨论(0)
  • 2020-11-30 08:09

    I just overcome the same problem. and the problem is not in javascript, but the href attribute on the <a> element.

    my js code

    function sebelum_hapus()
    {
    var setuju = confirm ("Anda akan menghapus data...")
    if (setuju)
    window.location = "index.php";
    }
    

    my previous html was

    <a href="" onClick="sebelum_hapus();">Klik here</a>
    

    and I update it to

    <a href="#" onClick="sebelum_hapus();">Klik here</a>
    

    or remove the href attribute

    hope this helps.

    0 讨论(0)
  • 2020-11-30 08:11

    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")
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-30 08:11

    For reference I had the same problem.

    onclick = "javascript: window.location('example.html');" didn't work under FF (latest)

    I just had to rewrite to onclick = "javascript: window.location = 'example.html';" to get it working

    0 讨论(0)
  • 2020-11-30 08:12

    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

    0 讨论(0)
提交回复
热议问题