SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Jan 2008
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cool Change a string of characters with aonether in a HTML file

    Hi all of you,

    Problem:
    ~300 simple HTML files that have an common image somewhere in the body of the page, without span tags or id.
    In all those files a have a <script src=".../ascript.js></script> in the body tag.
    I can't change nothing else then the ascript.js file and I need to change the src of that image from all those 300 pages from the .js

    I foud some information about a script that can find a specific string in the <body></body> of the page and change it automatically with the desired one, when the page loads, without needing IDs and Spans, but I didn't found the script itself.

    I'll give you an example of how my html file looks:

    <html>
    <head>
    </head>
    <body>
    <img src="files/asd.jpg">
    <script src="scr/ascript.js></script>
    </body>
    </html>

    So I can't change nothing in that page, just the ascript.js can be modified.

    Any solutions?

  2. #2
    SitePoint Member
    Join Date
    Jan 2008
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    P.S.

    I would want to change the entire "<img src=files/asd.jpg>" with something new

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    What are you wanting to change it to?
    If it's just another image then you could do something like the following

    Code Javascript:
    var images, i;
    images = document.getElementsByTagName('img');
    for (i = 0; i < images.length; i++) {
        if (images[i].getAttribute('src') === 'files/asd.jpg') {
            images[i].setAttribute('src', 'files/newname.jpg');
        }
    }

    That could be improved if I knew more about how the asd.jpg file is located on the page. If it's somewhere inside a an element with a id for example, but the above code is a good starting point.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  4. #4
    SitePoint Member
    Join Date
    Jan 2008
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    And if I would want to change all that img tag with <a href="whatever.htm">NewThing</a> ?

    How can I do that?

  5. #5
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    You can do that with a variation of the above code.

    Code Javascript:
    var images, anchor, text, i, parent;
    // get image
    images = document.getElementsByTagName('img');
    // create anchor
    anchor = document.createElement('a');
    anchor.setAttribute('href', 'whatever.htm');
    text = document.createTextNode('NewThing');
    anchor.appendChild(text);
    // replace with anchor instead of image
    for (i = 0; i < images.length; i++) {
        if (images[i].getAttribute('src') === 'files/asd.jpg') {
            parent = images[i].parentNode;
            parent.replaceChild(anchor, images[i]);
        }
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •