• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 520
  • Last Modified:

Check link's validity with JavaScript

<a href="http://www.example.com" id="L523136">Link to Example</a>

Using JavaScript at the bottom of my page how can I test if the link is to http://www.example.com and the link text is "Link to Example" ?

I want False to be returned if either the link or the link text has been changed.
1 Solution
Below is a function "CheckLink" that should work. Please note the '/' at the end of www.example.com. Firefox at least adds a / to all domains in the href attribute of links, don't know for other browsers.
function CheckLink()
  if((document.links['L523136'].href == "http://www.example.com/") &&
     (document.links['L523136'].text == "Link to Example"))
    return true;
  return false;

Open in new window

hankknightAuthor Commented:
Thank you, agriesser.

Your example works on its own but when I tried to integrate it into my app it does not work in IE.

I posted a question about that here:

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now