使用Javascript [no cache]强制在Chrome中重新加载页面

时间:2012-05-23 12:02:50

标签: javascript google-chrome safari webkit browser-cache

我需要使用JavaScript重新加载页面,并确保它不会从浏览器缓存中提取,而是从服务器重新加载页面。 [由于页面元素在过渡期间会发生变化]

在IE和FF上,我发现以下代码工作正常;

window.location.reload(true);

但它无法在Chrome或Safari上使用。

我尝试了以下内容,但也无济于事;

window.location.replace(location.href);
document.location.reload(true);
document.location.replace(location.href);

这个问题有解决方案吗?

首饰

在研究之后我发现这个问题是HTTP协议处理;

  1. Chrome使用Pragma: no-cache HTTP字段
  2. 发送请求
  3. 服务器以Last-Modified: DATE1字段
  4. 响应
  5. JS使用location.reload(true)强制从服务器重新加载而不是缓存
  6. Chrome会使用If-Modified-Since: DATE1字段
  7. 发送请求
  8. 服务器以HTTP Status 304 Not Modified
  9. 回复

    服务器应用程序因未注意到动态页面内容中的状态更改而出错,因此未返回200。 但是,Chrome / WebKit是唯一一个在调用JS If-Modified-Since时发送location.reload(true)字段的浏览器。

    我想我会把我的发现放在这里以防其他人遇到同样的问题。

5 个答案:

答案 0 :(得分:9)

你可以使用这个黑客:

 $.ajax({
        url: window.location.href,
        headers: {
            "Pragma": "no-cache",
            "Expires": -1,
            "Cache-Control": "no-cache"
        }
    }).done(function () {
        window.location.reload(true);
    });

答案 1 :(得分:5)

为确保不从缓存加载页面,您可以添加一些唯一的数字进行查询:

window.location = location.href + '?upd=' + 123456;

您也可以使用日期代替123456

答案 2 :(得分:1)

很棒的发现!我刚遇到同样的问题,这真的很有帮助! 但是,除了您的发现之外,Chrome似乎总是发送一个针对location.reload()的GET请求...... IE / FF正在重复上一个请求。

答案 3 :(得分:1)

我这样做是为了确保我的应用程序文件在chrome上强制重新加载:

<?php

// Session is started.
session_start();

// Name of the template file.
$template_file = 'couples-template.php';

// Root folder if working in subdirectory. Name is up to you ut must match with server's folder.
$base_path = '/couple/';

// Path to the directory where you store the "couples-template.php" file.
$template_path = '../template/';

// Path to the directory where php will store the auto-generated couple's pages.
$couples_path = '../couples/';

// Posted data.
$data['groom-name'] = str_replace(' ', '', $_POST['groom-name']);
$data['bride-name'] = str_replace(' ', '', $_POST['bride-name']);
// $data['groom-surname'] = $_POST['groom-surname'];
// $data['bride-surname'] = $_POST['bride-surname'];
$data['wedding-date'] = $_POST['wedding-date'];
$data['email'] = $_POST['email'];
$data['code'] = str_replace(array('/', '-', ' '), '', $_POST['wedding-date']).strtoupper(substr($data['groom-name'], 0, 1)).urlencode('&').strtoupper(substr($data['bride-name'], 0, 1));

// Data array (Should match with data above's order).
$placeholders = array('{groom-name}', '{bride-name}', '{wedding-date}', '{email}', '{code}');

// Get the couples-template.php as a string.
$template = file_get_contents($template_path.$template_file);

// Fills the template.
$new_file = str_replace($placeholders, $data, $template);

// Generates couple's URL and makes it frendly and lowercase.
$couples_url = str_replace(' ', '', strtolower($data['groom-name'].'-'.$data['bride-name'].'.php'));

// Save file into couples directory.
$fp = fopen($couples_path.$couples_url, 'w');
fwrite($fp, $new_file);
fclose($fp);

// Set the variables to pass them to success page.
$_SESSION['couples_url'] = $couples_url;
// If working in root directory.
$_SESSION['couples_path'] = str_replace('.', '', $couples_path);
// If working in a sub directory.
//$_SESSION['couples_path'] = substr_replace($base_path, '', -1).str_replace('.', '',$couples_path);

header('Location: success.php');

?>

答案 4 :(得分:0)

尝试window.location = window.location