how can i use this javascript code on a webpage, who is inside an iframe html tag?
window.highlight = function() {
var selection = window.getSelection().getRangeAt(0);
var selectedText = selection.extractContents();
var span = document.createElement("span");
span.style.backgroundColor = "yellow";
span.appendChild(selectedText);
span.onclick = function (ev) {
this.parentNode.insertBefore(document.createTextNode(this.innerHTML), this);
this.parentNode.removeChild(this);
}
selection.insertNode(span);
}
If the iframe is on the same domain as the parent, you can use window.parent.whatever instead of window.whatever, and parent.document.whatever instead of document.whatever.
If the iframe is NOT hosted on the same domain as the parent, then you cannot access it, as this is what is called cross-site-scripting (XSS).