SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Addict palgrave's Avatar
    Join Date
    Jan 2006
    Posts
    357
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Is this an accessible link?

    Hi folks,

    I am designing a page that has javascript to change the content of a layer based on http://www.sitepoint.com/article/lay...ent-javascript

    Is it ok to make the link like this:

    HTML Code:
    <a href="page.htm" onclick="WriteLayer('content',null,sText); return false;">link</a>
    or is that a really silly JavaScript mistake?

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    It would be better to use unobtrusive methods. Use the DOM to find your link and then add the click event listener to it.

    For example:
    HTML Code:
    <a href="page.html" id="p">link</a>
    Code Javascript:
    document.getElementById('p').onclick = function(e) {
      if (!e) window.event.returnValue = false;
      else e.preventDefault();
      WriteLayer('content'), null, sText);
    }
    Now you don't have any javascript in your HTML and you can put it in the <head>, where it should be.

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,527
    Mentioned
    84 Post(s)
    Tagged
    4 Thread(s)
    I thought policy was changing on that now, so that the end of the document is where most of the javascript should be loading from.

  4. #4
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Whose policy?

    If you want your javascript to execute once the document has been parsed but not wait for images to download, then you should do it on the DOMContentLoaded event (and "similar" things for IE and Safari) and not via the window's onload event.

  5. #5
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,527
    Mentioned
    84 Post(s)
    Tagged
    4 Thread(s)
    Who's policy? Standard policy for faster web pages.

    See Yahoo's Best Practices for Speeding Up Your Web Site

  6. #6
    SitePoint Addict palgrave's Avatar
    Join Date
    Jan 2006
    Posts
    357
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you.

  7. #7
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    That might be Yahoo's suggestion, but that doesn't make it "standard policy". Besides, the difference in speed will be insignificant unless you have a super heavy script. By far the best optimisation you can make is make fewer HTTP requests. E.g. making sprites instead of using separate images in CSS or combining stylesheets/scripts into a single file.

    Besides, SCRIPT elements in the body are a bit like "pollution" to me. Probably a bit over the top.

  8. #8
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,527
    Mentioned
    84 Post(s)
    Tagged
    4 Thread(s)
    The difference in speed can be quite significant when people are on a dial-up connection instead of broadband.

    The main concern for myself with having scripts in the head is that all further processing of the page is halted until the script has been loaded, parsed and processed. It is for this particular reason that I agree with placing the script at the very bottom of the body.

    Getting into controversial territory, I believe that the head section should be for only an informational purpose. Having scripts running in there feels akin to mixing model and control components of a framework. *shudder*


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
  •