Solved

Check link's validity with JavaScript

Posted on 2008-06-23
2
505 Views
Last Modified: 2008-06-23
<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.
0
Comment
Question by:hankknight
2 Comments
 
LVL 14

Accepted Solution

by:
agriesser earned 500 total points
Comment Utility
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

0
 
LVL 16

Author Comment

by:hankknight
Comment Utility
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:
http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_23508640.html
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now