Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
544 views
in Technique[技术] by (71.8m points)

Reload an HTML page just once using JavaScript

How can I reload an HTML base web page only once? I am using history.go(0); with function onLoad in body tag but i want to run it only once. Please keep in mind that I am using iframe so this is not possible to to use the below types code:

<script language=" JavaScript" ><!--
function MyReload()
{
window.location.reload();
}
//--></script>

<Body onLoad=" MyReload()" > 

The above code did not work for me.

However the code below is working well but the problem is that I need it to load only once:

<BODY BGCOLOR=#FFFFFF background="../images/Index_04.jpg" onLoad="history.go(0)" >

Note: I am using two iframes when user click on main page link a page loads in iframe then I want to reload the whole page with current iframe page.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You could use a querystring at the end of the page url (e.g. ?r), and check for it before redirecting, as if it exists, the redirect is already done.

if(window.location.href.substr(-2) !== "?r") {
  window.location = window.location.href + "?r";
}

Disclaimer: I agree with others that you probably have a better solution - and should never need to refresh 'just once'.

Explanation of use

At the bottom of your page, just above the </body> you should put this:-

<script type="text/javascript">
   if(window.location.href.substr(-2) !== "?r") {
      window.location = window.location.href + "?r";
    }
</script>

That's it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...