Unresolved Mystery
I have encountered an issue with how Chrome (version 47.0.2526.73) handles xml files. Despite working perfectly in Firefox (version 43.0.4), the code fails to function as expected in Chrome. As a result, I am left wondering why this discrepancy exists and how I can resolve it for Chrome.
The Goal:
My objective is to create a JavaScript bookmarklet that can check sitemap xml links for various HTTP error statuses such as 404s or 500s.
The Problematic Code Snippet:
var siteMap="http://www.example.com/sitemap.xml";
var httpPoke = function(url,callback){
var x;
x = new XMLHttpRequest();
x.open('HEAD', url);
x.onreadystatechange = function() {
if (this.readyState == this.DONE) {
callback(this.status);
}
}
x.send();
};
var response=httpPoke(siteMap,function(n){
console.log(n);
});
When executing this code on any page within the domain, the response is:
200
However, upon navigating to the actual sitemap at http://www.example.com/sitemap.xml, the same code produces the following error:
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.
This poses a challenge to my plan of providing a bookmarklet that can be used directly on the sitemap itself.
How to Reproduce the Issue:
1) Locate an XML file from a website by searching "filetype:xml sitemap" and choose one that leads to a direct XML file.
2) Implement the provided code snippet either as a bookmarklet or in your browser's developer console.
3) Ensure that the variable siteMap is set to the current URL to comply with CORS. You may even use siteMap=location.href;
You will observe that while this setup works in Firefox, it encounters issues in Chrome.
Note:
Executing code FROM an HTML page, targeting an HTML page does work.
Executing code FROM an HTML page, targeting an XML page does work.
Executing code FROM an XML page, targeting an HTML page does not work.
Executing code FROM an XML page, targeting an XML page does not work.
Steps Taken So Far:
In researching this issue, most resources focus on:
- Cross domain requests
- Having the source or target on localhost, file:///, or other local servers.
My situation does not fall under either of these categories.