开发者

Refresh Page only time

I write a meta tag for refreshing web page. Now i want to refresh a page only one time. What is the code for refreshing page only one time. Please help me to fix the pro开发者_如何学编程blem...

Thanks in Advance..


Using javascript you could set a cookie with a "refreshed" variable in it and check if it's set, if not then refresh the page. Of course this involves quite a lot of code for setting and reading from the cookie plus the function to be called when you reload.

My approach would be url vars, then again it's php not meta tags, it would be something like this:

<?php 
    if($_GET['r'] != 1) header('refresh: 0; url=/index.php?r=1');
?>

Which reloads the page setting a variable in the url ,in this case r for refreshed, as true. So the next time it loads it will not reload . It works, it's just one line of code and it will save you some coding time and get the job done.

Update: (User wanted it in asp)

Should work but I haven't tried it nor can I try it at the moment (I'm at the airport)

<%
    dim refreshOnce
    refreshOnce = request.querystring("r")

    if refreshOnce <> 1 then  Response.AddHeader "Refresh", "0;URL=/index.php?r=1"
%>


Javascript solution, using a form field to store the state:

<html>
  <head>
    <script language="javascript">
      function init() {
        var form = document.getElementById('theform');
        var input = form.refreshed;
        function reload() {
          location.reload(false);
        }
        function isRefreshed() {
          return !!input.value;
        }
        function doDisplay() {
          var el = document.getElementById(isRefreshed() ? 'two' : 'one');
          el.style.display = 'block';
        }
        function conditionalRefresh() {
          if (!isRefreshed()) {
            input.value = 'true';
            setTimeout(reload, 1000);
          }
        }
        doDisplay();
        conditionalRefresh();
      }
    </script>
  </head>
  <body onload="init();">
    <form id="theform">
      <input type="hidden" name="refreshed" />
    </form>
    <div id="one" style="display: none;">
      one
    </div>
    <div id="two" style="display: none;">
      two
    </div>
  </body>
</html>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜