I have an html document loaded in an iframe.
I developed javascript popup menu code for this document, and I am inserting code in an iframe from the main document.
But my code does not work in an iframe because it uses a " document " object, and this surprisingly applies to the main document, not the iframe document.
I also play around content selection, and I need window.getSelection() , which returns to the main documents, and I need window.frames[0].getSelection() .
Am I doing it wrong?
Is there a simple solution to overcome this?
UPDATE: (lightening)
As Bergi pointed out, my question is how to run the javascript code I entered correctly to get the local region, not the global region in the iframe?
Therefore, I do not need to rewrite document and window in the code ...
UPDATE: (skeleton of my html)
<html> <head></head> <body> <iframe> [ <body> ... <script></script> </body> ] </iframe> </body> </html>
The script looks like this (using jquery):
$(function(){ $('iframe').load(function(){ var $doc = $('iframe').contents(); var $head = $('head', $doc); $head.append('<link rel="stylesheet" href="/stylesheets/book-popup-menu.css" />'); var $body = $('body', $doc); $body.append(' \ <div id="popup"> \ </div> \ '); $body.append(' \ <script type="text/javascript"> \ console.log(document.body);// it still displays the global body \ </script> \ '); }); });
UPDATE : Violin showing a problem
source share