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.
Related
How can i make redirect without clicking.
i want to do it automatically
<script>
$("#Div3").click(function(){
window.location.replace("http://your.next.page/");
});
</script>
If you want to redirect as soon as the page is loaded, you can use
$( document ).ready(function() {
window.location.replace("http://your.next.page/");
});
or you can use window.location.href to simulate a mouse click, whereas .replace simulates a HTTP redirect from the server, if that matters to you.
you can add in the header :
<head>
<meta http-equiv = "refresh" content = "3; url = http://your.next.page/" />
</head>
source
You can use window.location.replace("http://your.next.page/");
directly so that the page is reloaded as soon as the javascript code is read.
I have a web form which contains a submit button which updates the database.
After clicking the button I need to display a message and go back to the previous page after 2 seconds.
Regardless on technologies you use, you can simply store "back link" in the URL as one of the GET parameters and then use this URL to redirect user back to the required page. For example, your link might look like this:
/action/send/?backlink=/path/to/previous/page/
Of course, this link should be URL encoded and will look like this:
/action/send/?backlink=%2Fpath%2Fto%2Fprevious%2Fpage%2F
You can also send this link using POST method, alongside with the query for the database you might send. After the link being passed to the backend, you can place this link as a destination for redirect on the page with the message you want to show. You can use redirect by <meta> tag in HTML document like this:
<meta http-equiv="refresh" content="5;url=/path/to/previous/page/" />
5 — Delay, amount of seconds before refresh will be fired;
url=/path/to/previous/page/ — Path for redirection.
As another solution, you can write JavaScript code which will do the same thing. Minimal implementation will look like this:
<script>
// Wait for page to load
window.addEventListener('load', function() {
// Set the timer for redirection
setTimeout(
function () {
location.href = '/path/to/previous/page/';
},
// Amount of milliseconds before refresh triggered
5000
);
});
</script>
Response.AppendHeader("Refresh", "2;url=page.aspx");
this works.
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.
I was wondering if it is possible to stop javascript running on a page when a link is clicked. The problem i am having is the pages refreshes every 30 seconds (A list needs to be up to date on the site) But if a user clicks a link from the list and before that has finished loading the 30 second refresh begins, it stops the link from opening and instead refreshes the page.
Here is the script that is running,
<script type="text/javascript">
window.name = "NewRegistration";
setTimeout("location.reload(true);",30000);
</script>
The link is to another page on the site.
If anything is not clear please ask.
Thanks,
Jack.
<script type="text/javascript">
var id;
function stopRefresh()
{
window.clearTimeout(id);
}
window.name = "NewRegistration";
id = window.setTimeout("location.reload(true);",30000);
</script>
Some Link
If you're using jQuery, this is much easier to apply to all the links on the page:
<script type="text/javascript">
$(document).ready(function() {
$('a').click(function() {
stopRefresh();
});
});
</script>
You don't have to stop all the javascript on the Page.
You have to work on your refreshing page function. For example you can set a flag to false when a link is clicked.
After 30sec
if (flag == false) do not refresh the page
else
refresh the page
That's all :)
The most common way of doing this these days is to update in the background without reloading the entire page, though that's a bit more work to set up. Read up on Ajax to learn more.
A solution that is simpler to implement is to... well, Sean Bright already beat me to that just now I see ;)
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.