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

  • Thread starter Thread starter Chandra
  • Start date Start date
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.
 
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 );
}
 
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
 
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
 
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;
}
 
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...
 
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
 
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
 
Back
Top