Showing posts with label JavaScript. Show all posts
Showing posts with label JavaScript. Show all posts

Monday, November 3, 2014

ASP.NET: Force client to update javascript files after each new build


Problem

How to force a client to update cached javascript files after each new build

Solution

Add build number property to your page class

Protected ReadOnly Property CurrentVersion() As Integer
        Get
            Return System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.Build
        End Get
End Property

And update script source to utilize the build number property


<script src="Script/Functions.js?v=<%= CurrentVersion%>" type="text/javascript"></script>


After each new build script source url is changed so browser will update javascript-file to the cache


Friday, June 8, 2012

ASP.NET MVC: How to get model values from separate javascript file


Problem
How to get ASP.NET MVC model values from separate javascript file?

Solution
There are many ways to get model values from separate javascript file. Here are a couple of way to do it:

1. If you are using HTML5 you can use custom data attributes
 <div id="baseTargetUrl" data-basetargeturl="<%= Url.Content("~/") %>" />  
and get custom attribute value from javascript file
 var rootUrl = location.protocol + '//' + location.host +  
 $('#baseTargetUrl').data('basetargeturl');  
2. Set javascript variable from view
 <script type="text/javascript">  
 var baseTargetUrl= <%= Url.Content("~/") %>;  
 </script>  
and use this variable from separate javascript file

3. Extend jQuery to allow own namespace to store values. Here is a great example how to do it

Tuesday, December 9, 2008

ASP.NET: UpdatePanel and popup with javascript

Problem

How to open popup-window with javascript when there is a UpdatePanel control in the page

Solution

You must use ScriptManager.RegisterStartupScript() instead of ClientScript.RegisterStartupScript(). That's because ScriptManager enables partial page update what AJAX needs but ClientScript doesn't enable partial page updates.