JS/CSS file modification After browser local cache resolution

Source: Internet
Author: User
Tags sha1

The example in this article describes how to update the cache with the modification of the HTML page with JS. Share to everyone for your reference. The implementation method is as follows:

Many friends will encounter this situation: if we load the page JS words will be called the next time the JS cache file, but for our modified after debugging and release is very inconvenient, this article to talk about how to solve this problem, let's take a look.

First, the background to write a Urlcommon js/css URL connection Operation class

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingsystem.web;6 usingSystem.Web.Optimization;7 8 namespaceWebapp.common9 {Ten      Public Abstract classUrlcommon One     { A          Public Abstract string_defaulttagformat {Get; } -          Public Abstract string_vertagformat {Get; } -         protected Internal Virtualstring ComputeSHA1 (string fileName) the         { -String hashSHA1 =String.Empty; -             if(System.IO.File.Exists (fileName)) -             { +                 using(System.IO.FileStream fs =NewSystem.IO.FileStream (FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read)) -                 { +System.Security.Cryptography.SHA1 Calculator =System.Security.Cryptography.SHA1.Create (); Abyte[] Buffer =Calculator.computehash (FS); at Calculator. Clear (); -StringBuilder StringBuilder =NewStringBuilder (); -                      for(inti =0; I < buffer. Length; i++) -                     { -Stringbuilder.append (Buffer[i]. ToString ("X2")); -                     } inHashSHA1 =stringbuilder.tostring (); -                 } to             } +             returnhashSHA1; -         } the          Public VirtualIhtmlstring URL (stringUrlBOOLver =true) *         { $             stringAbsurl =System.Web.Optimization.Scripts.Url (URL). ToString ();Panax Notoginseng             stringstrURL =string. Format ( This. _defaulttagformat, Absurl); -             if(ver) the             { +                 stringFilePath =System.Web.HttpContext.Current.Server.MapPath (URL); A                 stringVernum = This. ComputeSHA1 (FilePath); the                 //if (!string. Isnullorwhitespace (vernum) && vernum.length >) +                 //{ -                 //vernum = vernum.substring (0, +); $                 //} $strURL =string. Format ( This. _vertagformat, Absurl, vernum); -             } -             return Newhtmlstring (strurl); the         } -     }Wuyi      Public classUrlscript:urlcommon the     { -          Public StaticUrlscript Instance =NewUrlscript (); Wu          Public Override string_defaulttagformat -         { About             Get{return "<script src=\ "{0}\" ></script>"; } $         } -          Public Override string_vertagformat -         { -             Get{return "<script src=\ "{0}?v={1}\" ></script>"; } A         } +     } the      Public classUrlstyle:urlcommon -     { $          Public StaticUrlstyle Instance =NewUrlstyle (); the          Public Override string_defaulttagformat the         { the             Get{return "<link href=\ "{0}\" rel=\ "stylesheet\"/>"; } the         } -          Public Override string_vertagformat in         { the             Get{return "<link href=\ "{0}?v={1}\" rel=\ "stylesheet\"/>"; } the         } About     } the}
View Code

Second, the view page to add Smart tips

Iii. Methods of Use

How to add Css/js to the view page

@UrlStyle. Instance.url ("~/css/monther-footer.css")

@UrlScript. Instance.url ("~/scripts/common.js")

Page load Effect

As long as the file changes the value of the SHA1 will change, corresponding to the v=xxxxxxx will be changed, so that the browser will automatically download new files, as long as the file does not modify the local cache can be used, two good ways

JS/CSS file modification After browser local cache resolution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.