I want to refresh the whole page when javascript detects a change in json data.
Here is the code i am trying:
var previous = null;
var current = null;
setInterval(function(){
$.getJSON("https://www.mylivepolls.com/json.php?shortURL=0", function(json){
current = JSON.stringify(json);
if(previous && current && previous !== current){
console.log('refresh');
location.reload();
}
previous = current;
});
}, 1000);
When this code is executed, it does not refresh the page.
But when i try to open inspect element on that page, it refreshes!
please check if i am doing something wrong.
Possibly when your debugger has focus, 'location' is being scoped to the frame of the debugger. Try window.location.reload(), instead of simply location.reload().
Related
I'm trying to differentiate between page click and auto reload. Auto reload is done through javascript below.
var pageReload = {
Initialize: function () {
window.setTimeout("location.reload(true);", 60000);
}
pageReload.Initialize();
I'm trying to set a hidden variable in the above code for which I'm trying to check the changed value in Page_PreRender to understand the difference between page click and auto reload.
var hdnReloadType = document.getElementById('<%=hdnReloadType.ClientID%>');
hdnReloadType.value = "1";
The javascript is loaded after PreRender and I'm sure how to proceed.
Any thoughts?
Thank you for all the replies.
Because I'm using asp.net, I was able to capture the previous page click url from Request object and from there I'm determining if this was a page auto reload or button click. The query string reload option is great too but we didn't want to expose that to the user. This solution worked for my case.
Instead of just doing a reload(), you could add a "reloaded" parameter to the URL. If we use something like the replaceUrlParam() function from this answer, we can say:
var pageReload = {
Initialize: function () {
window.setTimeout(
function() {
var url = replaceUrlParam(window.location.href, 'reloaded', '1');
window.location.href = url;
},
60000
);
}
}
pageReload.Initialize();
Now, when the page has been auto-reloaded (and only then), the query string will contain "reloaded=1".
You could always use localStorage to leave a breadcrumb behind when your function reloads the page:
localStorage.setItem( 'page-reloaded', 'true' );
When the page loads, check for the breadcrumb:
var reloaded = localStorage.getItem('page-reloaded') || false;
And then cleanup afterwards:
localStorage.removeItem('page-reloaded');
I would like the code below to execute only once when the website first loads, as at the moment the script executes every time the index page loads and is therefore showing the div every time the user comes back to the index page from within the site.
NB. I only have the code on the index page.
It would be really helpful is somebody could show me the code I need to paste instead of this.
<script type="text/javascript">
function show(target) {
document.getElementById(target).style.display = 'block';
}
function hide(target) {
document.getElementById(target).style.display = 'none';
}
</script>
if ((typeof localStorage !== 'undefined') &&
(localStorage.getItem('yourCodeDescription') === null)) {
// Your code here
console.log('ok');
localStorage.setItem('yourCodeDescription', true);
}
Try something like this to make use of a localStorage.
I'm trying to run some small JavaScript code. I need to refresh data on a page (using a certain link) then I want to load that page with the updated data.
I basically have to open a link before opening the page to update the data due to the platform that I'm using.
I quickly wrote some JavaScript and tested it. It works perfectly in Internet Explorer and Google Chrome, but for some reason it seems like it's not running the while loop in Firefox. I'm not sure why it's not working as I looked up the syntax and it seems correct.
var refreshed = false;
while (refreshed != true) {
refreshpage = window.open('https://na10.salesforce.com/dash/dashboardRefresh');
refreshed = true;
refreshpage.close();
}
window.open('/dashboard', '__tab');
I tried using a timeout function for closing the window. But I'm not sure if I'm using it right. It will execute everything - including what's in the loop - but it never closes the window.
Here's the updated code:
var refreshed = false;
var timeOut = setTimeout(function() {
refreshpage.close();
}, 1000);
while (refreshed != true) {
refreshpage = window.open('https://na10.salesforce.com/dash/dashboardRefresh');
refreshed = true;
clearTimeout(timeOut);
}
window.open('/dashboard', '__tab');
From the original post:
Here is what I used to get it to work:
var refreshed = false;
while (refreshed != true) {
refreshpage = window.open('https://na10.salesforce.com/dash/dashboardRefresh');
refreshed = true;
var timeOut = setTimeout(function() {
refreshpage.close();
window.open('/dashboard', '__tab');
}, 500);
}
I call
window.location.reload(false)
in a javascript method to update the page. I have additional javascript calls after this call. Is there a way to know when window.location.reload(false) has completed running before calling the additional javascript calls?
You simply have to provide a function to onload : a reload isn't different from a load.
window.onload = function() { // ...
Your line of code window.location.reload(false) causes the browser to reload the current page - no script after this statement will be executed ....
You could set a cookie on first load - then on subsequent loads check for the existence of the cookie and perform an action ... pseudo code :
onload = check cookie
if cookie is present
run function
else
set cookie
reload
You could check the time on the cookie and choose to execute the function after a period of time (1 hour for example) has passed ....
I use the hashtag to set variables that tells me if the page is reloaded or not. You could do something like this:
// Get the hash of the page
var hashstring = window.location.hash.substring(1);
var found = false;
// Do a hash exist?
if (hashstring.length > 0)
{
// Split the hash by '&'-sign (in case you have more variables in the hash, as I have)
var a = hashstring.split("&");
// Loop through the values
for (i = 0; i < a.length; i++)
{
// Split the string by '=' (key=value format)
var b = a[i].split("=");
// If the key is 'reloaded' (which tells us if the page is reloaded)
if(b[0] == 'reloaded')
{
found = true;
}
}
}
if(!found)
{
location.hash = 'reloaded=true';
window.location.reload();
}
// Do other stuff, this will only be executed if the page has been reloaded
I've put the code that finds a variable in the hash in a seperate function in my project, but fot simplicity I just added it here above. This makes it possible to determine if the page has been reloaded, and run code only if it has.
I have an array of two urls that I read and feed into an iframe src value using:
document.getElementById("iframe").src = unescape(dashboard.url);
Immediately after this line, I issue an alert(document.getElementById("iframe").src), which all works fine.
My problem is, is that the first URL in the array seems to load correctly and the alert display the correct URL being used by the iframe.
But when the second URL comes around to be processed and fed into the iframe src value, the alert displays the correct URL, but my iframe which is on the same page is not being refreshed with the correct URL and so still displays the first URL in my array that worked previously.
Am I missing something - is this an IE6 bug of some sort as from what I can see, the iframe second time around is not being called.
I noticed this by putting an onLoad="alert('Test');" in my iframe and it only appeared once and NOT the second time for the second URL.
Any ideas?
By the way, I am using a setTimeout() call to cycle through the arrays.
Thanks.
See it in action. (works x-browser)
<iframe id="rotator" src="http://...first"></iframe>
<script>
// start when the page is loaded
window.onload = function() {
var urls = [
"http://...first",
"http://...second",
// ....
"http://...tenth" // no ,!!
];
var index = 1;
var el = document.getElementById("rotator");
setTimeout(function rotate() {
if ( index === urls.length ) {
index = 0;
}
el.src = urls[index];
index = index + 1;
// continue rotating iframes
setTimeout(rotate, 5000);
}, 5000); // 5000ms = 5s
};
</script>