Saving js file using 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 the main tag for the site.master file. I would like to remove this multi-line part from my head and put it in the jeditable.js file, which is now empty. If I copy / paste, then part <% %> %% <% %> will not be executed. In PHP, I would save the js file as jeditable.js.php, and the server would compile the code that is in the <?php ?> Tag. Any ideas how to solve this problem?

Thanks in advance,
Ile

+4
source share
4 answers

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

Keep in mind that php is now forced to process the entire javascript file for each request. This is usually "Bad Thing" TM and it uses server resources that can be spent elsewhere.

As already mentioned in Raj Kimal's answer, what we do in ASP.Net to deal with this most efficient way is a short script defined in a line with a page that does nothing but assign variables to the result of the server code. Do this before declaring other scripts, and you can use these variables directly in these scripts. This way you do not need to do extra server work for external javascript files.

I will make one addition to Mr. Kimal. Often it is best to enclose these variables in an object to avoid name collisions. Something like that:

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

Then your jeditable.js file will 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'/>" }); }); 
+4
source

Define the variable as js variable.

var foo = '<% = Url.Action ("UpdateSettings", "Article")%>'; and put it before the js link. Then use the variable in the js file.

+2
source

You can put the script in the .aspx file and set the content type to text/javascript in the @page directive. You can still use code tags.

The cost of processing the entire javascript file of each request can be easily mitigated by applying server-side caching, so this should not be a problem. This can also be configured in the @page directive.

+2
source

You have several 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 β€œcan” move the actual function to an external file and reference JS, and then add a small inline script block that calls the function with two parameters, but that defeats the goal of what you want.

The real question is: why the overhead of an additional HTTP request for one method?

+1
source

Source: https://habr.com/ru/post/1305262/


All Articles