Results 1 to 6 of 6

Thread: Replacing HTML in a page

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

    Post Replacing HTML in a page

    I would like to replace some code on the HTML page when it is loaded.

    Like this <div class="class-here-to-replace"> and when the code executes then it will like this <div class="my-class-here">. I would like to specify the exact class to replace. ie. class-here-to-replace.

    This is also embedded in an iFrame Tag. I do not know how to do it. Any tips will be fantastico, but the whole code will be fantastico x 12.5


    Thankyou

  2. #2
    Join Date
    Aug 2004
    Posts
    10,143
    Thanks
    3
    Thanked 1,008 Times in 993 Posts
    Blog Entries
    16

    Default

    you looking to physically modify the code when the page loads, or via JavaScript to merely manipulate the document's DOM dynamically? BTW this would seem to be a coding question versus "Looking for such a script...", so moving thread.

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

    Default

    In order of advisability:
    • Use a server-side script to perform the transformation on the fly.
    • Edit your page statically.
    • DOM version:
      Code:
      window.onload = function() {
        for(var i = 0, e = document.getElementsByTagName("div"); i < e.length; ++i)
          if(e[i].className === "class-here-to-replace")
            e[i].className = "my-class-here";
      };
    • innerHTML version:
      Code:
      window.onload = function() {
        document.body.innerHTML = document.body.innerHTML.toString().replace(/<div class="class-here-to-replace">/g, '<div class="my-class-here">');
      };
    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!

  4. #4
    Join Date
    Dec 2006
    Location
    Twin Cities, MN
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    or you can just change the class using the id.

    Place it right after the div you want to change, and it will change right when it's done loading.

    Code:
    <div class="someoldclass" id="myDiv">
      Lorem ipsum dolor sit amet.
    </div>
    
    <script>
      document.getElementById("myDiv").className = "mycoolnewclass";
    </script>
    or you could use it a a function in the header and then trigger it however you want.

    Code:
    <head>
      <title>whatever...</title>
      <script>
        function changeClass(myElementID,newClassName) {
          document.getElementById(myElementID).className = newClassName; 
        }
      </script>
    </head>
    <body onload="changeClass('myDiv','mycoolnewclass')">
      <div class="someoldclass" id="myDiv">
        Lorem ipsum dolor sit amet.
      </div>
    </body>

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

    Default

    or you can just change the class using the id.
    That won't replace all instances, as was requested. Other than that, the principle is that of the first script I posted.
    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!

  6. #6
    Join Date
    Dec 2006
    Location
    Twin Cities, MN
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Didn't see where he wanted every instance swapped. But yeah, Twey's solution should work fine for that.

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
  •