Win a copy of Modern JavaScript for the Impatient this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

How to find whether URL is active or not using javascript?

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While user click on the image link in the html page, It leads the user to the landing URL. If that landing URL is not active at that time, then some other activity has to be done. How to write Java script code, to find whether that landing URL is active or not, before it displays the content of that landing URL page to user?
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It can only be done if it is the same domain. I talked about it a long time ago here: http://radio.javaranch.com/pascarello/2005/06/24/1119626686861.html

Eric
 
bhuvani krishna
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Eric,
My need is not the one which you gave.I want to find the broken links(URL is active or not) using java script.

I have only the URL of the page. Using that, how can i know whether that URL is active or not.

We got the solution for our problem. But it will work only for certain browsers. I am giving two files ajaxNavigation2.js and sample.html. sample.html will use this ajaxNavigation2.js to find the broken links.

ajaxNavigation2.js
----------------------

/* Ultimater's edited version of:
http://jibbering.com/2002/4/httprequest.html
to serve IE7 with XMLHttpRequest instead of ActiveX */

var xmlhttp=false;
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
try {
xmlhttp = new XMLHttpRequest();
} catch (e) {
xmlhttp=false;
}
}

/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE versions.
// and security blocked creation of the objects.
if (!xmlhttp){
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
}
@end @*/

if (!xmlhttp && window.createRequest) {
try {
xmlhttp = window.createRequest();
} catch (e) {
xmlhttp=false;
}
}

/* Ultimater's edited version of:
http://javascript.internet.com/ajax/ajax-navigation.html */

var please_wait = "Please wait...";

function open_url(url, targetId) {
if(!xmlhttp)return false;
var e=document.getElementById(targetId);if(!e)return false;
if(please_wait)e.innerHTML = please_wait;
xmlhttp.open("GET", url, true);
xmlhttp.onreadystatechange = function() { response(url, e); }
try{
xmlhttp.send(null);
}catch(l){
while(e.firstChild)e.removeChild(e.firstChild);//e.innerHTML="" the standard way
e.appendChild(document.createTextNode("request failed"));
}
}

function response(url, e) {
if(xmlhttp.readyState != 4)return;
var tmp= (xmlhttp.status == 200 || xmlhttp.status == 0) ? xmlhttp.responseText : "Ooops!! A broken link! Please contact the webmaster of this website ASAP and give him the following error code: " + xmlhttp.status+" "+xmlhttp.statusText;
var d=document.createElement("div");
d.innerHTML=tmp;
setTimeout(function(){
while(e.firstChild)e.removeChild(e.firstChild);//e.innerHTML="" the standard way
e.appendChild(d);
},10)
}
 
Eric Pascarello
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code I linked to constructs a URL and sees if it is there. It is what you needed with slight changes. (And I have to yell at my friend Ultimater for stealing my idea! Yes I know him...LOL)

Eric
 
bhuvani krishna
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by bhuvani krishna:
While user click on the image link in the html page, It leads the user to the landing URL. If that landing URL is not active at that time, then some other activity has to be done. How to write Java script code, to find whether that landing URL is active or not, before it displays the content of that landing URL page to user?

 
bhuvani krishna
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks,
I got it.
 
    Bookmark Topic Watch Topic
  • New Topic