Javascript client side page validation
April 3rd, 2009Recently I had a problem with “dead” links in a HTML document that was generated from a software solution we have developed at work. After a lot of research this is a smooth solution to the problem if there’s no server-side solution available (i.e. if you watch a local HTML file in a web browser).
It’s not really anything you should use professionally, but at least it’s a workaround…
This is the script:
<script type=”text/javascript”>
function Exist(url) {
var req = false;
// For Safari, Firefox, and other non-MS browsers
if (window.XMLHttpRequest) {
try {
req = new XMLHttpRequest();
} catch (e) {
req = false;
}
} else if (window.ActiveXObject) {
// For Internet Explorer on Windows
try {
req = new ActiveXObject(”Msxml2.XMLHTTP”);
} catch (e) {
try {
req = new ActiveXObject(”Microsoft.XMLHTTP”);
} catch (e) {
req = false;
}
}
}
var res = ”;
if (req) {
// Synchronous request, wait till we have it all
res = false;
try {
req.open(’GET’, url, false);
req.send(null);
} catch (e) {
res = false;
}
if (req.responseText!=”) { res = true; }
} else {
res = false;
}if (!res) { alert(’Page does not exist.’); }
return res;
}
</script>
Call it like this:
<a href=”testpage1.html” onclick=”return Exist(’testpage1.html’);”>



















