Asp.Net Mvc - ContentPlaceHolder - Javascript

发布于 2024-08-21 02:41:52 字数 1881 浏览 7 评论 0原文

根据我所在的页面,我希望更改 javascript 函数的内容。

示例:

MasterPage

<script type="text/javascript">
    Cufon.now();
    Cufon.replace('p#characters', { fontFamily: 'Helvetica95-Black' });
</script>

页面 A

<script type="text/javascript">
    Cufon.now();
    Cufon.replace('p#characters', { fontFamily: 'Helvetica95-Black' });
    Cufon.replace('p#others', { fontFamily: 'Helvetica95-Black' });
</script>

因为我不想重复代码,所以我正在寻找解决方案。我想也许我可以使用像这样的 ContentPlaceHolder 之类的东西(但它不起作用):

<script type="text/javascript">
    Cufon.now();
    Cufon.replace('p#characters', { fontFamily: 'Helvetica95-Black' });

    <asp:ContentPlaceHolder id="cphJS" runat="server" />
</script>

有什么方法可以解决此类问题吗?

更新

我想避免的是有这样的代码:

// Add by MasterPage
<script type="text/javascript">
    Cufon.now();
    Cufon.replace('p#characters', { fontFamily: 'Helvetica95-Black' });
</script>

// Add by Page A
<script type="text/javascript">
    Cufon.now();
    Cufon.replace('p#others', { fontFamily: 'Helvetica95-Black' });
</script>

// Add by Page B
<script type="text/javascript">
    Cufon.now();
    Cufon.replace('p#main', { fontFamily: 'Helvetica95-Black' });
    Cufon.replace('p#menu', { fontFamily: 'Helvetica95-Black' });
</script>

因为这种代码可以工作,但它不是执行它的清洁方式。我更喜欢这样的东西:

<script type="text/javascript">
    Cufon.now();        
    Cufon.replace('p#characters', { fontFamily: 'Helvetica95-Black' });
    Cufon.replace('p#others', { fontFamily: 'Helvetica95-Black' });
    Cufon.replace('p#main', { fontFamily: 'Helvetica95-Black' });
    Cufon.replace('p#menu', { fontFamily: 'Helvetica95-Black' });
</script>

结果是一样的。我只发现第二个输出更干净。

Depending on which page I am, I would like the content of a javascript function to change.

Example :

MasterPage

<script type="text/javascript">
    Cufon.now();
    Cufon.replace('p#characters', { fontFamily: 'Helvetica95-Black' });
</script>

Page A

<script type="text/javascript">
    Cufon.now();
    Cufon.replace('p#characters', { fontFamily: 'Helvetica95-Black' });
    Cufon.replace('p#others', { fontFamily: 'Helvetica95-Black' });
</script>

Because I do not want to do code repetition, I'm looking to a solution. I thought that maybe I could use something like a ContentPlaceHolder like this (but it doesn't work) :

<script type="text/javascript">
    Cufon.now();
    Cufon.replace('p#characters', { fontFamily: 'Helvetica95-Black' });

    <asp:ContentPlaceHolder id="cphJS" runat="server" />
</script>

Any way to solve this kind of problem ?

UPDATE

What I will like to avoid is to have code like that :

// Add by MasterPage
<script type="text/javascript">
    Cufon.now();
    Cufon.replace('p#characters', { fontFamily: 'Helvetica95-Black' });
</script>

// Add by Page A
<script type="text/javascript">
    Cufon.now();
    Cufon.replace('p#others', { fontFamily: 'Helvetica95-Black' });
</script>

// Add by Page B
<script type="text/javascript">
    Cufon.now();
    Cufon.replace('p#main', { fontFamily: 'Helvetica95-Black' });
    Cufon.replace('p#menu', { fontFamily: 'Helvetica95-Black' });
</script>

Because that kind of code work but it's not the cleaning way of doing it. I would prefer to have something like that :

<script type="text/javascript">
    Cufon.now();        
    Cufon.replace('p#characters', { fontFamily: 'Helvetica95-Black' });
    Cufon.replace('p#others', { fontFamily: 'Helvetica95-Black' });
    Cufon.replace('p#main', { fontFamily: 'Helvetica95-Black' });
    Cufon.replace('p#menu', { fontFamily: 'Helvetica95-Black' });
</script>

The result is the same. I only find that the second output is cleaner.

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

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

发布评论

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

评论(5

↘紸啶 2024-08-28 02:41:52

为什么不简单地将附加代码添加到脚本标记之外的内容占位符中?

因此,您可能会看到类似的内容:

<head>
   <script type="text/javascript">
      // do some stuff
   </script>
   <asp:ContentPlaceHolder runat="server" id="someid" />
</head>
...

或者,您可以在页面早期的数组中收集函数,然后稍后执行它们,如下所示:

在 中:

var callbacks = [];
function addCallback(arg) {
   callbacks.push(arg);
}

在其他位置:

addCallback(function() {
   ...
});

然后在页面底部,就在 之前:

for (var i = 0; i < callbacks.length; i++) {
   var c = callbacks[i];
   if (c && c instanceof Function)
      c();
}

您能否提供有关要求的更多详细信息?例如:上面的代码应该做什么,是否必须立即执行,是否依赖于创建的局部变量,是否必须在某个时间运行等等。

编辑:
在新的 MVC 项目中使用内容占位符似乎对我来说效果很好。智能感知没有拾取它,但它渲染正确。对于我来说,代码是这样的。

在母版页中:

...
<script type="text/javascript">
    var inTheMasterPage;
    <asp:ContentPlaceHolder runat="server" id="js1">
    </asp:ContentPlaceHolder>
</script>

...

在页面中:

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

<asp:Content runat="server" ContentPlaceHolderID="js1">
    var inThePage;
</asp:Content>
...

以及渲染的 HTML:

    <script type="text/javascript"> 
        var inTheMasterPage;

    var inThePage;

    </script> 
</head> 

Why not simply add the additional code in a content place holder outside of the script tag?

So, you would have something like:

<head>
   <script type="text/javascript">
      // do some stuff
   </script>
   <asp:ContentPlaceHolder runat="server" id="someid" />
</head>
...

Or, you might collect functions in an array early in the page and then execute them later on like so:

In the <head>:

var callbacks = [];
function addCallback(arg) {
   callbacks.push(arg);
}

At some other location:

addCallback(function() {
   ...
});

And then at the bottom of the page, just before the </body>:

for (var i = 0; i < callbacks.length; i++) {
   var c = callbacks[i];
   if (c && c instanceof Function)
      c();
}

Could you provide more details about the requirements? Ex: What the above code is supposed to do, does it have to be executed all at once, does it depend on local variables that are created, does it have to run at a certain time, etc.

Edit:
Using a content placeholder in a new MVC project seemed to work fine for me. Intellisense didn't pick it up but it rendered correctly. Here's what the code looks like for me.

In the master page:

...
<script type="text/javascript">
    var inTheMasterPage;
    <asp:ContentPlaceHolder runat="server" id="js1">
    </asp:ContentPlaceHolder>
</script>

...

In the page:

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

<asp:Content runat="server" ContentPlaceHolderID="js1">
    var inThePage;
</asp:Content>
...

And the rendered HTML:

    <script type="text/javascript"> 
        var inTheMasterPage;

    var inThePage;

    </script> 
</head> 
转瞬即逝 2024-08-28 02:41:52

您可以使用RegisterClientScriptBlock。基本上,这可以创建脚本标签和代码,同时为您提供更多服务器端控制。话虽如此,您需要与母版页进行通信。您可以将 StringBuilder 公开为母版页的公共属性:

StringBuilder javascriptCode = new StringBuilder();
javascriptCode.Append("Cufon.now();" + System.Environment.NewLine);
javascriptCode.Append("Cufon.replace('p#characters', { fontFamily: 'Helvetica95-Black' });" + System.Environment.NewLine);
public StringBuilder JSCode
{
    get { return javascriptCode; }
}

在内容页面中,您添加 MasterType 指令以访问 Master 对象:

<%@ MasterType VirtualPath="~/virtualPath/nameOfMasterPage.page" %>

在内容页面中,您可以附加更多 JavaScript 代码并调用 RegisterClientScriptBlock:

Master.JSCode.Append("Cufon.replace('p#others', { fontFamily: 'Helvetica95-Black' });" + System.Environment.NewLine);
ClientScriptManager clientScript = Page.ClientScript;
clientScript.RegisterClientScriptBlock(this.Page.GetType(), "MadeUpNameForJSScript", Master.JSCode.ToString(), true);

我还没有测试了这段代码,如果有任何问题请告诉我。这可能是一种不方便的方法,但它应该将所有 JavaScript 组合到页面上的一个脚本块中。

编辑:我也不知道这是否适用于 MVC

You can use RegisterClientScriptBlock. Basically, this can create the script tags and code while giving you more server-side control. With that said, you'll need to communicate with the master page. You could expose a StringBuilder as a public property of the Master Page:

StringBuilder javascriptCode = new StringBuilder();
javascriptCode.Append("Cufon.now();" + System.Environment.NewLine);
javascriptCode.Append("Cufon.replace('p#characters', { fontFamily: 'Helvetica95-Black' });" + System.Environment.NewLine);
public StringBuilder JSCode
{
    get { return javascriptCode; }
}

In the content page you add a MasterType directive to get access to the Master object:

<%@ MasterType VirtualPath="~/virtualPath/nameOfMasterPage.page" %>

And in your content page you could append more JavaScript code and call RegisterClientScriptBlock:

Master.JSCode.Append("Cufon.replace('p#others', { fontFamily: 'Helvetica95-Black' });" + System.Environment.NewLine);
ClientScriptManager clientScript = Page.ClientScript;
clientScript.RegisterClientScriptBlock(this.Page.GetType(), "MadeUpNameForJSScript", Master.JSCode.ToString(), true);

I haven't tested this code so let me know if there's any problems with it. It may be a bit of an inconvenient method but it should combine all the JavaScript into one script block on the page.

Edit: I also don't know if this will work with MVC

香橙ぽ 2024-08-28 02:41:52

ContentPlaceHolder 是个好主意,你只是放错了地方
您也可以

母版页中

<asp:contentplaceholder id="scripts" runat="server" >
   <script type="text/javascript">
       Cufon.now();
       Cufon.replace('p#characters', { fontFamily: 'Helvetica95-Black' });
   </script>
</asp:contentplaceholder>

在页面 A 的

<asp:Content ID="scriptContent" ContentPlaceHolderID="scripts" runat="server">
   <script type="text/javascript">
       Cufon.now();
       Cufon.replace('p#characters', { fontFamily: 'Helvetica95-Black' });
       Cufon.replace('p#others', { fontFamily: 'Helvetica95-Black' });
   </script>
</asp:contentplaceholder>

页面中的内容将覆盖母版页中的内容

覆盖它们,如果您想附加脚本,则

<script type="text/javascript">
    Cufon.now();
    Cufon.replace('p#characters', { fontFamily: 'Helvetica95-Black' });

    <asp:ContentPlaceHolder id="cphJS" runat="server" />
</script>

,然后将内容占位符保留在页面 A 的母版页中,不要放置脚本标签

<asp:Content ID="scriptContent" ContentPlaceHolderID="cphJS" runat="server">
        Cufon.replace('p#others', { fontFamily: 'Helvetica95-Black' });
</asp:contentplaceholder>

ContentPlaceHolder is a good idea you simply missplaced it
also you can override them

in Master page

<asp:contentplaceholder id="scripts" runat="server" >
   <script type="text/javascript">
       Cufon.now();
       Cufon.replace('p#characters', { fontFamily: 'Helvetica95-Black' });
   </script>
</asp:contentplaceholder>

in page A

<asp:Content ID="scriptContent" ContentPlaceHolderID="scripts" runat="server">
   <script type="text/javascript">
       Cufon.now();
       Cufon.replace('p#characters', { fontFamily: 'Helvetica95-Black' });
       Cufon.replace('p#others', { fontFamily: 'Helvetica95-Black' });
   </script>
</asp:contentplaceholder>

the content from the page will override the content from the master page

if you want to append scripts then keep your content place holder in master page

<script type="text/javascript">
    Cufon.now();
    Cufon.replace('p#characters', { fontFamily: 'Helvetica95-Black' });

    <asp:ContentPlaceHolder id="cphJS" runat="server" />
</script>

in your page A don't put the script tags

<asp:Content ID="scriptContent" ContentPlaceHolderID="cphJS" runat="server">
        Cufon.replace('p#others', { fontFamily: 'Helvetica95-Black' });
</asp:contentplaceholder>
泼猴你往哪里跑 2024-08-28 02:41:52

理想情况下,您需要累积这些部分脚本并将它们呈现为页面末尾的一个块,

虽然不是 100% 确定如何在 MVC 中执行此操作,但 这是一个普通 ASP.NET 的示例 你应该能够融入 MVC

Ideally you need to accumulate these partial scripts and render them as one block at the end of the page

Not 100% sure how to do this in MVC but here is a an example for plain ASP.NET that you should be able to shoe horn into MVC

╰つ倒转 2024-08-28 02:41:52

选择器的优点之一是对象不必存在于页面上。您还可以一次向 Cufon 提供多个选择器...

因此,如果您将其放入母版页中,它应该可以正常工作...

<script type="text/javascript">
    Cufon.now();        
    Cufon('p#characters, p#others, p#main, p#menu', { fontFamily: 'Helvetica95-Black' });
</script>

One of the beauties of the selectors is that the object doesn't have to exist on the page. You can also supply multiple selectors to Cufon in one hit...

So if you put this in your master page it should just work...

<script type="text/javascript">
    Cufon.now();        
    Cufon('p#characters, p#others, p#main, p#menu', { fontFamily: 'Helvetica95-Black' });
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文