The following functionfrom the master page's
bodydon't start.
<bodyonload="hello()";>
If I createa page without a
masterpageit's okay.
Unfortunately Ineed to run
a javascript functionthat manages the
dynamic layoutof the site,
therefore it actson the elements that
are in themasterpage.
How doI fix it?
I confirmed that calling a js function in the body onload event on a masterpage works. Where does your "hello()" function come from? Is it in the content page or the master page? Is it inline or imported from a js file? Maybe the problem is with the page
not finding your function. This scenario is possible because your example that works:
WebDeveloper3
<body onload="alert('ciao')";>
is using a js built in function. The sample that doesn't work is looking for a user-defined function.
Another side note, the semicolon is unneccessary in this context but it belongs inside of the quotes. Like this:
The path that you specify for the script file needs to be relative to the content page, not the masterpage. The way you have it now, your content page is going to look up one folder (../) and then try to find the .js file. Try to think of the masterpage
as a entity belonging to the content page, not vice versa.
WebDeveloper...
Member
2 Points
20 Posts
Javascript function in MasterPages body
Dec 27, 2012 12:46 PM|LINK
Hi,
The following function from the master page's body don't start.
<body onload="hello()";>
If I create a page without a master page it's okay.
Unfortunately I need to run a javascript function that manages the dynamic layout of the site, therefore it acts on the elements that are in the master page. How do I fix it?
If in master page's body I write
<body onload="alert('ciao')";>
it go.
If I write:
<body onload="hello()";>
with in the script file
function hello() {
alert('hello');
}
it don't go.
joshj
Contributor
3173 Points
546 Posts
Re: Javascript function in MasterPages body
Dec 27, 2012 01:32 PM|LINK
I confirmed that calling a js function in the body onload event on a masterpage works. Where does your "hello()" function come from? Is it in the content page or the master page? Is it inline or imported from a js file? Maybe the problem is with the page not finding your function. This scenario is possible because your example that works:
is using a js built in function. The sample that doesn't work is looking for a user-defined function.
Another side note, the semicolon is unneccessary in this context but it belongs inside of the quotes. Like this:
<body onload="alert('ciao');">WebDeveloper...
Member
2 Points
20 Posts
Re: Javascript function in MasterPages body
Dec 27, 2012 01:43 PM|LINK
The function is in Script\GeneralFunctions.js
Master Page is in MasterPage\
Default is in site root.
In Master Page i have:
<script src="../Scripts/GeneralFunctions.js" type="text/javascript"></script>
I drag the js file in design window of master page.
WebDeveloper...
Member
2 Points
20 Posts
Re: Javascript function in MasterPages body
Dec 27, 2012 01:49 PM|LINK
Master Page's code is:
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage_MasterPage" %> <!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"> <script src="../Scripts/jquery-1.8.2.js" type="text/javascript"></script> <script src="../Scripts/GeneralFunctions.js" type="text/javascript"></script> <head runat="server"> <title></title> <link href="../Styles/MasterPage.css" rel="stylesheet" type="text/css" /> <link href="../Styles/Proprietà.css" rel="stylesheet" type="text/css" /> <link href="../Styles/MenuStyle.css" rel="stylesheet" type="text/css" /> <asp:ContentPlaceHolder id="head" runat="server"> </asp:ContentPlaceHolder> </head> <body onload="ciao()"> <form id="form1" runat="server"> <div id="div-bg"> <asp:Image ID="Sfondo" runat="server" CssClass="sfondoCss" ImageUrl="../Images/Photovoltaics.jpg" /> </div> <div id="barraVerticale" class="opacity90"> <asp:Menu ID="Menu1" runat="server" DataSourceID="SiteMapDataSource1"> <StaticMenuStyle CssClass=" StaticMenuStyle" /> <StaticMenuItemStyle CssClass="StaticMenuItemStyle" /> </asp:Menu> <asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" ShowStartingNode="False" /> <asp:Image ID="Image1" client-id="Image1" runat="server" ImageUrl="../Images/Eng.jpg" Width="25px" Height="16px" /> </div> <div id="mainContent" > <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server"> </asp:ContentPlaceHolder> </div> </form> </body> </html>WebDeveloper...
Member
2 Points
20 Posts
Re: Javascript function in MasterPages body
Dec 27, 2012 01:56 PM|LINK
If I insert in my Default page:
<script src="Scripts/GeneralFunctions.js" type="text/javascript"></script>
It go.
The default page don't inherit the js included in master page?????
MetalAsp.Net
All-Star
112151 Points
18246 Posts
Moderator
Re: Javascript function in MasterPages body
Dec 27, 2012 02:06 PM|LINK
Try putting the script tags inside the head section.
WebDeveloper...
Member
2 Points
20 Posts
Re: Javascript function in MasterPages body
Dec 27, 2012 02:21 PM|LINK
No, with the script tags in head of master pages it don't go.
But if I move default page in \Form folder, it go. The ...\Script it's the same path.
joshj
Contributor
3173 Points
546 Posts
Re: Javascript function in MasterPages body
Dec 27, 2012 02:40 PM|LINK
The path that you specify for the script file needs to be relative to the content page, not the masterpage. The way you have it now, your content page is going to look up one folder (../) and then try to find the .js file. Try to think of the masterpage as a entity belonging to the content page, not vice versa.
As an example, here is a structure that works:
c:\inetpub\wwwroot
-site.master
-\content
-default.aspx
-\script
-script.js
site.master
<head> <script type="text/javascript" src="~/script/script.js" /> </head>WebDeveloper...
Member
2 Points
20 Posts
Re: Javascript function in MasterPages body
Dec 27, 2012 03:02 PM|LINK
My structure is
c:\inetpub\wwwroot
-default.aspx
-\MasterPage
-masterpage.aspx
-\script
-script.js
I tried with ~ in my master page but it don't go. I don't know why.
<head> <script type="text/javascript" src="~/script/script.js" /> </head>joshj
Contributor
3173 Points
546 Posts
Re: Javascript function in MasterPages body
Dec 27, 2012 04:16 PM|LINK
Remove the "~" and the ".." So it looks like: