SitePoint Sponsor

User Tag List

Results 1 to 25 of 25
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Server Root Question

    Hey all,

    I'm working on a site right now and am using a script that rotates the top banner logo...

    The problem I'm having now is for pages that are located in different directories... Because the image folder is located in the root folder, any pages located in sub-directories do not display the image...

    I've tried putting a forward slash (/) in front of the path, but unfortunately that only works when testing it out on the server... When developing on the local machine here (no web server present), I can't get the image to show up.

    Is there anything in the DOM that allows me to specify a root folder? Something similar to PHP's $_SERVER['DOCUMENT_ROOT'] variable?

    Thanks for any help.

  2. #2
    SitePoint Zealot Saj's Avatar
    Join Date
    May 2003
    Location
    USA
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nope. Javascript is only Client-side. A.k.a. it cannot do anything server related like write files, read files, connect to databases or anythig like that. Also, you cannot use the Document Root in Javascript in any way.

  3. #3
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually - you can probably get away with this using the base element. It defines either the base target or href (which modifies any src attributes in the page as well), or both of them.

    <base href="C:whateverdirectory" />

    Just put it in the <head> and make sure to comment it out or delete it when uploading.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  4. #4
    SitePoint Enthusiast
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by beetle
    Actually - you can probably get away with this using the base element. It defines either the base target or href (which modifies any src attributes in the page as well), or both of them.

    <base href="C:whateverdirectory" />

    Just put it in the <head> and make sure to comment it out or delete it when uploading.
    Beetle,

    Thanks for the tip... I thought it would work, but it didn't...

    I've narrowed the problem down to the external JS files I'm using in my pages... Only the images referenced in those external files are causing the problem.

    ie: I have the home page which is located in the root dir, and then I have an images folder... Any pages located in a subfolder of the root directory have the problem where the JS random image function will not display the image because the JS function is written to read the files as though the HTML page was located in the root directory.

    Is there any way I can adjust the script so that it would work regardless of where the page is located?


    Thanks again!

  5. #5
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Most likely - provided you know how to properly make a relative path. Do you?

    I'm surprised the base didn't work. You sure you used backslashes for the path?

    <base href="C:\some\directory\" />
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  6. #6
    SitePoint Addict
    Join Date
    Aug 2001
    Location
    Los Angeles, CA
    Posts
    346
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nope, what you need to do is this:

    <img src="../images/img.jpg">

    The two periods before the slash tell the browser to go to your root directory, and then to the images folder directly under it.

  7. #7
    SitePoint Enthusiast
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by beetle
    Most likely - provided you know how to properly make a relative path. Do you?

    I'm surprised the base didn't work. You sure you used backslashes for the path?

    <base href="C:\some\directory\" />
    Yeah, I know how to make a relative path, but when working on the local machine, I run into problems... Unfortunately, this machine doesn't have it's own server, so using a relative path causes problems when files reside in different directories...

    I know, I thought that would have solved it all.... Actually, I tried it both ways, front and back, and neither seemed to work...

    The problem is in the external javascript file... I reference the image files as if the script was in the root directory (images/foo.gif), but when the script is called from files located in subdirectories, the "images/foo.gif" doesn't work, as I need to use "../images/foo.gif" or "/images/foo.gif"....

    It works fine for the files in subdirectories, but then when I call pages located within the root dir (ie: index.html, etc..), those pages now display the "X" rather than the image.

    Hope that explains it a little more...

  8. #8
    SitePoint Enthusiast
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by gregc
    Nope, what you need to do is this:

    <img src="../images/img.jpg">

    The two periods before the slash tell the browser to go to your root directory, and then to the images folder directly under it.
    Greg,

    Thanks for the tip... Unfortunately, it doesn't solve my problem... Read the post above for the details, and you'll see why I can't use that path to refer to my images...

    Thanks!

  9. #9
    SitePoint Addict
    Join Date
    Aug 2001
    Location
    Los Angeles, CA
    Posts
    346
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well show us the random image script, because that's the only way to do what you want I think (putting the two periods, slash, and then the directory right under the root that you want to access).

  10. #10
    SitePoint Enthusiast
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's the script I'm using... Hopefully this will help you figure out the problem... I'm still lost on this one!

    Code:
    function rotate(){
    
    	var image = (Math.floor(Math.random() * 13));
    	document.splash.src = (root + "images/splash/splash_" + banner_images[image] + ".jpg");
    
    }

  11. #11
    SitePoint Addict
    Join Date
    Aug 2001
    Location
    Los Angeles, CA
    Posts
    346
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Easy, all you have to do is implement my idea into your script, like so:

    Code:
    document.splash.src = ("../images/splash/splash_" + banner_images[image] + ".jpg" );



    See the two periods and the slash? That will point to the root directory, and the browser will go to your images folder that is directly under your root directory. That's all man, piece of cake.

  12. #12
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by gregc
    Easy, all you have to do is implement my idea into your script, like so:

    Code:
    document.splash.src = ("../images/splash/splash_" + banner_images[image] + ".jpg" );



    See the two periods and the slash? That will point to the root directory, and the browser will go to your images folder that is directly under your root directory. That's all man, piece of cake.
    That will only work if the script is called from pages which are in folders which are siblings of the images folder, not from anywhere in the site.

    For that to work, root-relative paths must be used. If it's not working on your local server I can only assume that you've got what is your live server's web root mapped to a virtual folder on your local server...

    i.e. http://www.yourserver.com/ = http://localhost/testsite/

    Does that make sense?

    Simply make your local web root the root of your site, either by replacing whatever is your current local root site, or by installing the site in question as a standalone site on a different port (e.g. 81).

    If that's not crystal clear, it's probably because I'm sleep deprived and slightly merry.... ZZZZzzzzzzz.........
    MarcusJT
    - former ASP web developer / former SPF "ASP Guru"
    - *very* old blog with some useful ASP code

    - Please think, Google, and search these forums before posting!

  13. #13
    SitePoint Addict
    Join Date
    Aug 2001
    Location
    Los Angeles, CA
    Posts
    346
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't agree Marco. I've used the same method to call a single text file from every page of my site, and I haven't had any problems at all. If the page he is using the periods in is already in the root directory, the browser will ignore the periods and will still get to the right folder.

  14. #14
    SitePoint Enthusiast
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by gregc
    I don't agree Marco. I've used the same method to call a single text file from every page of my site, and I haven't had any problems at all. If the page he is using the periods in is already in the root directory, the browser will ignore the periods and will still get to the right folder.
    Well, I tried your method... But it still won't work for pages that are located in the root directory.

    I'm not sure if I made it clear before, but the system I'm working on right now doesn't have a webserver, so I'm running directly off the D: drive... There's no webroot directory or anything, it's just a regular static site setup...

    I won't upload the site to the server until it's ready to go, and then I'm sure everything will work fine, but until then I guess i'll have to settle with having the images show up as "Xs"....

    Unless there may be another suggestion?


    Thanks,
    Frank

  15. #15
    SitePoint Addict
    Join Date
    Aug 2001
    Location
    Los Angeles, CA
    Posts
    346
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can always pass part of the image path to your function, like so:

    Code:
    function rotate(imgPath)
    Code:
    {
    var image = (Math.floor(Math.random() * 13));
     
    document.splash.src = (imgPath+"/splash_" + banner_images[image] + ".jpg" );
     
    }
    </script>
     
    <img src="img.jpg" OnMouseOver="rotate('images/splash')" OnMouseOut="rotate('images/splash')"> 
     
    ^^ THE ABOVE WOULD MAKE THE ROTATE SCRIPT WORK FOR A PAGE ALREADY IN THE MAIN DIRECTORY.
     
    <img src="img2.jpg" OnMouseOver="rotate('../images/splash')">


    See what I'm doing? This should work...

  16. #16
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by gregc
    I don't agree Marco. I've used the same method to call a single text file from every page of my site, and I haven't had any problems at all. If the page he is using the periods in is already in the root directory, the browser will ignore the periods and will still get to the right folder.
    grec
    I'm sorry that you don't agree, but it's sinply not a matter for debate - I'm stating the way that URLs are designed to work! It's a fact, and it's in the W3C specs (don't have the reference, but look it up if you don't believe me).

    As I said earlier, the URL "../images/img.jpg" will only resolve correctly if the calling page is in a sibling folder of the images folder, although it is possible (as you suggest) that a page in the root would work too (but this behaviour may vary between browsers, since it's up to the user-agent to decide how to handle the broken URL), which might explain why it has worked for you so far, but pages lower down (e.g "/admin/security/users.asp") certainly won't work, and that's what I'm talking about. Giving root-relative paths as I suggest works *ALL* of the time, whereas using paths relative to the current document as you suggest will only work *SOME* of the time if you're reusing the same path for pages (at different levels of the folder tree) throughout a site.

    Anyway, I don't want to go on, so I'll leave it there.


    Ikonified
    Stop testing your sites by simply accessing straight from disk - no-one develops that way because it's not the same environment as a real web server, and it will cause you unnecessary problems (just like you're experiencing now). Since you're not using server-side scripting (which you also wouldn't be able to test in the way you do currently), any free web server software will do you fine - Apache (free from here) will do you fine, and if you're running NT/2K/XP Pro (but not XP Home) then you can install IIS (which comes with these OSes) via Control Panel --> Add/Remove Programs --> Windows Components.

    Once up and running, you will have a proper development/test environment, and you will see that everything I have said is true!
    MarcusJT
    - former ASP web developer / former SPF "ASP Guru"
    - *very* old blog with some useful ASP code

    - Please think, Google, and search these forums before posting!

  17. #17
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I 2nd everything M@rco said
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  18. #18
    SitePoint Enthusiast
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the information everyone! I really appreciate the effort!

    Unfortunately, I have to test on this bare-bone system for the time being... Not my choice... and I don't have a choice!

    Anyways, I guess I'll have to wait until it's up on the test server before I can really assess whether or not a problem exists... But I will definately keep everyone's suggestions in mind!

    Thanks again!
    Ik

  19. #19
    SitePoint Addict
    Join Date
    Aug 2001
    Location
    Los Angeles, CA
    Posts
    346
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Ikonified
    Thanks for the information everyone! I really appreciate the effort!

    Unfortunately, I have to test on this bare-bone system for the time being... Not my choice... and I don't have a choice! [img]images/smilies/wink.gif[/img]

    Anyways, I guess I'll have to wait until it's up on the test server before I can really assess whether or not a problem exists... But I will definately keep everyone's suggestions in mind!

    Thanks again!
    Ik
    Did you try my script?

  20. #20
    SitePoint Enthusiast
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by gregc
    Did you try my script?
    Hi Greg,

    Yes, I used your script, and it worked great for the pages I tried... The only problem I have is that because we have a large amount of pages, the amount of time needed to make those changes to every page are beyond my control right now...

    I'm hoping that once the site is up on a server, the problem will be rectified by using the "/images/foo.gif" so that no matter where the pages are located (root or sub), the images will always be found.

    If you know of an easier way I can make the changes, please let me know...

    Thanks again for your help... It definately opened up my eyes!

  21. #21
    SitePoint Addict
    Join Date
    Aug 2001
    Location
    Los Angeles, CA
    Posts
    346
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Then use external files or something! Sheesh, you wanted a solution and I gave you one, but there's still problems? Is there any way to solve your problem then? Come on man, I had to ask you if you even tried the script, at least be appreciative that people like me take time to answer your questions and help you with your programming...

    I altered your script exactly the way you needed it to be. Why post a thread like this if you're just going to say that you don't want to bother implementing it into all the pages?

  22. #22
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Greg - I think you should not take things so personally. Ikonified thanked everyone for for their effort and that should be sufficient. Just because you present a solution doesn't mean he'll like/want/use it -- even if it does solve his problem. Perhaps he was looking-for/needing something quicker to implement. If he's not understanding a solution involving external files, then go the distance and help him out.

    Lighten up - keep throwing that attitude around and people won't appreciate your help -- regardless of your time investment.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  23. #23
    SitePoint Addict
    Join Date
    Aug 2001
    Location
    Los Angeles, CA
    Posts
    346
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well you're right Beetle, I was a bit out of line. But then again, it's somewhat frustrating when so many people have put time into something that didn't really get anywhere. And also please do not generalize this as if I act rudely all the time -- that is a generalization and is completely untrue. I help as many posters on this JS board as anybody, and I'd appreciate a little more respect.

    And Ikonified, I'm not sure if you know what external JS files are. In case you don't know how to use them, do the following steps:

    - Open Notepad, and save a blank document as jsInclude1.js. This will create an external JavaScript text file.

    - Take the code from my script and paste it into Notepad (leave out the <script> and </script> tags, though). Then save it.

    - Paste this code into the HTML page that you want to implement my script into:
    Code:
    <script language="JavaScript" src="jsInclude1.js"></script>
    That line of code will implement my script into every HTML page you write it into...and copying one line of text into a bunch of HTML pages won't be too much hassle, right? Also, if you don't want to bother with the parameters in my script (the imgPath), you can always create a second external JS file for web pages that are already in your root folder.

    And just to let you know, there essentially isn't any difference whether you're running your website locally on your computer or over the web. Uploading your site won't change anything unless you've got a bunch of links that have C:\ in them and so forth...so you should get it to work now because nothing is going to magically fix itself by just uploading it to the web.
    Last edited by gregc; Jun 16, 2003 at 22:41.

  24. #24
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I didn't generalize nor was I being direspectful - but your wishes are so noted.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  25. #25
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    gregc, there is absolutely no need for you to get upset at Ikonified or Beetle. Ikonified has clearly stated that he is grateful for everyone's input, and he is perfectly entitled to make a free choice as to what advice to follow, or even to ignore it all if he wants to!

    Quote Originally Posted by gregc
    Well you're right Beetle, I was a bit out of line. But then again, it's somewhat frustrating when so many people have put time into something that didn't really get anywhere.
    Alas, it happens to us all from time to time (trust me, I've been there), but it comes with the territory - it's just one of those things. Deal with it.

    Quote Originally Posted by gregc
    And also please do not generalize this as if I act rudely all the time -- that is a generalization and is completely untrue. I help as many posters on this JS board as anybody, and I'd appreciate a little more respect.
    As Beetle has said above, no-one has disrespected you. Perhaps you're just having a bad day and venting your spleen, but there's no need for it, and it only makes YOU look bad to the readers/participants of this thread, and doesn't contribute to a happy community. I do hope that this thread does NOT discourage Ikonified and others from asking more questions in the future.

    Quote Originally Posted by gregc
    And just to let you know, there essentially isn't any difference whether you're running your website locally on your computer or over the web. Uploading your site won't change anything unless you've got a bunch of links that have C:\ in them and so forth...so you should get it to work now because nothing is going to magically fix itself by just uploading it to the web.
    I'm sorry to keep going on, but that is simply not true. There IS a difference - by using a webserver (either local or external) you gain a web root, which is vital for developing a maintainable site using root-relative linking. The only way that you can do this with pages spooled directly off your hard disk is to make the root of a hard drive the root of your site by dumping all the files there. This is not really to be recommended, but it might be enough to get Ikonified up and running in the very short term, and it will continue to work when he DOES get a web server to test on.
    MarcusJT
    - former ASP web developer / former SPF "ASP Guru"
    - *very* old blog with some useful ASP code

    - Please think, Google, and search these forums before posting!


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
  •