javascriptxpathbookmarkletdocument.evaluate

IE JavaScript error : document.evaluate illegal # character


this script is developed to support only IE

I have this:

var result = document.evaluate(xpath, document, null, XPathResult.STRING_TYPE, null);

with a known xpath;

the xpath is:

id("productTitle")

why do i get this error message:

Error ! This name may not contain the '#' character:

-->#<--comment

a working example, simple one : http://jsfiddle.net/gWpcU/6/

the example that crashes : http://jsfiddle.net/YkwTM/


Solution

  • The reason you get the error message in the example document is because xPath is trying to reread the document to do its own thing on it, and whenever it sees a "<!", the script parser gives an error. Going through and removing all cases of "<!" (namely, the <!DOCTYPE html>, and all the <!-- comments), makes the page work without errors. I would paste it here, but the answer becomes too long: so you can do that yourself.

    Conversely, pasting the <!DOCTYPE html> into your working example also makes it get that error.