Does URL exist

check JavaScript File exists

Page 1 of 1

3 Replies - 13579 Views - Last Post: 06 February 2009 - 03:50 PM

#1 JohnWaters  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 138
  • Joined: 28-November 08

Does URL exist

Posted 06 February 2009 - 06:43 AM

Using Javascript I need to figure out how to check if a URL actually exists.

The problem is that the Javascript file may not always be there so I have I have to check every time I need it.

AJAX is not an option AND its has to cross browser compatible.

*NOTE The URL will always be pointing to a Javascript File

So far I have this

function is_URL_Available(url)
	{
		var script = document.createElement("script");
		script.src = url;
		script.type = "text/javascript";
		var head = document.getElementsByTagName("head")[0];
		head.appendChild(script);
		if (self.getstatus) {
			head.removeChild(script);
			return  true;
		  } else {
			head.removeChild(script);
			return false;
		  }
	}
		



The Javascript file will always contain var getstatus ="";
But adding the script tag doesn't stop the processing so getstatus may not have been declared yet.

I hope this is not confusing. All help is greatly appreciated :)

This post has been edited by JohnWaters: 06 February 2009 - 06:44 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Does URL exist

#2 teampoop  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 140
  • Joined: 19-June 08

Re: Does URL exist

Posted 06 February 2009 - 09:51 AM

Why is AJAX not an option? If you're using javascript, you have access to the xmlHttpRequest object which will allow you to check if the file is available. Create a new xmlhttprequest object and send for the file in question. If the status change returns a 200 (http OK) you're good to go. Else, file doesn't exist.
Was This Post Helpful? 0
  • +
  • -

#3 SoLi  Icon User is offline

  • andydust.com

Reputation: 41
  • View blog
  • Posts: 1,438
  • Joined: 27-January 02

Re: Does URL exist

Posted 06 February 2009 - 10:19 AM

Ajax would be tricky if the javascript is hosted on an external domain (until cross domain ajax requests are more widely supported).

If you wanted to continue using your current function, you can add an onload event listener to your script variable.

I.e.
script.onload = scriptLoaded;
function scriptLoaded() {
if (self.getstatus) {
			head.removeChild(script);
			return  true;
		  } else {
			head.removeChild(script);
			return false;
		  }
}


This post has been edited by SoLi: 06 February 2009 - 10:19 AM

Was This Post Helpful? 0
  • +
  • -

#4 JohnWaters  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 138
  • Joined: 28-November 08

Re: Does URL exist

Posted 06 February 2009 - 03:50 PM

Thanks SoLi. Yes cross domain was an issue.

Unfortunetly this didnt work either. When i added a small timeout to allow the script to load this seemed to work.

I've never used script.onload so I'm not sure how reliable it is.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1