javascript - 错误 : window. parent.sayhello 不是函数?

标签 javascript asp.net

我得到了一个在 JScript.js 文件中定义的 js 函数。该文件在母版页上定义。我试图从 inIframe.aspx 页面调用此文件中的 sayhello 函数。这个 inIframe.aspx 在 webform1.aspx 页面内“运行”,webform1.aspx 页面有一个名为 masterWithJs.master 的主页。

当我点击:

http://localhost:8022/inIframe.aspx

我在 firebug 中遇到脚本错误:

window.parent.sayhello is not a function

主页:

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="masterWithJs.master.cs"
    Inherits="IFrameJS.masterWithJs" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <asp:ContentPlaceHolder ID="head" runat="server">
        <script src="Scripts/JScript1.js" type="text/javascript"></script>
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
        </asp:ContentPlaceHolder>
    </div>
    </form>
</body>
</html>

webform1.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/masterWithJs.Master" AutoEventWireup="true"
    CodeBehind="WebForm1.aspx.cs" Inherits="IFrameJS.WebForm1" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <iframe id="myIframe" runat="server"></iframe>
</asp:Content>

inIframe.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="inIframe.aspx.cs" Inherits="IFrameJS.inIframe" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        this is the iframe page
    </div>
    </form>
    <script type="text/javascript">
        window.parent.sayhello();
    </script>
</body>
</html>

JScript1.js

function sayhello() {
    alert( 'hello');
}

webform1.aspx 背后代码:

 public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            myIframe.Attributes.Add("src","inIframe.aspx");
        }
    }

最佳答案

webform1.aspx 页面中删除带有 ContentPlaceHolderID="head" 的空 Content 控件。

当页面中有 Content 控件时,它将替换母版页中 ContentPlaceHolder 中的内容,因此不会包含 Javascript 文件。

关于javascript - 错误 : window. parent.sayhello 不是函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7964229/

相关文章:

javascript - React+Redux 中数据更改时更新状态

javascript - Leaflet JS - 将 esri 形状更改为特定缩放级别的标记

asp.net - Windows 共享主机 - 自定义计划任务

asp.net - 如何在源代码控制下轻松地对本地 Web.config 进行特定更改

javascript - 使用 Enyme/Jest 时如何模拟 axios.defaults

javascript - 将一个长的无序列表拆分为多个列表

c# - 如何使用 ASP.NET Identity Framework 设置密码过期

asp.net - 配置 IIS 服务器以使用 Aurelia 框架和推送状态

javascript - 如果单击单选按钮,如何使不可见的文本框可见?

javascript - 在类组件的方法中获取查询