SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Jul 2006
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy arguments help please

    I'm a bit of a js newbie, but have read DOM Scripting and a lot of other good stuff on the subject, I'm trying to write a function that will add a link that says print after an element define in the head of the HTML document, this function is in my external js file:

    // Add Print Link after an element with the specified id ~ by Brent Lagerman
    function addPrintLnk(elementID) {
    if (!document.getElementById) return false;
    if (!document.getElementById(elementID)) return false;
    var lnkHere = document.getElementById(elementID);
    var printLnk = document.createElement("a");
    var lnkTxt = document.createTextNode("print");
    printLnk.appendChild(lnkTxt);
    lnkHere.appendChild(printLnk);
    printLnk.onclick = function(){
    window.print()
    }
    }

    and I'm calling it like this:

    <!--Add Print Links--><script type="text/JavaScript">addPrintLnk('AccessKeys');</script>

    it doesn't work obviously or I wouldn't be here - - firebug says addPrintLnk is undefined, but aren't I 'defining' it by adding the argument? I'm sure this is something easy, can anyone help me?

    b r e n t

  2. #2
    SitePoint Guru Chroniclemaster1's Avatar
    Join Date
    Jun 2007
    Location
    San Diego, CA
    Posts
    784
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ahhhh... ok, your script tag should only reference your .js file. DOM Scripting (or the newest name for it Unobtrusive Javascript) puts no actual js in the HTML file. Your error is telling you that the browser doesn't know where to find addPrintLink, it doesn't know about your js file. Rewrite your script tags to include the src attribute to specify the path to your external js file like this...

    Code:
    <script type="text/javascript" src="scripts.js"></script>
    That's all the Javascript related code you include in the webpage. Even your calls are made within the js file. If you are adding these links on an event like mouseover, simply specify them. If you're adding them in page load, I'd recommend Simon Willison's addLoadEvent function, it seems to have become the unobtrusive standard.

    http://simonwillison.net/2004/May/26/addLoadEvent/
    Whatever you can do or dream you can, begin it.
    Boldness has genius, power and magic in it. Begin it now.

    Chroniclemaster1, Founder of Earth Chronicle
    A Growing History of our Planet, by our Planet, for our Planet.

  3. #3
    SitePoint Member
    Join Date
    Jul 2006
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cool maybe that's part of the problem, but maybe not

    I didn't mention that I also am linking my global.js file before because I thought that was a given. The addPrintLnk function is in the global.js file, I just want an easy way to tell the links where to go on a page by page basis. I figured the best way would be to make argument and pass it the ids manually instead of hardcoding it on a page by page basis. Is it not unobtrusive to put a call to a function in the head of the page? I also already have the on page load function by Simon Wilson (remember I did read DOM Scripting) so I could just call the function that way, but then it'd be called on every page in my site (since I link in the global.js file to all)

    anyway, it still doesn't work ...

    <script type="text/JavaScript" src="/includes/global.js"></script>
    <script type="text/JavaScript" src="/includes/swfobject.js"></script><!-- FlashObject embed: http://blog.deconcept.com/ -->

    <!--Add Print Links--><script type="text/JavaScript">addPrintLnk('AccessKeys');</script>

  4. #4
    SitePoint Guru Chroniclemaster1's Avatar
    Join Date
    Jun 2007
    Location
    San Diego, CA
    Posts
    784
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sounds like you're less of a newbie than you think.

    My practice is to keep the script reference at the end of the HTML because linking in the head of the document doesn't work in unobtrusive scripting. The HTML controls have to be built on the webpage before the script can see them so it comes last on the page. Then I keep all my code in the external file; unobtrusive script is mainly a code separation technique. I was under the impression that DOM Scripting was basically the same thing, but obviously it sounds like that's not true. You can tell which I'm more familiar with. I hope I didn't muddy the waters, and that there's someone with a better answer to your question.
    Whatever you can do or dream you can, begin it.
    Boldness has genius, power and magic in it. Begin it now.

    Chroniclemaster1, Founder of Earth Chronicle
    A Growing History of our Planet, by our Planet, for our Planet.


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
  •