Adding keyword links in the body of a page

Discussion in 'JavaScript' started by edr, Jan 21, 2007.

Thread Status:
Not open for further replies.
  1. #1
    My question is this:

    Does anybody know of a script or tool to automatically add links to the body of a page after page load? I am thinking of something similar to how IntelliTXT manages to add links.

    What I am looking to do is create in-page links to other areas of my site (not links going outbound to other sites) in order to have related content linked.

    Any help would be appreciated.
     
    edr, Jan 21, 2007 IP
  2. datropics

    datropics Peon

    Messages:
    309
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    0
    #2
    hmmm Edr - nice question - never really thought of that - if you can pull it off it'll be cool. Aight here's my stab at it:

    Page loads (onLoad or whatever)
    Call the function that gets the innerText of a div.
    Once you have gotten the innerText (or innerHTML for that matter - not sure - I'm writing this on the fly!!) - search for your desired string and once found replace it with your new string.

    Aight here goes -
    <body onLoad="setLinks();">
    <script language="javascript">
    function setLinks()
    {
    var bodyText = document.getElementById('interface').innerHTML;
    var textsArr = new Array('college','school', 'loan','opportunity','you get the picture');
    var ArrayLen = textsArr.length-1;
    for (var i=0;i<=ArrayLen;i++)
    {
    if ((bodyText.indexOf(" " + textsArr + " ") != -1) || (bodyText.indexOf(textsArr + " ") != -1))
    {
    bodyText = bodyText.replace(textsArr,"<a href='http://www.msn.com'>" + textsArr + "</a>");
    }
    }
    document.getElementById('interface').innerHTML = bodyText;
    }
    </script>
    <div id="interface">
    <p>school esse cillum dolore eu fugiat nulla pariatur. Sed do eiusmod tempor incididunt ullamco laboris nisi lorem ipsum dolor sit amet. Excepteur sint occaecat velit esse cillum dolore quis nostrud exercitation. Ut labore et dolore magna aliqua. Sunt in culpa mollit anim id est laborum.</p>
    <p>Qui officia loan excepteur sint opportunity eu fugiat nulla pariatur. Ut enim ad minim veniam, duis aute irure dolor quis nostrud exercitation. Eu fugiat nulla pariatur.</p>
    <p>Quis nostrud exercitation lorem ipsum dolor sit amet, ut labore et dolore magna aliqua. Velit esse cillum dolore duis aute irure dolor sed do eiusmod tempor incididunt. Excepteur sint occaecat mollit anim id est laborum. Sunt in culpa quis nostrud exercitation ut enim ad minim veniam. Cupidatat non proident, ullamco laboris nisi velit esse cillum dolore.</p>
    <p>Sunt in culpa eu fugiat nulla pariatur. Ut you get the picture, consectetur adipisicing elit, excepteur sint occaecat. Sunt in culpa ut labore et dolore magna aliqua. Cupidatat non proident, ut enim ad minim veniam, ullamco laboris nisi. Mollit anim id est laborum.</p>
    <p>In reprehenderit in voluptate ut aliquip ex ea commodo consequat. Quis nostrud exercitation qui officia deserunt sunt in culpa. Eu fugiat nulla pariatur. Ut enim ad minim veniam, ut labore et dolore magna aliqua.</p>
    </div>
    </body>
     
    datropics, Jan 21, 2007 IP
    edr likes this.
  3. ajsa52

    ajsa52 Well-Known Member

    Messages:
    3,426
    Likes Received:
    125
    Best Answers:
    0
    Trophy Points:
    160
    #3
    I think is better if you fill that links on your server, not on client.
    That way, robots will see that links also, and that benefit you on SERPs.
     
    ajsa52, Jan 22, 2007 IP
  4. datropics

    datropics Peon

    Messages:
    309
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    0
    #4
    Yup yup - I agree
     
    datropics, Jan 22, 2007 IP
  5. edr

    edr Guest

    Messages:
    784
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I completely see the benefit of that however adding something onto what exists is far more attractive than rewriting what exists to capture all or some of the inline link potentials.

    Another prospect is a "related links" added into the footer include which would contain a <div> which i can then use css for placement.

    That solution, however, does not give me inline links which is what I am after.
     
    edr, Jan 22, 2007 IP
  6. datropics

    datropics Peon

    Messages:
    309
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    0
    #6
    hmmm - maybe your solution is not javascript. In coldFusion you can do something like this

    <cfsavecontent variable="bodyContent">
    html body
    </cfsavecontent>

    You can then use the variable bodyContent and search for the keywords and change accordingly. Then when your are finished you display bodyContent <cfoutput>#bodyContent#</cfoutput> in your body HTML tag.

    I am sure PHP and asp both have a similar function as well.
     
    datropics, Jan 24, 2007 IP
Thread Status:
Not open for further replies.