Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: Changing Image paths on a page

  1. #1
    Join Date
    Nov 2006
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Changing Image paths on a page

    sup y'all.
    i'm trying to write a script that'll add to the paths of all the img tags on a page...
    in other words,
    HTML Code:
    <img src="images/someImage.gif" alt="" border="0" />
    would turn into
    HTML Code:
    <img src="../images/someImage.gif" alt="" border="0" />
    it's the "../" that needs to be added.
    here's what i have so far. works in FF but no dice in IE7:
    Code:
    function findImages(){  
      var img_tag = document.getElementsByTagName('img');  
      var every_image = img_tag[0];  
      var img_src = every_image.getAttribute("src");  
      for (var i=0;i<img_tag.length;i++) {  
           img_src = img_tag[i].getAttribute("src"); 
           img_tag[i].src = img_src.replace(/^images\/+/i, "../images/");
           }  
      }

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    How's this:
    Code:
    function prependToImages(s) {
      var base = document.createElement("img");
      base.src = "/";
      base = base.src;
      for(var i = 0, e = document.images; i < e.length; ++i)
        e[i].src = base + s + e[i].src.replace(base, "");
    }
    Untested.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Nov 2006
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Twey View Post
    How's this:
    Code:
    function prependToImages(s) {
      var base = document.createElement("img");
      base.src = "/";
      base = base.src;
      for(var i = 0, e = document.images; i < e.length; ++i)
        e[i].src = base + s + e[i].src.replace(base, "");
    }
    Untested.
    that creates image tags. i want to replace the src attribute.

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    No it doesn't. Read it again.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Nov 2006
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    it doesn't seem to work.

  6. #6
    Join Date
    Feb 2007
    Location
    England
    Posts
    254
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Cheap n Nasty

    EDIT: Sorry, wrong idea

    This should work. Just put in a HTML file and use away.

    Code:
    <textarea id="textToSort" cols=50 row=20>
    <img src="images/hellokitty.png">
    <img src="images/ohoh.png">
    </textarea>
    <input type="button" onClick="replaceSrcBeg()" value="Replace">
    
    <script>
    function replaceSrcBeg()
    	{
    	var text = document.getElementById("textToSort").innerHTML;
    	text = text.replace(/img\ssrc\=\"/gi,"img src=\"../")
    	document.getElementById("textToSort").innerHTML =text;
    	}
    </script>
    Last edited by Bob90; 03-20-2007 at 07:02 PM. Reason: wrong idea

  7. #7
    Join Date
    Feb 2007
    Location
    England
    Posts
    254
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default

    Does this not work?

    Code:
    for(var i = 0, e = document.images; i < e.length; ++i)
        e[i].src = "../"+e[i].src;

  8. #8
    Join Date
    Nov 2006
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    ok.
    i guess i didn't explain exactly what it is i'm trying to do...
    i want the js to go through the page and prepend each image tag with "../" or maybe even a "../../".
    this is a solution to the far-to-often problem in static sites with pages residing in different levels of the site that use the same include file. until now, i've just used a different include file for pages that are one or two levels deep, but i thought that a simpler solution would be to have those pages use a script that simply acts as a "base url" for just images. i would use the base url tag but it can't be relative.

  9. #9
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>Source Changer</title>
    <script type="text/javascript">
    function load() {
    var img = document.getElementsByTagName("img");
    for (var i=0;i<img.length;i++) {
    	img[i].setAttribute("src","../"+img[i].getAttribute("src"));
    	}
    document.onclick=function(){alert(img[0].getAttribute("src"))}
    }
    window.onload=load
    </script>
    </head>
    <body>
    <img src="images/foo.png">
    </body>
    </html>
    The above in red is just a test to show the first image's new src. You can remove it.
    - Mike

  10. #10
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    this is a solution to the far-to-often problem in static sites with pages residing in different levels of the site that use the same include file. until now, i've just used a different include file for pages that are one or two levels deep, but i thought that a simpler solution would be to have those pages use a script that simply acts as a "base url" for just images. i would use the base url tag but it can't be relative.
    Static sites cannot include files, unless you're talking about something like using XMLHttpRequest to retreive a page -- this is horribly ugly.
    Modify the paths with whatever server-side language you're using to include the files.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

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
  •