如何在 ContentPlaceHolder 中运行 JavaScript?

发布于 2024-09-01 20:13:00 字数 161 浏览 3 评论 0原文

当所有内容都在一个页面中时,我有一堆 JavaScript 运行良好。现在我已经将其分离出来,我不确定如何包含 javascript,在 master 中插入 HEAD 标签,而且,我有在主体加载时运行的函数,但它们也在 master 中。

顺便说一句,这是一个 .NET 2.0 应用程序。

I had a load of javascript that ran well when everything was in one page. Now that I've separated it out, I'm not sure how to include the javascript, ssing the HEAD tag in in the master, and also, I had functions that ran when the body loaded but they are in the master too.

By the way this is a .NET 2.0 app.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

2024-09-08 20:13:00

如果您有特定于某些页面的脚本,并且使用一个主 MasterPage,只需在母版页的头部添加一个 ContentPlaceHolder 即可。

母版页:

<head runat="server">

    <script type="text/javascript" src="my-site-wide-script.js"></script>
    <asp:ContentPlaceHolder ID="cphHead" runat="server" />
</head>

内容页:

<asp:content ID="cHead" runat="server" ContentPlaceHolderID="cphHead">
    <script type="text/javascript" src="some-page-specific-script.js"></script>
</asp:content>

提供给浏览器的最终结果(通常):

<head>    
    <script type="text/javascript" src="my-site-wide-script.js"></script>
    <script type="text/javascript" src="some-page-specific-script.js"></script>
</head>

现在,您添加到内容页的任何脚本都将位于基本脚本之后。使您可以灵活地使用特定于页面的脚本。

请注意:VS2005 将抛出警告/错误,指出 head 标记中不允许使用 ContentPlaceHolder。忽略它,这是VS的一个bug。 VS2008+不会为此打扰你。

If you have scripts that are specific to certain pages, and you use one main MasterPage, simply add a ContentPlaceHolder in the head of the master page.

Masterpage:

<head runat="server">

    <script type="text/javascript" src="my-site-wide-script.js"></script>
    <asp:ContentPlaceHolder ID="cphHead" runat="server" />
</head>

content page:

<asp:content ID="cHead" runat="server" ContentPlaceHolderID="cphHead">
    <script type="text/javascript" src="some-page-specific-script.js"></script>
</asp:content>

End result served to browser (in general):

<head>    
    <script type="text/javascript" src="my-site-wide-script.js"></script>
    <script type="text/javascript" src="some-page-specific-script.js"></script>
</head>

Now any scripts you add to your content page will be after your base scripts. Gives you the flexibility to have page-specific scripts.

And note: VS2005 will throw up a warning/error that ContentPlaceHolder isn't allowed within the head tag. Ignore it, it's a bug in VS. VS2008+ will not bother you about it.

你在我安 2024-09-08 20:13:00

一般来说,您可以将多个页面使用的常见 JavaScript 放入一个单独的文件中,并从母版页链接到该文件,并且可以通过使用将任何一次性功能包含在其他任何地方

<script type="text/javascript"></script>

Generally, you would put common javascripts that more than one page use into a separate file and link to it from the master page and any one off functions can be included anywhere else by using

<script type="text/javascript"></script>
青春有你 2024-09-08 20:13:00

母版页的概念是针对服务器端的,您的浏览器将只看到一页结果,因此您可以将 JavaScript 函数放在母版的头部部分,如果需要,可以在内容页面中调用它们,或者在母版的其余部分中调用它们页。

编辑

您可以在母版页的头部部分放置一个内容占位符,因此如果您愿意,子页面可以在头部包含其特定脚本...

示例

<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript" src="yourglobaljsfile.js"></script>

    <asp:ContentPlaceHolder ID="ScriptContent" runat="server" />
</head>
<body>
    <div style="position: absolute; width: 100%; top: 0px;">
     <asp:ContentPlaceHolder ID="OtherPlaceholer" runat="server" />
    </div>
    <script type="text/javascript">yourGlobalfunction();</script>
</body>

子示例

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<CatalogViewModel>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">

    //blabla
     <script type="text/javascript">yourSpecificfunction();</script>
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="ScriptContent" runat="server">
    <script type="text/javascript" src="yourspecificjsfile.js"></script>
</asp:Content>

The concept of masterpage is for serverside, your browser will see the result as only one page so you can put your javascript functions on the head section of your master, and call them in your content pages if you want or in the rest of the master page.

EDIT

You can put a content place holder on your head section in your master page, so child page can include its specific script in the head if you want...

Example

<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript" src="yourglobaljsfile.js"></script>

    <asp:ContentPlaceHolder ID="ScriptContent" runat="server" />
</head>
<body>
    <div style="position: absolute; width: 100%; top: 0px;">
     <asp:ContentPlaceHolder ID="OtherPlaceholer" runat="server" />
    </div>
    <script type="text/javascript">yourGlobalfunction();</script>
</body>

A child example

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<CatalogViewModel>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">

    //blabla
     <script type="text/javascript">yourSpecificfunction();</script>
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="ScriptContent" runat="server">
    <script type="text/javascript" src="yourspecificjsfile.js"></script>
</asp:Content>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文