GregM
GregM

Reputation: 3734

Why does my javascript not get called in my onload?

I have my onload set to a function that calls my custom function

in the function my window should scroll to the location specified via the value after the hash - however I do not see my console log my debug text nor does the page move - what am I doing wrong?

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<script type="text/javascript">
window.onload = function()
    {
        scrollToLocation();
    };
function scrollToLocation()
{
    console.log("hi");
    var x = location.hash;
    console.log(x);
    $('html, body').animate(
    {
        scrollTop: x
    }, 500);
    var d = document.createElement("div");
    d.style.height = "101%";
    d.style.overflow = "hidden";
    document.body.appendChild(d);
    setTimeout(function ()
    {
        d.parentNode.removeChild(d);
    }, 10);
    return false;
}
</script>

Upvotes: 0

Views: 81

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388316

OK.

I can't figure out why the window load function is not firing.

Try document ready handler instead

$(function(){
     scrollToLocation();
})

As for hashchange event is considered have a look at this plugin

Upvotes: 2

Related Questions