简体   繁体   English

如何从父页面调用JavaScript函数?

[英]how to call javascript functions from parent page?

I got a page which has an iframe. 我有一个包含iframe的页面。 This page has a masterpage on which there is a js function defined. 这个页面有一个母版,其上有定义的js函数。

masterpage contains: 母版包含:

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

page with iframe: iframe页面:

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

how can i call functions in the 'somejs.js' file? 我怎么能调用函数中的“somejs.js”文件?

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> 你有page.master其具有<script type="text/javascript" src="/somejs.js"></script>

Then a default.aspx with 然后是default.aspx

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

In your iframe , i think you want to show some other page. 在您的iframe ,我认为您想显示其他页面。

So, if you want ur javascript outside iframe , 因此,如果您要在iframe之外使用javascript,

you can just call it normally like onload=function1(); 您可以像onload=function1();那样正常调用它onload=function1();

Coz when default.aspx is converted to html, it takes everything from master page including ur declaration for javascripts.. Coz当将default.aspx转换为html时,它将从母版页中获取所有内容,包括您对javascript的声明。

Another way is that you can call it from codebehind .cs file. 另一种方法是,您可以从.cs文件背后的代码中调用它。

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. 但是,如果您想在ifram中使用javascript,那是另一回事了,您需要在该原始页面上调用该脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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