dougao7801 2013-01-25 21:30
浏览 60

是否可以在加载之前刷新页面?

What if my website was updated, i mean the CSS and some content, and after the update old visitors when comming back on my page the CSS is not auto refreshing, you have to manually refresh the page. Well, visitors are not experts and they will think first that the page is broken or smthing.

How to make a script with Javascript or PHP, any... so that when some one is accessing the page/website, i want the page to be refreshed be fore it will open to the visitor. It will be ok if it will do the job only on the index page.

Is it possible?

I found this script with google help, but its not working, nothing is happen!

<script type='text/javascript'>

(function()
{
  if( window.localStorage )
  {
    if( !localStorage.getItem( 'firstLoad' ) )
    {
      localStorage[ 'firstLoad' ] = true;
      window.location.reload();
    }  
    else
      localStorage.removeItem( 'firstLoad' );
  }
})();

</script>
  • 写回答

2条回答 默认 最新

  • dqhsv0147421 2013-01-25 21:38
    关注

    Your best bet is to "version" your css urls:

    <link type="text/css" rel="stylesheet" href="my_style_sheet.css" />
    

    becomes

    <link type="text/css" rel="stylesheet" href="my_style_sheet.css?v=2.1" />
    

    Any time you update the stylesheet just modify the URLs. You've tagged this question as PHP, so just store the version as a variable or constant in a universally includeed file and it will be really easy:

    <link type="text/css" rel="stylesheet" href="my_style_sheet.css?v=<?=CSS_VERSION;?>" />
    

    If you MUST do it in Javascript, you can have javascript load a new stylesheet that has a unique tag on it (a timestamp, for instance) into the head tag, but this will waste bandwidth and slow down the page load, as the user will end up downloading two stylesheets:

    var css_file=document.createElement("link");
    css_file.setAttribute("rel", "stylesheet");
    css_file.setAttribute("type", "text/css");
    css_file.setAttribute("href", 'link_to_css.php?random_string='+escape(Math.random()));
    document.getElementsByTagName("head")[0].appendChild(css_file);
    

    You can also set short cache-expiration times either in the CSS headers (make your css files .php files that send css headers and also add an expires header:

    <?php header("Expires: [DATE IN PAST OR NEAR FUTURE]"); ?>
    

    Or in your webserver's configuration (in Apache, for instance) you can set default cache headers based on file type, so CSS could be set to have a rapid expiration period. However, once again, your application's performance will suffer slightly, as you're undermining some of the advantages of the user's browser cache.

    评论

报告相同问题?

悬赏问题

  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂