Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8872589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:15:19+00:00 2026-06-14T18:15:19+00:00

What I need to do I display an iframe with javascript in the body

  • 0

What I need to do

I display an iframe with javascript in the body of an HTML page.
With something like that document.write('<iframe ...></iframe'>);

In this iframe there is my javascript function witch search a keyword in the body of the parent document, and replace it with an html link <a href="#">keyword</a> in the parent document.

What I’ve tried

  • Javascript Bookmarklet to replace text with a link : complex script, but I need the skipTags
  • and Javascript .replace command replace page text? : very short and nice script, but there is not the skipTags function…

Those worked like a charm when the script is in the document but not in an iframe to work with the parent document.

My problems/questions

  1. The problem is that the ‘keyword’ is replaced with a
    ‘non-interpreted’ html as text. (Browser displays <a
    href="#">keyword</a>
    ).
  2. My second question is how to do the replace just once, and not for
    all the matching expressions ?

Usualy I use some jQuery but in this project I need to use only some javascript without any library.

Any idea to help me ? (I don’t want anyone to “write my code”, I just want some advices to make it by myself)

P.S. 1 : I use Chrome, but I would like to make it work in every browser.

P.S. 2 : English is not my first language, so if you don’t understand something, don’t hesitate to ask it to me, I’ll try to explain it better.

Edit 2

First script now works for the HTML, so question 1 is solved, but how to do the replace only once, even if the keyword is repeated several times ? (question 2)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T18:15:20+00:00Added an answer on June 14, 2026 at 6:15 pm

    With the help of xiaoyi, I’ve found some solutions :

    • Stop the loop and replace only the first match
    • Globalize the functions to search/replace multiple keywords

    I think that it could be optimized, but for me it works like a charm, and I share it with you, if it can help anyone (don’t forget to change the target of the document, here “parent”) :

    (function(){
        // don't replace text within these tags
        var skipTags = { 'a': 1, 'style': 1, 'script': 1, 'iframe': 1, 'meta':1, 'title':1, 'img':1, 'h':1 };
        // find text nodes to apply replFn to
        function findKW( el, term, replFn ) 
        {
            var child, tag,found=false;
            for (var i = 0;i<=el.childNodes.length - 1 && !found; i++)
            {
                child = el.childNodes[i];
                if (child.nodeType == 1) 
                { // ELEMENT_NODE
                    tag = child.nodeName.toLowerCase();
                    if (!(tag in skipTags)) 
                    {
                        findKW(child, term, replFn);
                    }
                } 
                else if (child.nodeType == 3) 
                { // TEXT_NODE
                    found=replaceKW(child, term, replFn); // if found=true, we stop the loop
                }
            }
         }; 
        // replace terms in text according to replFn
        function replaceKW( text, term, replFn) 
        {
            var match,
                matches = [],found=false;
            while (match = term.exec(text.data)) 
            {
                matches.push(match);
            }
            for (var i = 0;i<=matches.length - 1 && !found; i++)
            {           
                match = matches[i];         
                // cut out the text node to replace
                text.splitText(match.index);
                text.nextSibling.splitText(match[1].length);
                text.parentNode.replaceChild(replFn(match[1]), text.nextSibling);
                if(matches[i])found=true;// To stop the loop            
            }
            return found;
        };
        // First search/replace
        var replTerm = 'keyword';
        findKW(
            parent.document.body, 
            new RegExp('\\b(' + replTerm + ')\\b', 'gi'),
            function (match) 
            {
              var link = parent.document.createElement('a');
              link.href = 'http://www.okisurf.com/#q=' + replTerm;
              link.target = '_blank';
              link.innerHTML = match;
              return link;
            }
        );
        // A second search/replace
        var replTerm = 'word';
        findKW(
            parent.document.body, 
            new RegExp('\\b(' + replTerm + ')\\b', 'gi'),
            function (match) 
            {
              var link = parent.document.createElement('a');
              link.href = 'http://www.okisurf.com/#q=' + replTerm;
              link.target = '_blank';
              link.innerHTML = match;
              return link;
            }
        );
        // Other search/replace
        // ...
    }());
    

    I’ve also discovered that the second solution doesn’t works with Internet Explorer witch doesn’t accept the createTreeWalker() DOM function

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an HTML page containing a flash file, I need to write code
I need to display short description like label and near it data associated with
I need to display a link if the current page is being viewed in
I need to display another external site's content into my site. Normally <iFrame> tag
There is a website ( S ), that contains an iframe ( A ),
I'm using Javascript to load a PDF in an iframe on my web page.
I need to display another website in an iframe. I don't need to access
I need to display on my Web page a simple text string returned from
Could you please give a html code for the following? I need to display
Ive got a javascript calculating the height of a iframe element and the document

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.