5

I have problem: I need to replace some word on page with needed html. For example:

t = $('body').html();
t = t.replace(/Myword/g, '<div></div>');
$('body').html(t);

It is working successfully. But... If body contains javascript tag - JS running again.

If I use document.body - it working

t = document.body.innerHTML;
t = t.replace(/Myword/g, '<div></div>');
document.body.innerHTML = t;

But I am interested in jQuery solution. Maybe you have it?

Thanks in advance

4
  • 5
    instead of modifying the entire page html you need to target only those specific elements with the given text Commented Jan 4, 2014 at 8:06
  • @ArunPJohny Can you provide that with an example? Commented Jan 4, 2014 at 8:10
  • please share your html sample and where the text can appear Commented Jan 4, 2014 at 8:21
  • @ArunPJohny It can be plain text. Something like '<div><form>...</form>...Many many text...<div>...</div>Myword</div> Commented Jan 9, 2014 at 15:39

4 Answers 4

3

A simple solution would be to ignore all the script tags.

t = $('body').clone().find("script").remove().end().html();
t = t.replace(/Myword/g, '<div></div>');
$('body').html(t);

Demo

Sign up to request clarification or add additional context in comments.

1 Comment

I want use my script on 3rd party pages. I think script deleting is not good idea.
1

This solution might help you.

JavaScript not executed if:

  1. JavaScript inside the <script type=”text/xml”> doesn’t execute
  2. JavaScript inside the <script type=”text/xml” type=”text/javascript”> doesn’t execute as well because only the first type is considered

so change type:

function preventJS(html) {
     return html.replace(/<script(?=(\s|>))/i, '<script type="text/xml" ');
}

and use it:

t = $('body').html();
t = t.replace(/Myword/g, '<div></div>');
$('body').html(preventJS(t));

2 Comments

I think you are trying to put <tag> in your text and it is getting interpreted and hidden from view, so your first bullet points aren't clear. try putting back ticks around the tags.
Hm. I need to check it. Thank you very much
1

As Arun mentioned in the comments, you should target the elements more specifically. It sounds like <script> tags will be re-evaluated if you use JQuery to get the entire <body> content and then re-insert it. Try wrapping the desired content in a <div>:

<body>
    <div id="wrapper">
        <p>Text that will be altered</p>
    </div>
    <script type="text/javascript">
        //javascript code
    </script>
</body>

Then your code that will replace text can be:

t = $('#wrapper').html();
t = t.replace(/Myword/g, '<div></div>');
$('#wrapper').html(t);

2 Comments

You might want to rephrase that to make it clear that capturing the entire content of <body> is a requirement.
This SO question is asking the same thing, and they are discussing some other options there. Good luck!
1

A better way would be to just replace "Myword" in the most specific element possible. This will only replace Myword in the textNodes which contain it and nowhere else.

var each = Array.prototype.forEach;

var $myword = $(":contains('Myword')");
$myword.each(function() {
    each.call(this.childNodes, function(ele) {
        if(ele.nodeType === 3 && ele.nodeValue.indexOf('Myword') >= 0) {//if ele is a text node and contains myword
            ele.nodeValue = ele.nodeValue.replace(/Myword/g, "some text");
        } 
    }, this);
});

Try running it on this page

2 Comments

But if I have two and more divs with "Myword" - it replace only one (last) occurrence
Yes, I mentioned that - you could do a filter to correct that if you wanted

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.