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 following:
<?xml version="1.0" encoding="UTF-8"?>
But not sure.
No, JQuery HTML doesn't work with XML document. It only works for HTML documents.
jQuery. parseXML uses the native parsing function of the browser to create a valid XML Document. This document can then be passed to jQuery to create a typical jQuery object that can be traversed and manipulated.
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With