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

?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

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

You make a request from the address and check the http code of the response.

Use the XMLHTTP object to make the request. You can find code for that
in most any AJAX library/article.
 
L

Laurent Bugnion [MVP]

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

You can check if a server is running. The following page shows how:

http://www.galasoft-lb.ch/myjavascript/IsUrlActive/

The function is:

function isUrlActive( strUrl )
{
var oHttp = null;
if ( window.XMLHttpRequest )
{
oHttp = new window.XMLHttpRequest();
}
else
{
if ( window.ActiveXObject )
{
oHttp = new window.ActiveXObject( "Microsoft.XMLHTTP" );
}
else
{
throw "UNSUPPORTED PLATFORM";
}
}
if ( !oHttp )
{
throw "ERROR";
}
oHttp.open( "HEAD", strUrl, true ); // true = async, false = sync

oHttp.onreadystatechange = function()
{
if ( oHttp.readyState == 4 )
{
if ( oHttp.status == 200 )
{
alert( "Server replied OK" );
}
else
{
alert( "There was a problem: " + oHttp.status );
}
}
}
oHttp.send( null );
}
 
C

Chandra

Hi,


You can check if a server is running. The following page shows how:

http://www.galasoft-lb.ch/myjavascript/IsUrlActive/

The function is:

function isUrlActive( strUrl )
{
var oHttp = null;
if ( window.XMLHttpRequest )
{
oHttp = new window.XMLHttpRequest();
}
else
{
if ( window.ActiveXObject )
{
oHttp = new window.ActiveXObject( "Microsoft.XMLHTTP" );
}
else
{
throw "UNSUPPORTED PLATFORM";
}
}
if ( !oHttp )
{
throw "ERROR";
}
oHttp.open( "HEAD", strUrl, true ); // true = async, false = sync

oHttp.onreadystatechange = function()
{
if ( oHttp.readyState == 4 )
{
if ( oHttp.status == 200 )
{
alert( "Server replied OK" );
}
else
{
alert( "There was a problem: " + oHttp.status );
}
}
}
oHttp.send( null );



}- Hide quoted text -

- Show quoted text -


Laurent, that always give me a permission denied error in IE 7 and no
error but no output too in firefox. Any permissions to be given?

Thanks,
Chandra
 
C

Chandra

Laurent, that always give me a permission denied error in IE 7 and no
error but no output too in firefox. Any permissions to be given?

Thanks,
Chandra- Hide quoted text -

- Show quoted text -
The function gives error 'access is denied when run locally, hence I
hosted the same on some webserver.
Here the event onreadystatechange is never executed. I tried both
sync and async open. Any inputs on how to resolve this will be useful.

<<CODE SNIPPET START>>
var urlObj = null;
if ( window.XMLHttpRequest )
{
urlObj = new window.XMLHttpRequest();
}
urlObj.open( "HEAD", strUrl, true ); // true = async,
false = sync
urlObj.onreadystatechange = function()
{
alert(urlObj.readyState);
if ( urlObj.readyState == 4 )
{
if ( urlObj.status ==
200 )
{

alert( "Server replied OK" );
}
else
{

alert( "There was a problem: " + urlObj.status );
}
}
}
<<CODE SNIPPET END>>


Thanks & Regards,
Chandra
 
A

Abdul Aleem

You can use the following Javascript Function to validate the Url/Links.

function validateUrl(url)
{
pattern = /http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/;
if( !pattern.test( url ) )
{
alert( 'Url Is Not Valid');
return false;
}
return true;
}
 
M

Mark Rae

You can use the following Javascript Function to validate the Url/Links.

function validateUrl(url)
{
pattern = /http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/;
if( !pattern.test( url ) )
{
alert( 'Url Is Not Valid');
return false;
}
return true;
}

All that will do is tell you whether a URL *looks* like a URL or not - the
code in Laurent's reply will actually try to get a valid HTTP response from
the URL to check if it is live or not...
 
L

Laurent Bugnion [MVP]

Hi,

I am very sorry, I forgot to add that this is only possible for URLs
located on the server of origin. Unfortunately, XmlHttpRequest restricts
calls and forbids connections to another server.

Unfortunately, there are no workarounds for now.

Greetings,
Laurent
 
L

Laurent Bugnion [MVP]

Hi,

Mark said:
You can use the following Javascript Function to validate the Url/Links.

function validateUrl(url)
{
pattern = /http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/;
if( !pattern.test( url ) )
{
alert( 'Url Is Not Valid');
return false;
}
return true;
}

All that will do is tell you whether a URL *looks* like a URL or not - the
code in Laurent's reply will actually try to get a valid HTTP response from
the URL to check if it is live or not...

And the code will fail if the server is not the server of origin... That
XmlHttpRequest restriction is really a PITA.

Greetings,
Laurent
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top