2017-01-09 87 views
1

我知道這個問題已被問了很多次,但我仍然不能得到這個工作。我認爲這將有助於某人根據我的情況提出解決方案。如何防止iframe在Chrome中緩存?

我有一個從數組生成一個隨機視頻隨機video.php頁面,此工作正常,並在Chrome不會緩存:

<?php 
 
$array = [ 
 
    "http://www.mentor-distribution.com/media/Introducing_MConnect.mp4", 
 
    "http://www.mentor-distribution.com/media/hs_freedom_chair.mp4", 
 
\t "http://www.mentor-distribution.com/media/quickstand.mp4", 
 
\t "http://www.mentor-distribution.com/media/MFlex.mp4" 
 
]; 
 
$randomIndex = array_rand($array); 
 
$random = $array[$randomIndex]; 
 
?> 
 

 
<iframe width="320" height="187" frameborder="0" allowfullscreen src="<?php echo htmlspecialchars($random); ?>"></iframe>

這是款嘗試每次顯示隨機視頻的html頁面,但是一旦加載它,直到您清除緩存後纔會更改視頻:

<div class="rightsidevid"><iframe width="320" height="187" src="http://mentor-distribution.com/internal/random-video.php" frameborder="0" scrolling="no" allowfullscreen></iframe></div>

如果有人能幫助它,將不勝感激。我確信你可以通過在網址的末尾添加一個數字來實現這一點(例如url?RandomGenNumber),但我不知道如何做到這一點。

回答

0

不知道這是否適用於Chrome;不過,Firefox也有同樣的問題。我身邊有試過了以下工作在Firefox和它的工作:

<iframe src="webpage.html?var=xxx" id="article"></iframe> 
<script> 
var article = document.getElementById("article"); 
article.contentWindow.location.href = article.src; 
</script> 
0

我得到了它在Chrome通過以下緩存設置設置爲我的.htaccess文件工作。

## 
 
# Expires Headers YSlow START 
 
# Browser caching optimization by Sam Riveros 
 
## 
 

 
<IfModule mod_headers.c> 
 

 
\t # Turn on Expires and set default to 0 
 
\t ExpiresActive On 
 
\t ExpiresDefault A0 
 
    
 
\t # Set up caching on media files for 1 year (forever?) 
 
\t <FilesMatch "\.(ico|pdf|mp4|ogg|webm|svg+xml|ttf|otf|woff|woff2)$"> 
 
\t \t ExpiresDefault A29030400 
 
\t \t Header append Cache-Control "public" 
 
\t </FilesMatch> 
 

 
\t # Set up caching on media files for 1 week 
 
\t <FilesMatch "\.(jpg|jpeg|png)$"> 
 
\t \t ExpiresDefault A604800 
 
\t \t Header append Cache-Control "public" 
 
\t </FilesMatch> 
 
    
 
\t # Set up caching for commonly updated static files for 1 week 
 
\t <FilesMatch "\.(txt|html|js|css)$"> 
 
\t \t ExpiresDefault A604800 
 
\t \t Header append Cache-Control "proxy-revalidate" 
 
\t </FilesMatch> 
 
    
 
\t # Force no caching for dynamic files 
 
\t <FilesMatch "\.(php)$"> 
 
\t \t ExpiresActive Off 
 
\t \t Header set Cache-Control "private, no-cache, no-store, proxy-revalidate, no-transform" 
 
\t \t Header set Pragma "no-cache" 
 
\t </FilesMatch> 
 

 
</IfModule> 
 

 
<IfModule mod_expires.c> 
 

 
\t #Redundancy in case server admin turn mod_headers.c off 
 

 
\t ExpiresActive on 
 
\t ExpiresDefault \t \t \t \t \t \t \t \t \t \t "access plus 1 month" 
 
\t 
 
\t #CSS 
 
\t ExpiresByType text/css \t \t \t \t \t \t \t \t "access plus 1 year" 
 

 
\t #Data interchange 
 
\t ExpiresByType application/json \t \t \t \t \t \t "access plus 0 seconds" 
 
\t ExpiresByType application/xml \t \t \t \t \t \t "access plus 0 seconds" 
 
\t ExpiresByType text/xml \t \t \t \t \t \t \t \t "access plus 0 seconds" 
 
\t 
 
\t #Favicon 
 
\t ExpiresByType image/x-icon \t \t \t \t \t \t \t "access plus 1 year" 
 
\t 
 
\t #HTML components (HTCs) 
 
\t ExpiresByType text/x-component \t \t \t \t \t \t "access plus 1 month" 
 
\t 
 
\t #HTML 
 
\t ExpiresByType text/html \t \t \t \t \t \t \t \t "access plus 1 week" 
 
\t 
 
\t #Javascript 
 
\t ExpiresByType application/javascript \t \t \t \t "access plus 1 year" 
 
\t 
 
\t #Manifest files 
 
\t ExpiresByType application/x-web-app-xanifest+json \t "access plus 0 seconds" 
 
\t ExpiresByType text/oache-xanifest \t \t \t \t \t "access plus 0 seconds" 
 
\t 
 
\t #Media 
 
\t ExpiresByType image/jpg \t \t \t \t \t \t \t \t "access plus 1 week" 
 
\t ExpiresByType image/jpeg \t \t \t \t \t \t \t "access plus 1 week" 
 
\t ExpiresByType image/png \t \t \t \t \t \t \t \t "access plus 1 week" 
 
\t ExpiresByType audio/ogg \t \t \t \t \t \t \t \t "access plus 1 year" 
 
\t ExpiresByType video/mp4 \t \t \t \t \t \t \t \t "access plus 1 year" 
 
\t ExpiresByType video/ogg \t \t \t \t \t \t \t \t "access plus 1 year" 
 
\t ExpiresByType video/webm \t \t \t \t \t \t \t "access plus 1 year" 
 

 
\t #Web feeds 
 
\t ExpiresByType application/atom+xml \t \t \t \t \t "access plus 1 hour" 
 
\t ExpiresByType application/rss+xml \t \t \t \t \t "access plus 1 hour" 
 
\t 
 
\t #Web fonts 
 
\t ExpiresByType image/svg+xml \t \t \t \t \t \t \t "access plus 1 year" 
 
\t ExpiresByType application/x-font-ttf \t \t \t \t "access plus 1 year" 
 
\t ExpiresByType application/x-font-truetype \t \t \t "access plus 1 year" 
 
\t ExpiresByType application/x-font-opentype \t \t \t "access plus 1 year" 
 
\t ExpiresByType application/font-woff \t \t \t \t \t "access plus 1 year" 
 
\t ExpiresByType application/font-woff2 \t \t \t \t "access plus 1 year" 
 
\t ExpiresByType application/vnd.ms-fontobject \t \t \t "access plus 1 year" 
 
\t ExpiresByType application/vnd.xs-fontobject \t \t \t "access plus 1 year" 
 
\t ExpiresByType application/font-sfnt \t \t \t \t \t "access plus 1 year" 
 

 
</IfModule> 
 

 
<IfModule mod_deflate.c> 
 

 
\t # Compress HTML, CSS, JavaScript, Text, XML and fonts 
 
\t AddOutputFilterByType DEFLATE application/javascript 
 
\t AddOutputFilterByType DEFLATE application/rss+xml 
 
\t AddOutputFilterByType DEFLATE application/vnd.ms-fontobject 
 
\t AddOutputFilterByType DEFLATE application/x-font 
 
\t AddOutputFilterByType DEFLATE application/x-font-opentype 
 
\t AddOutputFilterByType DEFLATE application/x-font-otf 
 
\t AddOutputFilterByType DEFLATE application/x-font-truetype 
 
\t AddOutputFilterByType DEFLATE application/x-font-ttf 
 
\t AddOutputFilterByType DEFLATE application/x-javascript 
 
\t AddOutputFilterByType DEFLATE application/xhtml+xml 
 
\t AddOutputFilterByType DEFLATE application/xml 
 
\t AddOutputFilterByType DEFLATE font/opentype 
 
\t AddOutputFilterByType DEFLATE font/otf 
 
\t AddOutputFilterByType DEFLATE font/ttf 
 
\t AddOutputFilterByType DEFLATE image/svg+xml 
 
\t AddOutputFilterByType DEFLATE image/x-icon 
 
\t AddOutputFilterByType DEFLATE text/css 
 
\t AddOutputFilterByType DEFLATE text/html 
 
\t AddOutputFilterByType DEFLATE text/javascript 
 
\t AddOutputFilterByType DEFLATE text/plain 
 
\t AddOutputFilterByType DEFLATE text/xml 
 

 
\t # Remove browser bugs (only needed for really old browsers) 
 
\t BrowserMatch ^Mozilla/4 gzip-only-text/html 
 
\t BrowserMatch ^Mozilla/4\.0[678] no-gzip 
 
\t BrowserMatch \bMSIE !no-gzip !gzip-only-text/html 
 
\t Header append Vary referer 
 

 
</IfModule> 
 

 
## 
 
# Expires Headers YSlow END 
 
##

0

的Expires頭的YSlow START由Sam裏維羅斯是,如果你不已經有一個非常好的事情。這將有助於優化您的網站。然而,唯一需要糾正的問題是頭附加Vary引用。將以下內容添加到.htaccess文件中,並且應該可以正常工作,而不需要其他任何東西。甚至沒有JavaScript的:

<IfModule mod_deflate.c> 
 

 
\t Header append Vary referer 
 

 
</IfModule>