1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Javascript word to link replacer?

Discussion in 'JavaScript' started by postcd, Aug 31, 2014.

  1. #1
    Hello,

    i have an website with many subpages, i want certain words to link to certain pages sitewide:

    SEO -> http:// mydomain.com/seo/
    Web Hosting -> http:// mydomain.com/webhosting
    Programming -> http:// mydomain.com/programming

    i mean i dont want to edit source code to add hyperlinks, but i want javascript to convert these words to links (case insensitive)

    Please can you share the script which will do this? I have ZERO knowledge in javascript

    So far i found these:

    #1 source: http://bavotasan.com/2010/jquery-replace-word-with-link/
    A) Add into header
    <scripttype='text/javascript'src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js'></script>
    Code (JavaScript):
    B) Add into footer
    <script type="text/javascript"> (function($) {   var thePage = $("body");   thePage.html(thePage.html().replace(/jQuery/ig, '<a href="http://jquery.com">jQuery</a>')); })(jQuery) </script>
    Code (JavaScript):
    but this is only for one word, and also requires jquery, which is quite large.. :(

    #2 source: http://stackoverflow.com/questions/1583303/search-for-words-replace-with-links/1583362#1583362
    var replacementDict ={'foo':'http://www.foo.com/','bar':'http://www.bar.net/'};var theRegex =/\b(foo|bar)\b/g;
    theText.replace(theRegex,function(s, theWord){return"<a href='"+ replacementDict[theWord]+"'>"+ theWord +"</a>";});
    Code (JavaScript):
    (this means i need to add this into my header.php somehow?

    Hope someone can come with a solution / advice on how to achieve that? Thank You
     
    postcd, Aug 31, 2014 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    For the second solution you just need to put the javascript inside a script-tag, like this:
    
    <script type="text/javascript">
    var replacementDict ={'foo':'http://www.foo.com/','bar':'http://www.bar.net/'};var theRegex =/\b(foo|bar)\b/g;
    theText.replace(theRegex,function(s, theWord){return"<a href='"+ replacementDict[theWord]+"'>"+ theWord +"</a>";});
    </script>
    
    Code (markup):
    you can just place this in the footer, if you want
     
    PoPSiCLe, Aug 31, 2014 IP
  3. postcd

    postcd Well-Known Member

    Messages:
    1,037
    Likes Received:
    9
    Best Answers:
    1
    Trophy Points:
    190
    #3
    I tried to add above code into my page between <body> and </body>, but words was not turned into links
     
    postcd, Sep 1, 2014 IP
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #4
    I didn't look at the actual code - I figured you actually copied a _working_ solution, not a rough draft. Did you even read the comment that followed the code on the stackoverflow-link? Read some more, and look at some of the other code-examples.
     
    PoPSiCLe, Sep 1, 2014 IP
  5. postcd

    postcd Well-Known Member

    Messages:
    1,037
    Likes Received:
    9
    Best Answers:
    1
    Trophy Points:
    190
    #5
    No working code found yet. Not solved
    please if you dont have working solution, do not post
     
    postcd, Sep 2, 2014 IP
  6. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #6
    I don't think you have quite read his replies properly. He is only trying to help you, maybe you shouldn't act like you're actually paying him to do or something. And yes, he is right. Did you just copy that code into your page? Because that isn't going to work unless that's what your actually trying to replace. Post your actual website link or some code.
     
    HuggyStudios, Sep 2, 2014 IP
  7. postcd

    postcd Well-Known Member

    Messages:
    1,037
    Likes Received:
    9
    Best Answers:
    1
    Trophy Points:
    190
    #7
    HuggyStudios: this topic become useless. i thought some will post solution. this topic is not about ME and solving MINE issue!!!

    This topic is about hundreds or thousands people reading it in future and saving their time. If you cant provide solution, than dont waste this topic space. thanks
    No solution yet
     
    postcd, Sep 2, 2014 IP
  8. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #8
    I think you misunderstand how forums work...
     
    PoPSiCLe, Sep 3, 2014 IP
  9. postcd

    postcd Well-Known Member

    Messages:
    1,037
    Likes Received:
    9
    Best Answers:
    1
    Trophy Points:
    190
    #9
    No solution found yet
     
    postcd, Sep 30, 2014 IP