- Modify your widget to use unobtrusive javascript methods so that the entire DOM can be scanned for instances of your widget. Rely on classes instead of identifiers.
- Use a tool like Chirpy to pack all your CSS and Javascript files into one library file (one .js and one .css), which you can add at the top and bottom (respectively) of your main page / layout.
- Use the HTML helper to create your HTML widget. If you like, the HTML helper can display a partial view to save the HTML view code in the view file. Since you are using unobtrusive javascript, there is no need to create any kind of script inline. Just let the widget's javascript code scan the DOM for instances of your widget.
Step 1
The original example already uses some unobtrusive javascript methods, but here you can extend this template even further, as shown in this jsfiddle .
Widget.cshtml
<div class="date-range-widget"> <h2>Create a date range</h2> <div> <label for="startDate">Start:</label> <input id="startDate" class="date start-date" name="startDate" type="text" /> </div> <div> <label for="endDate">End:</label> <input id="endDate" class="date end-date" name="endDate" type="text" /> </div> <p class="output"></p> <button class="create-date-range">Create</button> </div>
Widget.js
$(function () { // This line could probably be done generally, regardless of whether you're // in the widget. $("input.date").datepicker(); $("div.date-range-widget").each(function(){ var $t = $(this); var startBox = $t.find("input.start-date"); var endBox = $t.find("input.end-date"); var output = $t.find("p.output"); $t.find("button.create-date-range").click(function () { output.html(startBox.val() + " to " + endBox.val()); }); }); });
Please note that now you have several instances of this widget on your page with different id and name values, and they will work independently of each other.
When there is no data range widget, javascript will still search for the DOM, will not find and will not jump. However, the cost of this operation is usually quite light compared to doing a separate round trip for another javascript file. Storing CSS and javascript in separate static files makes it possible for the browser to cache these files, which means that it spends less data on the browser on average (since only HTML changes from one request to the next).
Step 2
I personally found it appropriate to combine my javascript and CSS into one file, so the browser only needs to pull out one javascript and one css file for a given page load (and both will be cached after the first page loads). If you have a very large javascript file that is used only on certain pages, you may want to save it separately and upload only to certain pages.
Regardless, your javascript needs to be written so that you can simultaneously upload all javascript files to your site without stepping on each other's toes.
Step 3
It should be pretty clear.
Stripling warrior
source share