SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

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

    Referencing a file from an external .js

    When typing a path in an external .js file, do I go relative the the .js file or the page that has the <script src=".."> in it?

  2. #2
    .* draziW tnioPetiS *. bronze trophy
    Join Date
    Jun 2004
    Location
    "Then I figure the most good good guy will win."
    Posts
    1,666
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think it depends on how you like to work and how your site is built....

    I tend you use the root relative for head links, and absolute for all other links.

    Absolute: http://www.foo.com/some/folder/some/file.ext
    Root Relative: /start/at/the/root/of/server/to/your/doc.ext

    You could also go document relative if you feel like it:

    Code JavaScript:
    <script type="text/javascript" src="js/foo.js></script>

    Above would be a link relative to the calling document.

    • / = start at root of server.
    • ./ = start from this folder/location (the document location)
    • ../ = go up one folder from calling document/location
    • ../../ = go up two folders from calling document/location
    • ../../../../../ = 5 folders up from calling document/location


    Sometimes I use document relative for testing purposes... For example, I am working on a project that is a couple levels deep off of the root of the server, and I plan on moving everything to a new server very soon... So, I chose document relative linking... as soon as I get everything finalized, I will switch the links to absolute and/or root relative.

    Hope that helps!
    Cheers,
    Micky

  3. #3
    SitePoint Addict palgrave's Avatar
    Join Date
    Jan 2006
    Posts
    357
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for that Micky. I get the stuff about relative and absolute, I was just thinking about this scenario for example:

    I have a site that has two folders at root level. One is called images, the other is called js.

    The js folder contains javascript files that are linked to in the head of an html file.

    If I want to reference an image from a javascript file in the js folder, would it be ../images/picture.gif or images/picture.gif (as if the javascript was embedded in the html file.

    I'm actually using it for an image preloader, and it's kind of hard to tell if they have been preloaded when testing locally because everything happens quickly anyway.

  4. #4
    .* draziW tnioPetiS *. bronze trophy
    Join Date
    Jun 2004
    Location
    "Then I figure the most good good guy will win."
    Posts
    1,666
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ahhh, I see now. Sorry, I totally mis-understood your first post. Sorry bout that.

    Well, I think it is like a CSS file... Relative to the calling document. So, relative to the JS file, and not from where it [the js link] is being called [via the HTML <script src=".."> call].

    So, your example:

    • ROOT/
      • JS/foo.js
      • IMAGES/foo.gif


    If you call from foo.js, you would do this:

    ../IMAGES/foo.gif

    or:

    /IMAGES/foo.gif

    The last time I played around with pre-loading, and other things, I just setup some "settings" vars at the top of my script, like so:

    Code JavaScript:
    // Settings:
    // Absolute and/or relative path to folder that contains images:
    var imgLoc = '/images/template/';
    // Name of image to display if error occurs:
    var errorImage = 'error.gif';
    // pngsToLoad array -- Array of png high-resolution images to load via JS:
    var pngsToLoad = new Array('foo.png'); // Add more here.

    ...and then I would just concatenate the vars to access the images.

    Worked well for me.

    Hope that helps.
    Cheers,
    Micky

  5. #5
    .* draziW tnioPetiS *. bronze trophy
    Join Date
    Jun 2004
    Location
    "Then I figure the most good good guy will win."
    Posts
    1,666
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Or did I completely mis-understand you again?


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
  •