views:

21

answers:

2

I've seen/read plenty advocating "unobtrusive" JavaScript contained in separate files. I'm preparing to combine all my JavaScript from three partial views into a single file that I will then reference somewhere in my master.

My question is: is there any type of JavaScript that should remain behind in the html? One example that seems to me may present a problem would be something like:

<script type="text/javascript">
    $(document).ready(function () {
        $('#newQuoteLink').click(function () {
            $('#newQuoteLink').hide();
            $('#newQuoteDiv').load('/Quote/Create/<%:Model.Book.BookID%>');
            return false;
        });
    });
</script>

--in particular the

<%:Model.Book.BookID%>

Am I correct in assuming this script would not work if loaded from a separate file?

I mostly wanted to check if there were any caveats or other considerations prior to combining everything into this lone, separate file.

Thanks in advance.

+1  A: 

Yep, you're right in that <%:Model.Book.BookID%> will not be visible to the script file. These things are part of the server side script that generates the HTML that is sent to the browser.

You can put all the bulk of the work in the script in a funciton which accepts the id as a param, and then in your html, from your .ready(..) call the function like doStuff("<%:Model.Book.BookID%>") etc.

Javascript experts: other caveats? I'll update when i think of some

Java Drinker
So each event/function that relies on this sort of parameter would need to be re-written as well as the calls being changed as well...hmm. I've got some work to do it seems.Thank you.
PolishedTurd
On the topic of SO protocol: does one immediately mark a helpful answer such as this, or rather wait for additional content/caveats that may potentially be added?
PolishedTurd
That is typically a question for the meta site, http://meta.stackoverflow.com/. However, I would mark it as an answer if it answers your question. You can always un-mark and mark someone else if there is something better poster.
Dustin Laine
+3  A: 

Nope, promise to never ever hardcode url addresses that are route dependent like you did in your javascript file. It's bad, bad, bad. Did I say it's bad?

That's too much of a javascript in a view (it's a bandwidth waste). You could try a global javascript variable declaration your view:

<script type="text/javascript">
    var quoteUrl = '<%: Url.Action("create", "quote", new { id = Model.Book.BookID }) %>';
</script>

and in your javascript file:

$(function () {
    $('#newQuoteLink').click(function () {
        $('#newQuoteLink').hide();
        $('#newQuoteDiv').load(quoteUrl);
        return false;
    });
});

That's a path I wouldn't personally take. Still a script tag with a global javascript variable declaration in your view. Still a waste.


Things become even prettier like this (and it is at that moment that you realize the real power of unobtrusive javascript):

<%: Html.ActionLink("Foo Bar Beer Link Text", "create", "quote", 
    new { id = Model.Book.BookID }, new { id = "newQuoteLink" }) %>

and in your external javascript:

$(function () {
    $('#newQuoteLink').click(function () {
        $('#newQuoteLink').hide();
        $('#newQuoteDiv').load(this.href);
        return false;
    });
});
Darin Dimitrov
That last one is exactly what I need to do in this case. Thanks.
PolishedTurd
@PolishedTurd, oh yeah, I know that's exactly what you need to do :-)
Darin Dimitrov