I am using DOMParser
to parse a document that contains relative URLs for things like the action property of a form. Because the baseURI of the document created by DOMParser
is null
accessing the action property yields a blank string. I can get around this by using getAttribute
but if it is possible to specify a baseURI when using DOMParser
that would be ideal.
Rather than injecting a <base>
in the HTML before parsing, have you considered doing something similar after parsing?
function parse(baseUri, htmlStr) {
var doc = (new DOMParser).parseFromString(htmlStr, 'text/html');
var base = doc.createElement('base');
base.href = baseUri;
doc.head.appendChild(base);
return doc;
}
var parsedDoc = parse('http://example.com', '<form action="/index.html"></form>');
console.log(
parsedDoc.querySelector('form').action
)
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