I want to redirect my page to another one if I stay there for a certain time. I tried to write the following script and I put it in the head of my webpage but it doesn't work. The location where to move isn't a real url because I'm on XAMPP.
$(document).ready(setTimeout(function () {
window.location.replace("../index.php");
}, 5000););
I want to redirect my page to another one if I stay there for a certain time. I tried to write the following script and I put it in the head of my webpage but it doesn't work. The location where to move isn't a real url because I'm on XAMPP.
$(document).ready(setTimeout(function () {
window.location.replace("../index.php");
}, 5000););
Share
Improve this question
edited Feb 23, 2020 at 14:56
Penny Liu
17.6k5 gold badges86 silver badges108 bronze badges
asked Jun 25, 2018 at 8:22
EnnioEnnio
1931 gold badge2 silver badges11 bronze badges
3
- Even if you are on XAMPP, how can that not be a real URL? – Praveen Kumar Purushothaman Commented Jun 25, 2018 at 8:22
- how can I obtain my url using javascript? – Ennio Commented Jun 25, 2018 at 8:24
-
Using
location.href
? – Praveen Kumar Purushothaman Commented Jun 25, 2018 at 8:25
2 Answers
Reset to default 9The way you have given is totally wrong, causes a Syntax Error. Check your console. The ready()
function expects a function and not an integer (as returned by setTimeout()
).
Try this way:
$(function () {
setTimeout(function() {
window.location.replace("../index.php");
}, 5000);
});
Or if you want to use only after 5 seconds of inactivity, you need to use a different approach by checking the user activity (keypress
, mousemove
) and then clear the timer and restart it.
If you wanna try the redirect after 5 seconds of inactivity, you can do this:
var timer = 0;
function startRedirect() {
timer = setTimeout(function () {
window.location.replace("../index.php");
}, 5000);
}
function restartTimer() {
clearTimeout(timer);
startRedirect();
}
$(function () {
startRedirect();
$(document).mousemove(restartTimer).keyup(restartTimer);
});
You can do it without JS by putting right meta tag into your header
<head>
<meta http-equiv="Refresh" content="5; url=http://google.">
</head>
where "5" is wait timeout.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743884884a4523846.html
评论列表(0条)