简体   繁体   English

使用c#命令保存js文件

[英]Saving js file with c# commands

    <head runat="server">
        <title><asp:ContentPlaceHolder ID="TitleContent" runat="server" /></title>
        <link href="../../Content/css/layout.css" rel="stylesheet" type="text/css" />    
        <script type="text/javascript" src="/Areas/CMS/Content/js/jquery-1.3.2.min.js"></script>    
        <script type="text/javascript" src="/Areas/CMS/Content/js/jquery.jeditable.js"></script>
        <script type="text/javascript" src="/Areas/CMS/Content/js/jeditable.js"></script> 

        <script type="text/javascript">
            $(document).ready(function() {
                $(".naslov_vijesti").editable('<%=Url.Action("UpdateSettings","Article") %>', {
                       submit: 'ok', 
                       submitdata: {field: "Title"},  
                       cancel: 'cancel',   
                       cssclass: 'editable',   
                       width: '99%',   
                       placeholder: 'emtpy',   
                       indicator: "<img src='../../Content/img/indicator.gif'/>"  
                });
            }); 
        </script>      
</head>

This is head tag of site.master file. 这是site.master文件的头标记。 I would like to remove this multiline part from head and place it in jeditable.js file, which is now empty. 我想从头部删除这个多行部分并将其放在jeditable.js文件中,该文件现在为空。 If I do copy/paste, then <% %> part won't be executed. 如果我复制/粘贴,则<% %>部分将不会被执行。 In PHP I would save js file as jeditable.js.php and server would compile code that is in <?php ?> tag. 在PHP中,我将js文件保存为jeditable.js.php,服务器将编译<?php ?>标签中的代码。 Any ideas how to solve this problem? 任何想法如何解决这个问题?

Thanks in advance, 提前致谢,
Ile

In PHP I would save js file as jeditable.js.php and server would compile code that is in tag. 在PHP中,我将js文件保存为jeditable.js.php,服务器将编译标签中的代码。

One thing to keep in mind here is that php is now forced to process that entire javascript file on every request. 这里要记住的一件事是,php现在被迫在每个请求上处理整个javascript文件。 This is generally a "Bad Thing" TM , and it uses up server resources that could be spend elsewhere. 这通常是一个“Bad Thing” TM ,它耗尽了可能在其他地方花费的服务器资源。

As Raj Kimal's answer already mentioned, what we do in ASP.Net to handle this in the most efficient way possible is have a short script defined inline with the page that does nothing but assign result of server code to variables. 正如Raj Kimal的回答已经提到的那样,我们在ASP.Net中以尽可能最有效的方式处理这个问题的方法是使用与页面内联定义的短脚本,除了将服务器代码的结果分配给变量之外什么都不做。 Do this before declaring other scripts, and you can then use these variables in those scripts directly. 声明其他脚本之前执行此操作,然后可以直接在这些脚本中使用这些变量。 That way, you don't have to do any extra server work for your external javascript files. 这样,您不必为外部JavaScript文件执行任何额外的服务器工作。

I'll make one addition to Mr Kimal's answer. 我将对Kimal先生的答案做一个补充。 It's often best to enclose these variables in an object, to help avoid naming collisions. 通常最好将这些变量括在一个对象中,以帮助避免命名冲突。 Something like this: 像这样的东西:

<head runat="server">
    <script language="javascript">
       var ServerCreated = {
          ArticleAction:'<%=Url.Action("UpdateSettings","Article") %>',
          OtherVar:'some server data'
       }
    </script>
</head>

Then your jeditable.js file would look like this: 然后你的jeditable.js文件看起来像这样:

$(document).ready(function() {
            $(".naslov_vijesti").editable(ServerCreated.ArticleAction, {
                   submit: 'ok', 
                   submitdata: {field: "Title"},  
                   cancel: 'cancel',   
                   cssclass: 'editable',   
                   width: '99%',   
                   placeholder: 'emtpy',   
                   indicator: "<img src='../../Content/img/indicator.gif'/>"  
            });
        }); 

Define your variable part as a js variable. 将变量部分定义为js变量。

var foo = '<%=Url.Action("UpdateSettings","Article") %>'; var foo ='<%= Url.Action(“UpdateSettings”,“Article”)%>'; and place it before the js reference. 并将它放在js参考之前。 Then use the varible in your js file. 然后在js文件中使用varible。

You can put the script inside an .aspx file and set the content type to text/javascript in the @page directive. 您可以将脚本放在.aspx文件中,并在@page指令中将内容类型设置为text/javascript You will still be able to use the code tags. 您仍然可以使用代码标记。

The cost of processing the entire javascript file every request can easily be mitigated by applying server side caching so that shouldn't be a problem. 通过应用服务器端缓存可以轻松减轻每个请求处理整个javascript文件的成本,因此这不应该是一个问题。 This can be configured in the @page directive as well. 这也可以在@page指令中配置。

You have a few options, but the key here is that if you need to use the <% %> syntax to get information you need to be in the context of an ASP.NET page. 您有几个选项,但关键是如果您需要使用<%%>语法来获取需要在ASP.NET页面上下文中的信息。

You "could" move the actual function to an external file, and reference the JS, then add a small inline script block that called the function with two parameters, but that defeats the purpose of what you want. 您可以“将”实际函数移动到外部文件,并引用JS,然后添加一个小的内联脚本块,该块使用两个参数调用该函数,但这会破坏您想要的目的。

The real question here, why have the overhead of an additional HTTP request for a single method? 这里真正的问题是,为什么要为单个方法增加额外的HTTP请求?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM