问题
I have a page with an <iframe>
, and inside that <iframe>
another <iframe>
pops up as a window. What I'm trying to do is call a function that is set in the <iframe>
's <iframe>
.... I know, lot's of the same tag going on.
Here's what I mean in HTML format.
<body>
<iframe src="blahblah.html" name="iframe">
//blahblah.html html
<iframe src="blahblahpopup.html" name="iFrame2">
//blahblahpopup.html html, also where the function is set.
</iframe>
</iframe>
</body>
So, as you can see, there is an <iframe>
in an <iframe>
.
Now lets get down to business. I'm using this JS code to get my JS context into the first level <iframe>
with the name 'iframe'
window.frames['iframe'].showAssetPicker();
What that does is it calls the popup inside the first-level <iframe>
. But now I need to call a function inside of the popup that was just called. (which is yet another <iframe>
... I'm not the cause of all the <iframe>
's...)
So here's my failed attempt of calling a function defined in the second-level <iframe>
...
window.frames['iframe'].frames['iFrame2'].populateTypePullDown('/getdata/');
I also tried
window.frames['iframe'].window.frames['iFrame2'].populateTypePullDown('/getdata/');
Neither seem to work. Any help with this? I am using node-webkit to remove <iframe>
security restrictions.
Thanks for any help you can throw my way!
P.S. Goodness gracious, I said <iframe>
far too many times in this post.
回答1:
Probably the issue is that you don't wait before the Asset Picker is loaded and therefore the function populateTypePullDown
is not yet defined.
Try something like this:
$('#iframe')
.ready(function() {
var $innerIframe = $(this).contents().find('#iFrame2');
console.log($innerIframe, 'ready');
$innerIframe.get(0).contentWindow.populateTypePullDown('/vsg/ipm/SecuredOfferRepository');
})
.get(0).contentWindow.showAssetPicker();
回答2:
Figured out the answer. What I did was I reached inside the main <iframe>
, and got the contentWindow of the <iframe>
inside.
$('#iframe').contents().find('#iFrame').get(0).contentWindow.performSearch();
来源:https://stackoverflow.com/questions/30061007/setting-javascript-context-to-iframe-in-an-iframe