inject javascript into an iframe for a portal application
I have an frame that has a web application inside it. it expects that certain javascript functions will exist on the page that it can call. How can I inject these javascript functions into the iframe from my paren开发者_C百科t application?
Your question is a little vague on details, such as whether you control the content inside the iframe or not. But there are a number of ways to go about accessing/applying Javascript between frames.
In the page contained within the iFrame:
parent.FunctionName();
This will call a function that exists within your main page that contains the iframe.
Similarly:
YourIFrameName.FunctionName();
Will call a function in your iframe from the parent.
You can also package the needed Javascript functions into a .js file. And include them in the header of whatever page needs them (the iframe and/or the main page).
Include this in your <head>
:
<script type="text/javascript" src="YourJavascriptFile.js"></script>
However, if you do not control the contents, and run into the same origin policy, you have two options:
1) Rethink your application.
2) A workable mess: You would need to call a script from the iframe that does some cURL type magic to pull the page contents of the included web app, inject the needed Javascript, and then output the altered contents in a meaningful way.
If you decided you need to go the route of #2, I can edit with more specifics.
As Robert mentioned, I think that violates the same origin policy in most (if not all) browsers.
Alternatively, instead of trying to inject the functions into the iframe, why not have the iframe content reference them directly from the iframe's parent?
精彩评论