/    Sign up×
Community /Pin to ProfileBookmark

XMLHttpRequest() off a localfile

Im trying to run some code which is designed to run locally.
So far it runs fine in Opera / Firefox but it doesnt work in Chrome.

The problem appears to be the XMLHttpRequest() – in Chrome it wont work off a local file but does if I upload my file to a server.

Heres the section:

var file = “file1.xml”;
try //Internet Explorer
{
xmlDoc=new ActiveXObject(“Microsoft.XMLDOM”);
xmlDoc.async=false;
xmlDoc.load(file);
}
catch(e)
{
try //Firefox, Mozilla, Opera, etc.
{
xmlDoc=document.implementation.createDocument(“”,””,null);
xmlDoc.async=false;
xmlDoc.load(file);
}
catch(e)
{
try //Google Chrome
{
var xmlhttp = new window.XMLHttpRequest();
xmlhttp.open(“GET”,file,false);
xmlhttp.send(null);
xmlDoc = xmlhttp.responseXML.documentElement;
}
catch(e)
{
error=e.message;
}
}
}

Is there anyway to get google Chrome to read the file locally ?

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@axemanauthorJul 27.2010 — Found the problem.

It's a feature of Google chrome.. i.e. it will not read XML files off a local hard drive.
Copy linkTweet thisAlerts:
@justinbarneskinJul 28.2010 — If its local use only, would you consider an IE only solution?
Copy linkTweet thisAlerts:
@axemanauthorJul 28.2010 — I wrote a small script and distributed it. I wanted as many people as possible to use it, for legal reasons it can not be distributed online on a website, so made an offline version.

Looking at my stats I discovered Google chrome users were only using it for 10 seconds and I started wondering why.

After a little investigation I discovered chrome wont allow XML file reading of local files i.e. XMLHttpRequest() or anything else for that matter. It doesn't even tell you.

I now recommend people not to use Chrome.
Copy linkTweet thisAlerts:
@Sterling_IsfineJul 28.2010 — I wrote a small script and distributed it. I wanted as many people as possible to use it, for legal reasons it can not be distributed online on a website, so made an offline version.

Looking at my stats I discovered Google chrome users were only using it for 10 seconds and I started wondering why.

After a little investigation I discovered chrome wont allow XML file reading of local files i.e. XMLHttpRequest() or anything else for that matter. It doesn't even tell you.

I now recommend people not to use Chrome.[/QUOTE]
It will read from a local machine using [FONT="Courier New"]http[/FONT] protocol but not[FONT="Courier New"] file[/FONT] protocol. It may be a security measure; On I.E. 7, using [FONT="Courier New"]file[/FONT] protocol, requests don't even respond.
Copy linkTweet thisAlerts:
@axemanauthorJul 28.2010 — How do you get it to use the http protocol ? I cant see any options for it.
Copy linkTweet thisAlerts:
@Sterling_IsfineJul 28.2010 — How do you get it to use the http protocol ? I cant see any options for it.[/QUOTE]You install a local server like Apache, which may not be too practical for your users.

For Chrome you can warn your users by detecting when the [I]responseText[/I] property is null.
×

Success!

Help @axeman 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 5.4,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

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