简体   繁体   中英

Running Javascript in an iframe from a different domain

I'm working on a website that uses iframes from another company to display a list of documents. The documents are paginated under a number of tabs ("All, Presentations, Reports", etc). If I use this element as an example:

<iframe id="frame" src="www.different-domain.com"></iframe>

... is there a way I can open a specific tab, upon page load? So, if I want to open up a page with "Presentations" loaded, rather than "All" (the default), can I use Javascript/jQuery to accomplish this?

The tabs inside the use Javascript to generate a list of all of the items, like this:

<a href="javascript:loadPresentationData()">Presentations</a>

I can't simply run this:

document.getElementById('frame').contentWindow.loadPresentationData();

... as I will get 'Permission denied to access property 'loadPresentationData'.

If I open the iframe src in a new browser tab, go to the console, and type 'loadPresentationData();', it works perfectly, though!

Is there any other way I can just get the tab to open upon page load, without having to contact the developers of the frame (this was supposed to be finalised already).

If I did have to contact them, what would I need to ask them to do, so that I could open the tab upon page load?

Thanks very much.

The only way you can manipulate tabs in the iframe is IMHO through url. It depends on how they (the 3rd party) implemented tabs. In JQuery UI for example you can access the tab with hashes eg

www.differentdomain.com/#tab-3

Note that for further investigation, we'd need to know more details (the real domain for example)

There are two ways you could tell the document in the iframe to do stuff, and neither can be done unilaterally.

As @stove mentionned, the easiest way (for structural things like tabs) would be to use the URL with a hash; the app on the other domain just needs to support them.

The other, more flexible way would be to use Cross-document messaging (you'll find a very relevant example there). For that to work, the developers of the app on the other domain must write a message handler on their side.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM