Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 211
ilija veselica Avatar asked Jan 22 '23 09:01

ilija veselica


2 Answers

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'/>"  
            });
        }); 
like image 51
Joel Coehoorn Avatar answered Jan 24 '23 21:01

Joel Coehoorn


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.

like image 40
Raj Kaimal Avatar answered Jan 24 '23 23:01

Raj Kaimal