Results 1 to 8 of 8

Thread: Replacing words with HTML

  1. #1
    Join Date
    Feb 2009
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Replacing words with HTML

    Hello, all.

    I'm in need of a script that's able to replace occurrences of specific words in the document's body with some HTML. It's easy to do that with "str.replace", but that method relies on the script also generating the text, which will definitely not work for my project. I've seen a lot of scripts that replace words with different words, but none that supports HTML.

    Just to be clear, an example: "There are a lot of houses in that area."
    Intended result: "There are a lot of <img src="house.png"> in that area."

    Lame example, but that's the idea.

    Hope somebody can help me out on this one... thank you for the attention.

  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    You can't do it in HTML... You can do it in PHP though:
    http://www.dynamicdrive.com/forums/s...91&postcount=2
    Jeremy | jfein.net

  3. #3
    Join Date
    Feb 2009
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    My problem is that this project is completely client-side. Are there really no alternatives? Combining some manner of find & replace code with "document.write" should do it, I think... I just don't know how to implement that.
    Last edited by MarkDarkness; 03-02-2009 at 02:32 AM.

  4. #4
    Join Date
    Sep 2008
    Posts
    119
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default

    when in doubt, use dynamic posting/ajax.
    document.write is document.wrong

  5. #5
    Join Date
    Feb 2009
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Well, as I was saying, this project is client-side... I don't have a server to interact with. The whole thing is supposed to run from a pendrive. With creativity I've circumvented a lot of limitations, but this one's a tough cookie.

    Anyway, that's why AJAX is out of question, unfortunately.

  6. #6
    Join Date
    Mar 2007
    Location
    Currently: New York/Philadelphia
    Posts
    2,735
    Thanks
    3
    Thanked 519 Times in 507 Posts

    Default

    This is an article by Patrick Haney about ampersands and typography. In it, he discusses a way to wrap ampersands in <span> tags to be able to style them differently. You can use the same principle (and practically the same code) to do what you're looking for.

  7. The Following User Says Thank You to Medyman For This Useful Post:

    MarkDarkness (03-02-2009)

  8. #7
    Join Date
    Feb 2009
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    That was one fine suggestion, Medyman! Works like a charm. Thank you very much for the help, it's very appreciated.

    One last question, though... to those of you more familiar with jQuery... is there any way I could turn the code below into some some sort of array? So that I don't have to repeat it over and over again to add different replacements?

    Code:
    $(document).ready(function() {
        $("*:contains('WORD GOES HERE')", document.body)
            .contents()
            .each(
                function() {
                    if( this.nodeType == 3 ) {
                        $(this)
                            .replaceWith( this
                                .nodeValue
                                .replace( /WORD GOES HERE/g, "<img src='images/layout/house.png' alt='house icon'>" )
                            );
                    }
                }
            );
    });

  9. #8
    Join Date
    Sep 2008
    Posts
    119
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default

    I am curious if the

    document.getElementById('fielder').value.replace(new RegExp(/\,/g),"-");

    regex function would insert html.....

    probably not, but that would be so much more convenient.
    document.write is document.wrong

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
  •