views:

184

answers:

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

+2  A: 

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.

Raj Kaimal
Don't forget to escape any quotes in the result.
Joel Coehoorn
Nice trick, but this still leaves some of variables in site.master
ile
@ile - yes, it does. But there's a good reason you _should_ do this, and that reason applies to php as well.
Joel Coehoorn
+1  A: 

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?

Mitchel Sellers
there will be much more of this js functions, maybe 10 of them, so I'd like to put it to external file
ile
"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," - I didn't really understand what you said here... I could move actual function to what external file?
ile
You _can_ tell asp.net to process .js files, but I don't recommend it.
Joel Coehoorn
you mean by adding runat="server"?
ile
@ile - basically take your big function and move it external as you want, but leave a small inline function that just calls the external one.
Mitchel Sellers
+3  A: 

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'/>"  
            });
        }); 
Joel Coehoorn
You would need to pass `ServerCreated.ArticleAction` to the js function in this case
s_hewitt
@s_hewitt thanks for reminding me to show the 'how to use it' part
Joel Coehoorn
I've just figured it :) Thanks!
ile
+2  A: 

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.

Sandor Drieënhuizen
This works very well, but you lose any syntax highlighting/intellisense provided by visual studio.
Joel Coehoorn
That's right, though putting `//<script type="text/javascript">` at the top and `//</script>` at the bottom fixes that. Not very pretty thing to have that go over the wire though...
Sandor Drieënhuizen