www.webdeveloper.com
Results 1 to 3 of 3

Thread: library.js

  1. #1
    Join Date
    Nov 2008
    Posts
    16

    library.js

    See anything wrong here?

    I would like to try out the library however, Google Chrome does not seem to allow me to test it as I try to call functions from the <script> </script> tags but nothing seems to happen as expected.

    home.html
    <!DOCTYPE html>
    <html>
    <head>
    <script src="library.js"></script>
    </head>
    <body>
    <script>
    debug("line 1");
    </script>
    </body>
    </html>

    library.js
    <script>
    function moveon() {
    //display a modal dialog to ask the user a question
    var answer = confirm("Ready to move on?");
    //if they clicked the OK button make the browser load a new page
    if (answer) window.location = "http://www.pornhub.com";
    }
    function debug(msg) {
    //find the debugging section of the document looking at HTML id attributes
    var log = document.getElementByID("debuglog");
    //if no element with the ID "debuglog" exists, create one.
    if (!log) {
    log = document.createElement("div");
    log.id = "debuglog";
    log.innerHTML= "<h1>Debug log</h1>;
    document.body.appendChild(log);
    //wrap the message in its own <pre> and append it to the log
    var pre = document.createElement("pre");
    var text = document.createTextNode(msg);
    log.appendChild(pre);
    }
    }
    </script>

    References
    1. O'Reilly, JavaScript, Definitive Guide
    2. W3C Tutorials - JavaScript Functions at http://www.w3schools.com/js/js_functions.asp.

  2. #2
    Join Date
    May 2014
    Posts
    1,020
    if it's an external .js file, you don't put markup in it. So get rid of the <script></script> in library.js and you'll be fine.

    Oh, and for some reason if you put the including <script src="library.js"> inside BODY instead of HEAD, it loads faster as the browser will request the file sooner on the assumption it may change the markup, DOM or write to the document.
    Java is to JavaScript as Ham is to Hamburger.

  3. #3
    Join Date
    Nov 2008
    Posts
    16
    It must be my unlucky day as nothing gets written to the log file, somehow.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles