something similar to this without using javascript? - 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.

Related

Javascript code to Click a Button on a Webpage for every 5 Minutes

I have a webpage and it has a Refresh Button. I need to click the button every 5 minutes. The Normal Refresh or Reload or F5 doesn't work in this situation. Is there a way that Javascript can do this task.
Like, I will save the javascript as Bookmark and once I click the bookmark. Then, the javascript event has to click the refresh button every 5 minutes.
I googled it and I found the below code. But, it doesn't work. When I click on it, it just showing a random number in a blank page.
javascript:if(window.autoRefreshInterval) { clearInterval(window.autoRefreshInterval); };
window.autoRefreshInterval = setInterval(function() { jQuery(".refresh").click(); },60000)
thank you in advance,
"I have a webpage and it has a Refresh Button. I need to click the
button every 5 minutes. The Normal Refresh or Reload or F5 doesn't
work in this situation. Is there a way that Javascript can do this
task."
It's not very clear to me, but every time you refresh a webpage, javascript is loaded again. So if you have intervals or variables they are reset at each refresh. If you want to keep some value among refreshs you can store values using localStorage or cookies for example.
If you want refresh automatically page you can use setInterval or metatag "refresh".
"Like, I will save the javascript as Bookmark and once I click the
bookmark. Then, the javascript event has to click the refresh button
every 5 minutes."
Look at this: Add a bookmark that is only javascript, not a URL
you can call your refresh code function or button click event in
setTimeout(yourFucntion(),5000);
else
setTimeout($("#btnName").click(),5000);
Try below code:
<!DOCTYPE html>
<html>
<body onload="f1()">
<script language ="javascript" >
var tmp;
function f1() {
tmp = setInterval(() => f2(), 2000); // replace this with 5 min timer
}
function f2() {
document.getElementById("Button1").click();
}
function f3() {
console.log("Hello World");
}
</script>
<button id="Button1" onclick="f3()">click me</button>
<div id="demo"></div>
</body>
</html>
There are two versions for you to try, one uses javascript to click the button the other automates running the function that they have tied to the button.
Non jQuery:
javascript:(function(){if(window.autoRefreshInterval) {clearInterval(window.autoRefreshInterval);}window.autoRefreshInterval = setInterval(function(){document.getElementsByClassName("refresh")[0].addEventListener('click');},60000);})()
Or with jQuery (OP's comment on original thread):
javascript:(function(){if(window.autoRefreshInterval) {clearInterval(window.autoRefreshInterval);}window.autoRefreshInterval = setInterval(function(){$ctrl.refresh();},60000);})()
Delayed post, but hopefully it helps someone :-).
The trick for me was locating the element by css document.querySelector('.pbi-glyph-refresh').click();
You can combine this with the original code like so, it correctly clicks the PowerBI refresh button on a 60 second timer (the var is in ms).
javascript:if(window.autoRefreshInterval) { clearInterval(window.autoRefreshInterval); };
window.autoRefreshInterval = setInterval(function() {document.querySelector('.pbi-glyph-refresh').click(); },60000)

Is there a better way than meta refresh when connection is lost?

I use often <meta http-equiv="refresh" content="60"> to update my page.
I noticed that this stops, if for any reason the connection to the server get lost for a while.
My first thought was to add a PHP information, when the page will be updated the next time, and a JavaScript variable what time it is now. This would require an action from the user. Can I do that differently?
You can use setTimeout
window.addEventListener('load', reloadPage, false);
function reloadPage() {
// reload the page in 60 seconds
setTimeout(function(){ location.reload(); }, 60000);
}

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.

Reload a url in each seconds with jquery

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', '');
});​

Detecting user inactivity on my website

What's the most simple way to detect user inactivity and ask him if he's there?
Basically, after 3 minutes of the user not clicking on anything on the site, I'd like to ask him if he's there. He click's the OK button of a prompt of sorts and the entire page reloads.
Is there a jQuery script for this?
Here is a jquery plugin "idleTime" that seems designed to do exactly what you need http://paulirish.com/2009/jquery-idletimer-plugin/
Demo
Here is a simple implementation:
$.idleTimer(10000);
$(document).bind("idle.idleTimer", function(){
var reload = confirm("Refresh the Page?");
if (reload){
location.reload(true);
}
else{
window.location = "Log Out Url";
}
});

Categories

Resources