javascript - App background script, accessing elements of opened window -
i working on chrome app, , need background script access elements of html window creates.
please not answer can other scripts launched created window, because not need.
this background.js file:
chrome.app.runtime.onlaunched.addlistener(function() { chrome.app.window.create('window.html', {'outerbounds': {'width': 600, 'height': 500 }}, function(mywin) { mywin.contentwindow.document.getelementbyid('areaid').value='new text!'; }); });
and windows.html:
<!doctype html> <html> <head> </head> <body> <textarea id="areaid" name="areaid" rows="4" cols="50"> text. </textarea> </body> </html>
when launch app, error:
"uncaught typeerror: cannot set property 'value' of null"
i tried mywin.document.getelementbyid(...)
, opening window var mywin=window.open(...)
without success.
so question is, how can access elements of newly created window background script?
okay, happens here wrong timing.
the callback of create
executes before dom structure of page constructed; therefore, getelementbyid('areaid')
doesn't find yet.
you can try hook domcontentloaded
event.
alternatively, can go recommended route , define functions/variables in callback code in opened window can use (docs recommend onload
).
Comments
Post a Comment