How can I make the browser see CSS and Javascript changes?


Thunder:

CSS and Javascript files don't change very often, so I want them to be cached by the web browser. But I also want the web browser to see the changes made to these files without the user clearing their browser cache. There is also a need for a solution that works well with version control systems such as Subversion.


Some solutions I've seen involve adding a version number to the end of the file in the form of a query string.

This can be done automatically for you using the SVN revision number: ASP.NET display SVN revision number

Can you specify how to include the Revision variable of another file ? i.e. in the HTML file, I can include the revision number in the URL of the CSS or Javascript file.

In the Subversion book, it talks about revisions: "This keyword describes the last known revision of this file that was changed in the repository".

Firefox also allows pressing CTRL+ Rto reload all content on a specific page.

To be clear, I'm looking for a solution that doesn't require the user to do anything themselves.

Thunder:

I found that if you append the file's last modified timestamp to the end of the URL, the browser will request the file when it is modified. For example in PHP:

function urlmtime($url) {
   $parsed_url = parse_url($url);
   $path = $parsed_url['path'];

   if ($path[0] == "/") {
       $filename = $_SERVER['DOCUMENT_ROOT'] . "/" . $path;
   } else {
       $filename = $path;
   }

   if (!file_exists($filename)) {
       // If not a file then use the current time
       $lastModified = date('YmdHis');
   } else {
       $lastModified = date('YmdHis', filemtime($filename));
   }

   if (strpos($url, '?') === false) {
       $url .= '?ts=' . $lastModified;
   } else {
       $url .= '&ts=' . $lastModified;
   }

   return $url;
}

function include_css($css_url, $media='all') {
   // According to Yahoo, using link allows for progressive 
   // rendering in IE where as @import url($css_url) does not
   echo '<link rel="stylesheet" type="text/css" media="' .
        $media . '" href="' . urlmtime($css_url) . '">'."\n";
}

function include_javascript($javascript_url) {
   echo '<script type="text/javascript" src="' . urlmtime($javascript_url) .
        '"></script>'."\n";
}

Related


How can I make the browser see CSS and Javascript changes?

Thunder: CSS and Javascript files don't change very often, so I want them to be cached by the web browser. But I also want the web browser to see the changes made to these files without the user clearing their browser cache. There is also a need for a solution

How can I make the browser see CSS and Javascript changes?

thunder: CSS and Javascript files don't change very often, so I want them to be cached by the web browser. But I also want the web browser to see the changes made to these files without the user clearing their browser cache. There is also a need for a solution

How can I make the browser show my code changes immediately

lyla l00 I am working on a web application and using wampserver localhost on chrome browser to display the interface. When I change the styles of webpages, they don't show up in my browser right away, but if I try the same code the next day, it works. Sometime

How can I make the browser show my code changes immediately

lyla l00 I am working on a web application and using wampserver localhost on chrome browser to display the interface. When I change the styles of webpages, they don't show up in my browser right away, but if I try the same code the next day, it works. Sometime

How can I make the browser show my code changes immediately

lyla l00 I am working on a web application and using wampserver localhost on chrome browser to display the interface. When I change the styles of webpages, they don't show up in my browser right away, but if I try the same code the next day, it works. Sometime

How can I make the browser show my code changes immediately

lyla l00 I am working on a web application and using wampserver localhost on chrome browser to display the interface. When I change the styles of webpages, they don't show up in my browser right away, but if I try the same code the next day, it works. Sometime

How can I make this box in CSS (please see my image)?

Munira In my divbox I have one bodyand headerpart. I want to give this headerpart a separate border which must be inside the div box and not have any padding. should indeed look like my given image: Here is what I have tried: HTML: <div id="story"> <p>

How can I make this box in CSS (please see my image)?

Munira In my divbox I have one bodyand headerpart. I want to give this headerpart a separate border which must be inside the div box and not have any padding. should indeed look like my given image: Here is what I have tried: HTML: <div id="story"> <p>

Users won't see CSS/JavaScript changes immediately in the browser

Yusuf I am developing a web application using Spring MVC and Hibernate and deploying it in Apache Tomcat 8. My users are using my app normally using Google Chrome, but when I make changes in CSS or JavaScript and redeploy the changes into tomcat, the users don

How can I see changes in a file?

Jon Cage: I have a log file that is being written by another process and I want to monitor it for changes. Every time there is a change, I want to read in new data to do some processing on it. What is the best way? I'm hoping to get some sort of pull from the

How can I see changes in a file?

Jon Cage: I have a log file that is being written by another process and I want to monitor it for changes. Every time there is a change, I want to read in new data to do some processing on it. What is the best way? I'm hoping to get some sort of pull from the

How can I see changes in a file?

Jon Cage: I have a log file that is being written by another process and I want to monitor it for changes. Every time there is a change, I want to read in new data to do some processing on it. What is the best way? I'm hoping to get some sort of pull from the

How can I see the changes I made in a merge commit

Podolski In git, how can I see the changes made by me only in the last merge commit ? Seeing that only the lines have changed is me, not someone else. Usually when I push but there are conflicts, I merge and resolve some conflicts. After resolving them, I woul

How can I see the changes I made in a merge commit

Podolski In git, how can I see the changes made by me only in the last merge commit ? Seeing that only the lines have changed is me, not someone else. Usually when I push but there are conflicts, I merge and resolve some conflicts. After resolving them, I woul

How can I make the browser refresh automatically?

learner one How to make the browser automatically refresh web pages. In Silverstripe CMS I have to pass in the URL appending ? flush to manually refresh the web page. Does anyone know? Jonh Doe Read this http://php.net/manual/en/function.flush.php this is the

How can I make the browser refresh automatically?

learner one How to make the browser automatically refresh web pages. In Silverstripe CMS I have to pass in the URL appending ? flush to manually refresh the web page. Does anyone know? Jonh Doe Read this http://php.net/manual/en/function.flush.php this is the

How can I see changes in a file before committing to git?

Timothy T. I've noticed that while processing a ticket or two, if I walk away, I'm not sure what I'm doing, what's changed, etc. Is there a way to see the changes made to a given file before git add and then git commit? Cascabel you are looking for git diff --

How can I compare dictionaries to see what changes?

Nishant Singh I have 3 dictionaries in my python code: self.new_port_dict = {}# Dictionary to store the new port from curr_host self.old_port_dict = {}# Dictionary to store old ports in old_host self.results_ports_dict = {}# save results for changed/newly adde

How can I see app changes on the device in real time?

Johnny Every time I change the code, even if it's only one line, I have to run the command sudo ionic cordova prepare ios And I have to click the play button on Xcode to upload the app to the iPhone and see the actual changes. Basically, I repeat this step e

How can I see changes on my SQL server in Visual Studio?

you are ardato I created a new database called "staj" and imported 3 excel files into it. I edited the column names and arranged the primary key. After that, I opened a new web form in Visual Studio and added the database to the project using Entity Framework.