Reputation: 468
I'm making a portable documentation with pure html and javascript that should be cross-browser compatible. I am placing the search/highlighter box and results page in separate frames. It all works fine in ie, ff and chrome (with the disable-web-security parameter) but throws error in Opera. I don't have a clue why is this and how could this be fixed.
Any help is appreciated.
The function from where Opera throws the exception:
function selectElementContents(el) {
if (window.getSelection && document.createRange) {
var sel = window.getSelection();
var range = document.createRange();
range.selectNodeContents(el);
sel.removeAllRanges();
sel.addRange(range);
} else if (document.selection && document.body.createTextRange) {
var textRange = document.body.createTextRange();
textRange.moveToElementText(el);
textRange.select();
}
}
Opera's error console:
Uncaught exception: DOMException: WRONG_DOCUMENT_ERR
Error thrown at line 56, column 8 in selectElementContents(el) in xy.js:
range.selectNodeContents(el);
Upvotes: 1
Views: 448
Reputation: 6229
You need to create the range in the document where you are going to use it. I think you've solved this already based on the jsfiddle demo, but for example the line
var range = document.createRange();
should be
var range = el.ownerDocument.createRange();
Upvotes: 3