|
Posted by Toby A Inkster on 03/07/07 10:33
Chandra wrote:
> How do I programmatically (javascript) check if link is valid in html?
What do you mean by valid?
http://efgsdgf.rertywe.iyugiugiukg/kighfgfjhg/sdfgsdfgjhb.erfged
is a valid URL, in that it conforms with all the syntactic rules that URLs
are required to conform to; but there's no web page there; indeed the
entire domain name does not exist, and in unlikely to exist at any time in
the foreseeable future, as it's in a currently non-existant top level
domain.
If you merely want to check that a link conforms to the syntax rules for
URLs (like the one above) then you can use regular expressions. Google for
"javascript regular expressions" and "regular expressions match url" and
you should be able to learn what you need.
If you actually want to check that a page exists at the given URL, and
isn't, say, a 404 page, then you'll need to look into AJAX. Because of the
Javascript security model, your Javascript can't directly check URLs on
other domains, so you'll need to write a server side script that takes a
URL, performs an HTTP HEAD request on that URL and checks for a "200 OK"
response code. Your client-side Javascript would then query that
server-side script via AJAX. There may be a significant delay in checking
the URL if either your server or the server hosting the supplied URL is too
slow.
--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux
* = I'm getting there!
Navigation:
[Reply to this message]
|