javascript - 强制浏览器忽略缓存

标签 javascript image caching

我的代码裁剪了一张照片。裁剪后的照片会不断更换,但浏览器只会加载第一张裁剪的照片。我已经在网上搜索过,但没有任何效果。我已将随机字符串添加到 php - new.jpg?time=t - 但这会阻止裁剪图像的保存。我已经包括

<head>   
    <meta charset="utf-8">   
    <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
    <meta http-equiv="Pragma" content="no-cache" />
    <meta http-equiv="Expires" content="-1" />
</head>

但浏览器仍然从缓存中加载。随后是 HTML JavaScript 和 PHP。还有其他建议吗?

                <html>
                <head>
                    <title>Image Crop</title>
                    <style>
                        body{
                            margin: 0;
                            padding: 0;
                        }
                        #container{
                            width: 300px;
                        }
                        #box{
                            position: absolute;
                            top: 0px;
                            left: 0px;
                            width: 100px;
                            height: 100px;
                            background: white;
                            border: 2px solid blue;
                            opacity: 0.5;
                        }
                        #crop_button{
                            background: #333;
                            color: #fff;
                            padding: 5px;
                            border: 0px;
                            margin: 5px;
                        }
                        #output{
                            position: absolute;
                            top: 0px;
                            left: 300px;
                        }
                    </style>
                </head>
                <body>
                    <div id="container">
                        <img src="resized_IMG0934.jpg"/>
                        <div id="box"></div>
                    </div>
                    <div id="output"><image />
                    </div>
                    <button id="crop_button">Crop</button>
                    <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
                    <script src="//code.jquery.com/jquery-1.10.2.js"></script>
                    <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
                    <script src="index.js"></script>
                </body>
            </html>

                 $(function() {
                $('#box').draggable({containment: '#container'});
                $('#box').resizable({containment: '#container'});
                $('#crop_button').click(function(){ 
                    var top = $('#box').position().top;
                    var left = $('#box').position().left;
                    var width = $('#box').width();
                    var height = $('#box').height();
                    $.post('crop.php', {top:top, left:left, width:width, height:height}, function(){
                        $('#output').html('<img src="new.jpg"/>');
                    });
                });
             });

                <?php
            $dst_x = 0;
            $dst_y = 0;
            $src_x = $_POST['left'];  //crop Start x
            $src_y = $_POST['top'];  //crop Start y
            $dst_w = $_POST['width']; //Thumb width
            $dst_h = $_POST['height']; //Thumb height
            $src_w = $_POST['width'];  //$src_w + $dst_w
            $src_h = $_POST['height'];  //$src_h + $dst_h

            $dst_image = imagecreatetruecolor($dst_w,$dst_h);
            $src_image = imagecreatefromjpeg("resized_IMG0934.jpg");
            imagecopyresampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
            imagejpeg($dst_image, "new.jpg");

最佳答案

您有以下选择: - 添加以下元标记,这将强制不缓存

<meta http-equiv="pragma" content="no-cache" /> which you have already as I see
  • 此外,如果要加载新版本的css文件,请执行以下操作

< link rel="stylesheet" type="text/css" href="mystyle.css?version={NewVersionOnRequierd}">

  • 图片相同
< img src="picture.jpg?123" alt="">

关于javascript - 强制浏览器忽略缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23734005/

相关文章:

javascript - knockoutjs绑定(bind)不同的API对象

javascript - 自动化批处理脚本 - 在 Photoshop 中将文件名转换为文本

image - 无法使用与灰度图像矩阵的乘法

c++ - 缓存链表——这可能吗?

当对象作为参数从 Angular2 组件传递时,外部 HTML 文件中的 JavaScript 函数不会被调用

c# - Xamarin 中不显示 ImagePicker

Java 图像截断

css - 如果父 css 文件使用查询字符串,@import css 文件是否仍会被缓存

java - org.springframework.data.domain.PageImpl 无法反序列化,当我想使用 findAll(Pageable pageable) 和注释 @Cacheable(spring cache) 时?

javascript - 在 Node.js 代理后面使用虚拟主机运行 Apache