How do I programmatically (javascript) check if link is valid in html?

T

Toby A Inkster

Chandra said:
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!
 
N

nice.guy.nige

While the city slept, Toby A Inkster ([email protected])
feverishly typed...
[...]

Ah yes, the good old difference between "Validation" and "Verification"...
Something I learnt about 20 years ago :)-S)... I threw that question ("what
is the difference between data validation and data verification?") at one of
my previous developers - he was a recent IT graduate... He had to think for
quite some time before he answered (correctly)... saying that, as it was 20
years since I learnt it, so did I ;-)

Cheers,
Nige
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top