/    Sign up×
Community /Pin to ProfileBookmark

Does page exist?

Is it possible to check if a page actualy exists before calling it and possibly getting a ‘Page not found’ error?

Thanks.

Jason.

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@DokSep 28.2006 — I guees you could try and load the page in a frame/iframe and then check whether it indeed loaded. I would use server side scripting for this though...
Copy linkTweet thisAlerts:
@mrhooSep 28.2006 — If you are using http and are checking on your own domain you can use

something like this:
[CODE]function isFile(str){
var obj;
if (window.XMLHttpRequest){//IE7 and everybody else
obj= new XMLHttpRequest();
}
else if (window.ActiveXObject) {//IE6 maybe
try{
obj= new ActiveXObject('MSXML2.XMLHTTP.3.0');
}
catch(er){
obj=false;
}
}

var O= obj;
if(!O) return false;
try{
O.open("HEAD", str, false);
O.send(null);
return (O.status==200) ? true : false;
}
catch(er){
// error handling return Yikes(er);
}
}[/CODE]

If you are not opening a page using http; or it is on a different domain, you need to call a server script to test a file's existence.
Copy linkTweet thisAlerts:
@RetroFiestaauthorSep 28.2006 — I can't use serverside scripting as the webspace doesn't allow it.

I'll have a look into the ActiveX script.

Thanks.

Jason.
Copy linkTweet thisAlerts:
@Howard_KaikowJul 27.2007 — Can try ... catch be used to detect that a page does not exist, then have the catch do whatever?
×

Success!

Help @RetroFiesta spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...