treehouse : what would you like to learn today?
Web Design Web Development iOS Development

load a function on page load

  • hi everyone i want to run a function in javascript when page loads. but after loading the page nothing happens! but if i manually refresh the page the function runs! i used these two methods but i dont know where is my problemo! :

      $(window).bind("load", function() {
    });
    

    and this: window.onload = showOnEdit;// showOnEdit is my function name

  • you can look for when the document is ready:

    $(document).ready(function() {
    //your javascript here...
    }
    

    or you can do it by calling onload on the body and then your javascript function. you seem to be missing the "" and the () when you call the function. this sould work fine:

    <body onload="showOnEdit()">
    

    hope this helped..

  • @CarlRosell thanks Carl, i will test "document ready" but i cant test the second one because my site is WordPress.