Jump to content

Verifying Links With Javascript - Noob Time :d


RecoJohnson
 Share

Recommended Posts

Can someone guide me in the right direction?I am trying to verify links and if they are real links they turn green, else they turn red.

<html><head><title>TRYING TO LEARN - NOT WORKING - FUUUUU</title><script type="text/javascript">function isUrl(s) {var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/return regexp.test(s); for(var i=0;i<document.getElementByTagName("a").length;i++){if (isUrl(document.getElementByTagName("a").href)){document.getElementByTagName("a").setAttribute("color","green");}else{document.getElementByTagName("a").setAttribute("color","red");}  }</script></head><body><a href="http://www.google.ca">GOOGLE</a><a href="http://www.9as0danotwaebsite.ca">GOOGLE</a></body></html>

Link to comment
Share on other sites

What do you mean by a "real link"? Your code seems to be checking whether the URL is well-formed or not (though I can't tell whether the regular expression is correct), but http://www.9as0danotwaebsite.ca is perfectly well-formed too (just that no one seems to have registered it yet). If you want to check whether there is a web server at the given domain, or that it returns 200 for the given URL, you will need to co-opt your server using AJAX.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...