How can I test for XML using jQuery?
I have a bookmarklet that needs to know whether the current page is XML. How can I check for this?
I imagine I will pass in the document and check for the f开发者_如何学Collowing:
<?xml version="1.0" encoding="UTF-8"?>
But not sure.
Not sure if you want to load the page again, but you could run it through jQuery.parseXML()
, which will throw an exception for invalid XML, so you could just try to parse the page and if no exception is thrown then it is (valid) XML.
For example:
var xml = "<rss version='2.0'><brokenxml></rss>";
try {
xmlDoc = $.parseXML(xml);
} catch (err) {
// was not XML
}
精彩评论