How can I reload a page once on page load (an equivalent to pressing F5)
$( window ).load(function() {
window.location.reload(true);
});
This loops itself. How can I make it happen only once?
How can I reload a page once on page load (an equivalent to pressing F5)
$( window ).load(function() {
window.location.reload(true);
});
This loops itself. How can I make it happen only once?
This will do it:
if (window.location.href.indexOf('reload')==-1) {
window.location.replace(window.location.href+'?reload');
}
With your jQuery-Code:
$( window ).load(function() {
if (window.location.href.indexOf('reload')==-1) {
window.location.replace(window.location.href+'?reload');
}
});
Nowadays... HTML5 provides localStorage:
/* if my "reload" var isn't set locally.. getItem will be false */
if (!localStorage.getItem("reload")) {
/* set reload to true and then reload the page */
localStorage.setItem("reload", "true");
location.reload();
}
/* after reloading remove "reload" from localStorage */
else {
localStorage.removeItem("reload");
// localStorage.clear(); // or clear it, instead
}
and this doesn't need jquery. Hope it helps, as it helped me.
<script type="text/javascript">
$(document).ready(function(){
//Check if the current URL contains '#'
if(document.URL.indexOf("#")==-1)
{
// Set the URL to whatever it was plus "#".
url = document.URL+"#";
location = "#";
//Reload the page
location.reload(true);
}
});
</script>
Due to the if condition the page will reload only once.I faced this problem too and when I search ,I found a nice solution. This works for me fine.And thanks for http://www.webdeveloper.com/forum/showthread.php?267853-Auto-refresh-page-once-only-after-first-load
Add below line into your HTML file:
`<input name="refreshed" value="no" id="refreshed" />`<input name="refreshed" value="no" id="refreshed" />
And call this function on onload of page,
<script type="text/javascript">
onload = function() {
if ($("#refreshed").val() == "no") {
location.reload();
$("#refreshed").val("yes");
}
}
</script>
Reload page with some query string for reference.
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
$(window).load(function() {
if(getParameterByName('reloaded')){
//loaded once
}else{
window.location = window.location.href+'?reloaded=true';
}
}
thanks for your help meanwhile i have made it working this way
function SetCookie (name, value) {
var argv=SetCookie.arguments;
var argc=SetCookie.arguments.length;
var expires=(argc > 2) ? argv[2] : null;
var path=(argc > 3) ? argv[3] : null;
var domain=(argc > 4) ? argv[4] : null;
var secure=(argc > 5) ? argv[5] : false;
document.cookie=name+"="+escape(value)+
((expires==null) ? "" : ("; expires="+expires.toGMTString()))+
((path==null) ? "" : ("; path="+path))+
((domain==null) ? "" : ("; domain="+domain))+
((secure==true) ? "; secure" : "");
}
function getCookie(c_name)
{
var c_value = document.cookie;
var c_start = c_value.indexOf(" " + c_name + "=");
if (c_start == -1)
{
c_start = c_value.indexOf(c_name + "=");
}
if (c_start == -1)
{
c_value = null;
}
else
{
c_start = c_value.indexOf("=", c_start) + 1;
var c_end = c_value.indexOf(";", c_start);
if (c_end == -1)
{
c_end = c_value.length;
}
c_value = unescape(c_value.substring(c_start,c_end));
}
return c_value;
}
if (getCookie('first_load'))
{
if (getCookie('first_load')==true)
{
window.location.reload(true); // force refresh page-liste
SetCookie("first_load",false);
}
}
SetCookie("first_load",true);
There is an easier way (worked in my case)
If you are using jQuery, set an global variable in JS:
<script type="text/javascript">
var only_once = "0";
</script>
Then use this JS code in your jQuery page (inside <div data-role="page" id="myPage">)
<script>
$('#myPage').bind('pageshow', function() {
if (only_once==1) {
only_once = 0 ;
location.reload();
}
});
</script>
I seem to have solved a similar problem with github static pages. I wanted to force reload of pages (ie no caching) since the caching was causing broken links.
The trouble with that was I could not use meta http-equiv OR htaccess to change caching since github overrides these settings.
None of the "reload only once" solutions would work across the back button.
The following seems to work on all browsers:
<body onbeforeunload="return window.location.reload(true)">
This forces a reload directly from the server whenever the user leaves a page (same effect as no caching). The dreaded loop is avoided.