简体   繁体   中英

how to call javascript functions from parent page?

I got a page which has an iframe. This page has a masterpage on which there is a js function defined.

masterpage contains:

  <script type="text/javascript" src="/somejs.js"></script>

page with iframe:

  <div >
    <iframe id="myIframe" runat="server"></iframe>
  </div>

how can i call functions in the 'somejs.js' file?

If both documents are from the same domain, you can simply call like

window.parent.someFunction();

Otherwise it's the whole different story.

I am not quite clear.

You have a page.master which has <script type="text/javascript" src="/somejs.js"></script>

Then a default.aspx with

<div >
    <iframe id="myIframe" runat="server"></iframe>
</div>

In your iframe , i think you want to show some other page.

So, if you want ur javascript outside iframe ,

you can just call it normally like onload=function1();

Coz when default.aspx is converted to html, it takes everything from master page including ur declaration for javascripts..

Another way is that you can call it from codebehind .cs file.

Eg,

Page.ClientScript.RegisterClientScriptInclude("selective", ResolveUrl(@"js\common.js"));
        if (!Master.Page.ClientScript.IsStartupScriptRegistered("alert"))
        {
            Master.Page.ClientScript.RegisterStartupScript
                (this.GetType(), "alert", "initHomeTabsLoad();", true);
        }

But if you want ur javascript inside ifram, it's another story as you need to call that script on that original page.

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