Master and Content js

  • Thread starter Thread starter David C
  • Start date Start date
D

David C

Can I have javascript in both the Master Page and inside the Content page?
I want to keep shared js inside the Master and have additional js inside the
content page. I tried using the ContentPlaceHolderID="head" in the content
page but then the Master Page "head" was ignored. My Master Page "head"
example is below. Thanks.

David


<asp:ContentPlaceHolder ID="head" runat="server">
<script language="javascript" type="text/javascript">
function getScrollBottom(p_oElem) {
return p_oElem.scrollHeight - p_oElem.scrollTop -
p_oElem.clientHeight;
}
function opentasks(stid) {
var surl = 'Tasks.aspx?tid=' + stid;
//give new window a name so we know where we came from when
closing task screen
window.open(surl, "viahome",
"height=475,width=800,status=yes,toolbar=no,menubar=no,location=no");
}
function openemail(spid) {
var surl = 'Tasks.aspx?pid=' + spid;
//give new window a name so we know where we came from when
closing task screen
window.open(surl, "viahome",
"height=475,width=760,status=yes,toolbar=no,menubar=no,location=no");
}
</script>
</asp:ContentPlaceHolder>
 
it seems to me you have your java script inside the placceholder, it should
be outside

take a look at this master page

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script type="text/javascript">
<!--
javascript

// -->
</script>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="masterForn" runat="server">
<asp:ContentPlaceHolder ID="body" runat="server">
</asp:ContentPlaceHolder>
</form>
</body>
</html>
 
Can I have javascript in both the Master Page and inside the Content
page? I want to keep shared js inside the Master and have additional
js inside the content page. I tried using the
ContentPlaceHolderID="head" in the content page but then the Master
Page "head" was ignored. My Master Page "head" example is below.
Thanks.

yes, but yours is inside the placeholder, which will be overwritten by the
actual page. As long as the scripts do not conflict, which can be tested in
a single page without a master, you can run as many as you want (master
page, page, control). This does not mean running scripts at every level is
a great idea, but it is possible.

Peace and Grace,


--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
OK, thanks. So what is the purpose of the "head" ContentPlaceholder?
Thanks.

So you can, at the page level, embed in scripts.That would be in the
asp:Content region for the head ContentPlaceholder. Do not place code in
the Content PlaceHolder unless you want it overwritten in some pages. I
hope this is clear, as I am in the middle of numerous things. ;-)

Peace and Grace,


--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
WAIT A MINUTE WAIT A MINUTE WAIT A MINUTE

Are you all gone off on vacation or what? All --SHARED-- JavaScript should
be in a linked file which has been the best practice for years. Is that
context being ignored here?

Otherwise, embedded or inline JavaScript can be used anywhere in the .master
however in general and especially when using 3rd party script in our .master
the <script> is most frequently embedded right before the </body> element to
avoid conflicts with other script while the page is loading (to the greatest
extent possible) .

Unless your using Themes which are really FUBAR and intrusive it is really
easy to control every line of code written into the <head> of a Content page
when embedded script is unique to that particular page but all things
considered script should be written to a file that loads from the file
system.
 
Back
Top