javascript - How can I read & write data to a new tab in Firefox? -


in past used work:

// writing var newtab = window.open(); newtab.document.write("<html><head><title>i'm tab</title></head>"); newtab.document.write("<body id='hello'>with text on it</body>"); newtab.document.write("</html>"); newtab.document.close();  // reading wrote newtab.document.getelementbyid('hello').addeventlistener("click", custom_search_function(), false); 

however when try execute code, firefox mentions security error:

error: securityerror: operation insecure.

i searched forum alternative , works:

var textonpage = "<html><head><title>i'm tab</title></head><body>"; var newtab = window.open("data:text/html;charset=utf-8," + encodeuricomponent(textonpage)); newtab.document.close(); 

but can't access page via getelementbyid

newtab.document.getelementbyid('hello').addeventlistener("click", custom_search_function(), false); 

returns:

error: typeerror: newtab.document.getelementbyid(...) null

how can write new tab , go read through functions such getelementbyid?

you're falling foul of single origin policy. when open new window without url, by definition can't have same domain name original (opener) window.

you instead have window.open() call open url on site (mostly blank html) , part of body.onload event handler (or jquery.ready()) set event handler message event this:

$(document).ready(function(){    window.addeventlistener("message", receivemessage, false); });  function receivemessage(evt) {   if (evt.origin !== "https://your-domain.here")     return;    // evt.data   $(document.body).append(""+evt.data); } 

in originating window call:

otherwindow.postmessage(message, "https://your-domain.here"); 

the postmessage api supported across variety of modern browsers.

you'll still not able directly reach in maniuplate content of otherwindow, can post messages otherwindow originating window achieve same effect. (e.g: put content manipulation code in otherwindow's content , 'call' originating window).


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

.htaccess - Matching full URL in RewriteCond -