SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2006
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to replace text with link?

    I want to replace any keyword which displays on html page with keyword with link. I want to do this by javascript. This keyword can be already linked, if this keyword is already linked then I don't want to link this keyword with new link tag but if it is not linked then link with some url.

    This keyword will also not linked when it is an alt text of an image or used in script or in field of the form or somewhere else which is not displayed on page.

    I have to do this using javascript. Can somebody suggest me how to do this?
    Submit your seo tool or resource at seo web directory

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I think you have to be a bit more specific than this. Otherwise you will end up with links absolutely everywhere, which will drive your visitors away. They will also end up in undesirable places like H1 tags, or perhaps navigation areas and whatnot. I think you need to decide to limit them to a certain area, e.g. a "main" or "content" block somewhere in the page. Then you can start looking for those keywords.

    In order to do that you will need to look for text nodes and put them in a collection (an array). Then you loop through them, adding links where appropriate. If the parentNode is an anchor (A) then you just move on without adding a link.

    The tricky bit is going to be getting the text nodes into a collection. XPath would be easiest but unfortunately IE's support for it is inadequate, since it uses an ActiveX object. You would therefore most likely need to loop through all the child nodes of the base element you start with and check the contents of each text node against your keywords.

    Using innerHTML would make this process a lot simpler, but then there's the issue of keywords in alt tags, class names, etc, so I think using the DOM is less prone to error.

  3. #3
    SitePoint Enthusiast
    Join Date
    Jan 2006
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I can use only body tag for getting innerHTML. can you tell me how can we get that parentNode is an anchor (A) so we can skip this step. And how can I get all occurrences of the keyword?
    Submit your seo tool or resource at seo web directory

  4. #4
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Why only the BODY? It would be very problematic to play with the entire innerHTML of the BODY tag.

    If you find something with innerHTML you can't check if it has a parentNode except by complex regular expressions. It would be a "dirty" way of doing things.


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
  •