I got a js function that is defined in a JScript.js file. This file is defined on the masterpage. I am trying to call the sayhello function in this file from the inIframe.aspx page. This inIframe.aspx ‘runs’ inside the webform1.aspx page and the webform1.aspx page has a masterpage called masterWithJs.master.
when I hit :
http://localhost:8022/inIframe.aspx
I get an 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 behindcode:
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
myIframe.Attributes.Add("src","inIframe.aspx");
}
}
Well Then i think i know the solution
Try to create a utility function for the path of the script holding the function you call
like this one
and then call it in the master page HTML
in the beginning of the body
and then try i think it should work if its all about paths as i think
Regards