C
Chandra
How do I programmatically (javascript) check if link is valid in html?
Chandra said:How do I programmatically (javascript) check if link is valid in html?
How do I programmatically (javascript) check if link is valid in html?
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 -
The function gives error 'access is denied when run locally, hence ILaurent, 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 -
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;
}
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...