Reload a url in each seconds with jquery - javascript

I want reload a url in each seconds with jquery, i try as following code, this code reloading url only once. How do i do?
setInterval(window.location = $('#thisLink').attr('href'), 1000);
DEMO: http://jsfiddle.net/QBMLm/

If it's your page, you can use this in the head :
<meta http-equiv="refresh" content="1;url=/">
of course, this only works for the page it's embedded in, and won't keep reloading some other external site?

setInterval is not persistent between browser reloads. Also, it takes a function as first argument. You can try something like:
setTimeout(function() {
window.location = $('#thisLink').attr('href');
}, 1000);
It will wait 1sec before redirecting. If the page you are redirecting to have the same code, it will do the same.

Once you re-load the url (window.location change) the context (and scope) of that setInterval become moot (the page is discarded and the next is loaded). The script's then reloaded and setInterval reassigned.
Oh, and syntactically that code is invalid. You probably want to wrap the window.location portion in a function(){}, e.g.
setInterval(function(){
window.location = $('#thisLink').attr('href')
}, 1000);
otherwise it's not actually executing in an interval fashion, but immediately.

Look that these which may help you:
JS setInterval executes only once
setInterval with jQuery.html only updates once?
http://www.google.com/search?q=jquery+setinterval+only+running+once&aq=0&oq=jquery+setinterval+only+running+once&sugexp=chrome,mod=1&sourceid=chrome&ie=UTF-8

It's reloading only once, since once you change window.location you leave your page.
You need to open the link in new named window or embed the child page in an iframe.
setInterval(function() {
window.open($('#thisLink').attr('href'), 'mywindow', '');
});​

Related

Javascript From Console - Load a Few Pages, Then Run Function Per Page Load [duplicate]

I'd like to be able to call a jquery function once window.location has completed loading a URL. Is this possible? I can't seem to find anything online about this.
for instance:
if(parseInt(msg.status)==1) {
window.location=msg.txt;
alert("This URL has finished loading")
}
Thanks,
-Paul
You can either use window.onload of the destination page (if you have access to modify the code of that page), or you can use window.onunload to have the alert be launched when unloading the current page. You cannot execute code on the current page after the new page has been loaded.
Yes.
This page demonstrates onload/onunload behavior.
<html>
<head>
<script>
window.doUnload = function(){
alert("Here!");
}
window.doLoad = function(){
window.location="http://www.google.com";
}
</script>
</head>
<body onload="doLoad();" onunload="doUnload();"></body>
</html>
After a user logs in for the first time I need to load my index page to initialize everything but then need to forward them to another page for profile completion.
use window.location to redirect the user to your index, adding a query parameter (something like window.location=index.php?firstLogin=true ) and on your index redirect (using javascipt http 300, header() or whatever you are using) to the profile page after it ends loading if the parameter is set
Iframe
One (ugly) method you could use is to instead of using window.location, clearing the body, adding an iframe with the relevant path and listening to its onload function.
After that you can run code inside the iframe as long as it's not cross-site scripting.
I use this method to perform small automated scripts, that can't really on third-party plugins.
Ajax
Another method might be using ajax to load the page/body content. Then replacing your body with the newly loaded body and start executing the next functions.

How redirect to homepage with php

I need do a automatic foward/redirect.
If the user dont click anywhere in the site five seconds after automatic foward to homepage... it is possible?
For example,
<meta http-equiv="Refresh" content="5;URL=http://www.teste.com/sv1/index.html">
Thanks
try this in your head:
<script type="text/javascript">
var redirect = setTimeout(function() {
window.location = "http://stackoverflow.com";
}, 5000);
document.onclick = function() {
clearTimeout(redirect);
}
</script>
In PHP it's not possible. You can add timeout in JS and stop it when user click somewhere (onclick event on body).
After that timeout (without clicks), yuo can redirect user by setting document.location.href to your homepage.
Short: No.
Longer: This is not possible with PHP, because PHP is precompiled on the server. So as soon as the user sees the page on his browser, the PHP script already ran through. You will have to use something else instead, for example JavaScript.

How to wait until a web page is loaded with javascript?

I want to change between two pages in html with javascript, but when I change with window.location, the code that is after this sentence continues executing.
So when I do, for example, a call to getElementById() it doesn't recognize the element because the page is still loading.
function myFun(){
// ...
window.location = 'page.html';
// ... wait until page.html is loaded
}
How can I wait until the page is loaded to avoid this problem?
When you do
window.location = 'page.html';
you replace the page in the browser, the one containing the code of myFun, by a new page. There is no way for the code following this instruction to be executed.
If you want to execute it, and only after that change page (but I don't see the point), then you might do
document.onload = function(){ window.location = 'page.html'; };
You can use jQuery document.ready or you can create custom bind like this one. In case you use jQuery.
$(document).ready(function() {
window.location = 'page.html';
});
You can't execute code in your own page after doing window.location = 'page.html';. Your page will be replaced by the new page and the code in the current page will no longer be there.
The only ways to execute some code after page.html is loaded into the current window are as follows:
Load it in an iframe or another window and monitor for when the iframe or window has finished loading. In this way, your current code stays around and is not replaced.
Have some code in page.html that monitors when it finishes loading and trigger your desired action from there.

Refresh Page for interval using js

How can i refresh a page for every one minute using javascript.
Note: I don't have control/option to edit HTML body tag (where we usually call onload function).
Just insert this code anywhere in the page:
<script type="text/javascript">
setTimeout(function(){
location = ''
},60000)
</script>
<script type="text/javascript">
setTimeout(function () {
location.reload();
}, 60 * 1000);
</script>
setTimeout will reload the page after a specified number of milliseconds, hence 60 * 1000 = 1m. Also, since the page is being refreshed, the timeout will always be set on page load.
You do not need to have the code in the body tag. Just add this snippet below and it should work no matter where it is in the page.
<script type="text/javascript">
setInterval('window.location.reload()', 60000);
</script>
As long as you can access the HTML some where and your editor doesn't filter out tags you should be fine. If your editor has a separate area for JavaScript code then just enter setInterval line. :)
Here's the thing mate!
(Point 4 is for this particular question)
1). If you want to reload the same windows over and over again then just execute
window.location.reload()
2). If you want to hard reload from the server then execute
window.location.reload(true)
(basically, just pass true as a boolean arg to the same line of code)
3). If you want to do the same job as point 1 and 2 with a time out. i.e. execute the reload after some time JUST ONCE, then execute
setTimeout("window.location.reload()",10000);
(this should execute on the window after 10 sec. JUST ONCE)
4). If you want to keep reloading the window with a certain timeout then execute
setInterval("window.location.reload()",10000);
(this should execute on the window after 10 sec. with 10 sec. for the interval)
Surely,there're many ways to pass a callback..
setInterval(function(){window.location.reload();},10000);
or
<code>
function call1(){
location.reload(true);
}
setInterval(call1,10000);
</code>
Note:
-Have a look at MDN Guides for [setTimeout][1] and [setInterval][2] functions.
-Using the window object is optional but good to be used. (window is a global object and already available to your current window.)
If you don't want to edit the page, here's the trick. Open the console and write the below-mentioned snippet.
INTERVAL = 5 // seconds
STOP_AFTER = 15 // seconds
// Open the same link in the new tab
win1 = window.open(location.href);
// At every 5 seconds, reload the page
timer1 = setInterval(() => {
win1.location.reload();
console.log("Refreshed");
},INTERVAL*1000)
// Stop reloading after 15 seconds
setTimeout(() => clearInterval(timer1), STOP_AFTER*1000)
Since you want to reload it, you can not simply write location.reload() since the console will be cleared once it is reloaded.
Therefore, it will open a new tab with the same link. It will be easily able to control the 2nd tab using the console of the 1st tab.
When your URL has parameters, it seems that using location = '' doesn't work in IE8. The page reloads without any parameters.
The following code works for me :
<script type="text/javascript">
setTimeout(function(){
window.location.href = window.location.href;
},10000)
</script>

something similar to this without using javascript?

The script below is pretty basic I guess, it starts loading something "on blur".
<script>
window.onblur = function(){
TIMER = setTimeout(changeItUp, 5000);
}
window.onfocus = function(){
if(TIMER) clearTimeout(TIMER);
}
function changeItUp()
{
location.href = "http://www.yahoo.com"
}
</script>
So, if I want to redirect the user to something else after a certain period of inactivity, is there any chance to do this without using Javascript?
thanks.
Short answer: No
Long answer: The only way to detect user activities on a page is via Javascript. No javascript, no keyup/keydown/focus/blur events to trigger on. You could do a redirect after 5 seconds using a <meta> tag redirect, but that's an unconditional redirect. No matter what the user is doing on the page, it'd still redirect after the specified time is elapsed.
No. HTML alone does not have the ability to redirect users as a certain time period has elapsed (conditionally). You will have to use javascript.
Meta Refresh will do something similar but only after a period of time regardless of activity...
<meta http-equiv="refresh" content="5;url=http://yahoo.com/">
Otherwise, like others stated, only JavaScript.

Categories

Resources