• How To?
  • Tips ‘n Tricks
  • WordPress
  • Snippets
  • Software
    • Browsers
    • Downloads
  • Web
  • Tools
    • Character Counter
    • chmod Calculator
    • Entities Encoder
    • Live HTML Editor
    • My IP
  • Contact
Twitter Facebook Google+ RSS
You are here: SumTips » Tips 'n Tricks » Force Refresh CSS, JavaScript & Any Static File in Browser Cache

Force Refresh CSS, JavaScript & Any Static File in Browser Cache

Posted on May 20, 2012 by Renji | Short URL: http://sumtips.com/?p=6611

Web browsers automatically save webpage stylesheets, JavaScripts, and other static files such as images and favicon on a user’s computer. This is called caching. Caching saves user time and bandwidth by not downloading the same files over and over again. It’s a great feature, but it also pose a big problem when a website makes some changes to a file. For instance, the webmaster may change some styles or JavaScript. Since the filename generally remains the same, a browser has no way of knowing it has been updated and uses old cached version of the files instead. To avoid this, browser needs to be notified when a file has been modified. Here are some ways to do so:

Manual Method

The simplest way of informing browser about a file modification is to change its name. This can be done by adding “ver” parameter to the filename. You can then increment the version number each time you want to force download a file. You can do it like this:

Force cached stylesheet to refresh:

<link href="http://sumtips.com/style.css?ver=2" rel="stylesheet" type="text/css" media="screen" />

Force cached JavaScript to refresh:

<script src="http://sumtips.com/script.js?ver=2" type="text/javascript"></script>

Force cached static files to refresh:

<link rel="shortcut icon" href="http://sumtips.com/favicon.ico?ver=2" />

You can add anything after the ‘?’ to force refresh files.

Automatically Using PHP

Updating version info manually each time may become tedious. To automate the process, we can use PHP to add the parameter:

<link href="http://sumtips.com/style.css?ver=<?php echo(filemtime("style.css")); ?>" rel="stylesheet" type="text/css" />
<script src="http://sumtips.com/script.js?ver=<?php echo(filemtime("script.js")); ?>" type="text/javascript" ></script>
<link rel="shortcut icon" href="http://sumtips.com/favicon.ico?ver=<?php echo(filemtime("favicon.ico")); ?>" />

Now whenever the file is changed on the server, its modified time is added to filename. This lets the browser know that the file has changed and it downloads a new copy.

WordPress

On WordPress, you can add the time() function to the code that adds the stylesheet and JavaScript files to your theme:

wp_enqueue_style( 'theme_style', 'style.css', '', time() );
wp_enqueue_script( 'theme_script', 'script.js', '', time() );

via WPEngineer

Hope it helps!


Tweet

Related posts:

  • Funny Google Image Search with JavaScript
  • Shares Files Browser-to-Browser without Limits
  • Increase WordPress File Upload Size Limit on Shared Hosts
  • Identify Unknown File Types with Smart File Advisor
  • Add Create Folder List, Copy File/Folder Name to Explore Context Menu
Categories: Tips 'n Tricks | Tags: CSS, JavaScript, WordPress
Give Your Site Visitors a Quick Print Preview Option, Ctrl+P Hotkey
Advanced Windows Search Filters and Query Operators

One Response to “Force Refresh CSS, JavaScript & Any Static File in Browser Cache”

  1. Web Development Company says:
    May 27, 2012 at 10:17 AM

    We’re a group of volunteers and opening a new scheme in our community. Your website provided us with valuable info to work on. You’ve done a formidable job and our whole community will be thankful to you.

  • Get Updates via Email

  • Recent Posts

    • Android: Get Chat Heads-like Notifications for all Apps
    • CMS2CMS: Migrate Site from Drupal or Joomla to WordPress
    • WordPress: Add Preview Button in Distraction Free Writing
    • How to Activate or Deactivate Individual Jetpack Modules
    • Windows 8: Auto Update Defender with Windows Update Disabled
    • Automatically Start and Close Programs at Specific Time
  • Random Posts

    • FreeCompose: Easily Type Special Characters on a Regular Keyboard
    • How to Create Your Own Free RSS Feed Translation Service
    • Winamp App for Android Now Available
    • Tag Editor For AVI, WMV, MPC, ASF, MP3, AVS, & More: TigoTago
    • Easily Save and Restore Ubuntu Panels
    • WinX DVD Ripper Platinum 6.0 [Halloween Giveaway]
  • Categories

    • Blogging
    • Games
    • Google
    • How To?
    • Linux
    • Microsoft
      • Windows
    • Miscellaneous
    • Phone
    • Snippets
      • AutoHotkey Snippets
      • CSS Snippets
      • htaccess Snippets
      • JavaScript Snippets
      • PowerShell Snippets
      • WordPress Snippets
    • Social
    • Software
      • Browsers
    • Tips 'n Tricks
    • Wallpapers
    • Web
© SumTips. Contact | Sitemap | Privacy Policy