-3

How can I run javascript code just after page load, when page is loaded, it trigger, and when I go to another page it also trigger another piece of code for that page, how can I do it ?

Khalid Kamal
  • 27
  • 1
  • 1
  • 1

2 Answers2

5

You can try

<!DOCTYPE html>
<html>
    <head>
        <title>Test</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <script type="text/javascript">
        function codeAddress() {
            alert('ok');
        }
        window.onload = codeAddress;
        </script>
    </head>
    <body>

    </body>
</html>

Click here http://jsfiddle.net/NEfR2/

sagar43
  • 3,341
  • 3
  • 29
  • 49
0

document.ready() is a jQuery event. JQuery’s document.ready() method gets called as soon as the DOM is ready (which means that the browser has parsed the HTML and built the DOM tree). This allows you to run code as soon as the document is ready to be manipulated.

For example, if a browser supports the DOMContentLoaded event (as many non-IE browsers do), then it will fire on that event. However, IE can’t safely fire until the document’s readyState reaches “complete”, which is typically later.

You can find more info here (of course, this requires jQuery to be loaded)

facundofarias
  • 2,973
  • 28
  • 27
  • 1
    Ready is not a method of document - it is as you pointed out - a function of the jQuery library which accepts the document as an parameter. $(function() { // Handler for .ready() called. }); does exactly the same. – axel.michel Dec 24 '14 at 08:45