简体   繁体   中英

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. 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 compile code that is in <?php ?> tag. 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.

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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