SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Dec 2007
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Noob question: importing core.js library

    Can someone please help with this

    MY HTML FILE:
    _____________________________________________________
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

    <html>
    <head>
    <title>Page title</title>
    <script type="text/javascript" src="core.js"> </script>
    <script type="text/javascript" src="test.js"> </script>
    </head>
    <body>

    <p>
    <a id="oliver" href="http://www.google.com">Oliver Twist</a>
    </p>


    </body>
    </html>
    __________________________________________________


    My JAVASCRIPT FILE (called test.js) and the core.js file is in the same directory.

    __________________________________________________

    function marker();
    {
    var koko = document.getElementById("oliver");
    koko.setAttribute("href","http://www.yahoo.com");
    };

    Core.start(marker);


    __________________________________________________

    This causes a Javascript error in IE and does not replace the Google link with the Yahoo link as the code intends. I am trying to just get the basics down of importing the core library. I cannot figure out what I am doing wrong.

  2. #2
    Google Engineer polvero's Avatar
    Join Date
    Oct 2003
    Location
    Mountain View
    Posts
    567
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    can you link us to the working example on your website?

  3. #3
    SitePoint Enthusiast SoMBrA's Avatar
    Join Date
    Oct 2007
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think i can tell you what the problem is.
    Core.start adds event to the "window.onload" that will trigger the INIT method of the object name you pass as a parameter. In this case you are calling a function...

    If you want to make it work, create a object and include the code you want to be executed inside a method called init.

    Example:

    var marker = {
    init: function() {
    var koko=document.getElementById("oliver");
    koko.href="http://www.yahoo.com";
    }
    };
    Core.start(marker);

  4. #4
    SitePoint Member
    Join Date
    Dec 2007
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thank You!!

    That did it!!!

  5. #5
    SitePoint Enthusiast SoMBrA's Avatar
    Join Date
    Oct 2007
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    im glad it worked
    I learned the javascript i know from that book, and then moved to other resources

    I have to tell you that Core library is really good so keep it near you at the beginning

  6. #6
    SitePoint Member
    Join Date
    Dec 2006
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Core.start vs. straight window.onload

    I attended a recent talk where the speaker used window.onload stating that was all you needed to do. I went up later asked him about the Core.start or the dojo equivalent, I've been using and he basically said not necessary. BTW his onload called a function that called a number of other functions.

    Is it just a matter of style? Keeping the js out of the HTML? Using event listeners instead of events?

    Thanks for the clarification.
    Rkpt.

  7. #7
    SitePoint Enthusiast SoMBrA's Avatar
    Join Date
    Oct 2007
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The problem with using just window.onload is that if you use it on different scripts the last one you call will overwrite your previous window.onload used on other script so you have to attach the event (addEventListener), this way you can start all you need without any worries.

    The approach you explaining its ok but you will always have to find that window.onload and keep adding all the functions you want to start...

    Keep the JS out of the HTML is to separate behaviour from presentation (css) and the markup (html).


Bookmarks

Posting Permissions

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