I have a JavaScript function that is defined in a JScript.js file, which is located on the masterpage. My goal is to call the sayhello function from this file on the inIframe.aspx page. The inIframe.aspx page is nested within the webform1.aspx page, and the webform1.aspx page is associated with a masterpage called masterWithJs.master.
When I visit :
http://localhost:8022/inIframe.aspx
I encounter a script error in firebug:
window.parent.sayhello is not a function
Masterpage:
<%@ 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 behind code:
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
myIframe.Attributes.Add("src","inIframe.aspx");
}
}