我得到了一个在 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/