admin管理员组

文章数量:1415153

    <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. I would like to remove this multiline part from head and place it in jeditable.js file, which is now empty. 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 pile code that is in <?php ?> tag. Any ideas how to solve this problem?

Thanks in advance,
Ile

    <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. I would like to remove this multiline part from head and place it in jeditable.js file, which is now empty. 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 pile code that is in <?php ?> tag. Any ideas how to solve this problem?

Thanks in advance,
Ile

Share Improve this question edited Mar 26, 2010 at 21:56 Justin Johnson 31.3k7 gold badges66 silver badges89 bronze badges asked Mar 26, 2010 at 21:42 ilija veselicailija veselica 9,58439 gold badges95 silver badges151 bronze badges 2
  • Are you mixing WebForms and MVC in your project? – Roman Commented Mar 26, 2010 at 21:47
  • hmmm... to be honest, I don't know. I've started with ASP.NET MVC 2 months ago and before that I never even worked with C# – ilija veselica Commented Mar 26, 2010 at 21:57
Add a ment  | 

4 Answers 4

Reset to default 4

In PHP I would save js file as jeditable.js.php and server would pile code that is in tag.

One thing to keep in mind here is that php is now forced to process that entire javascript file on every request. This is generally a "Bad Thing"TM, and it uses up server resources that could be spend elsewhere.

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. 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.

I'll make one addition to Mr Kimal's answer. 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:

$(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.

var foo = '<%=Url.Action("UpdateSettings","Article") %>'; and place it before the js reference. Then use the varible in your js file.

You can put the script inside an .aspx file and set the content type to text/javascript in the @page directive. 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. This can be configured in the @page directive as well.

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.

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.

The real question here, why have the overhead of an additional HTTP request for a single method?

本文标签: javascriptSaving js file with c commandsStack Overflow