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.
Related
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.
Consider the following inline code below:
<h1 id="top">Top Of Page</h1>
...
<a class='reload' onClick='window.location.reload();'>Reload</a>
How would one implement this in a fashion where when the window reloads, it goes to the top of the page of hits the anchor? I tried just doing:
reload to top of page
$(window).on('load', function(){
$('html, body').scrollTop(0);
});
and:
Reload browser does not reset page to top
$(document).ready(function(){
$(this).scrollTop(0);
});
Directly in the related template but it wouldn't load to top? Besides jQuery not being included Why would that be?
Is there a way to do this inline to keep consistent? so something like (which I tried):
<a class='reload' onClick='window.location.reload().scrollTop(0);'>Reload</a>
Which doesn't go to the top of the page, either.
I then thought to do the href of the id I can anchor to:
anchor jumping by using javascript (slightly tweaked to my situation)
<script type="text/javascript">
function goToTop(){
var url = location.href;
location.href = url + "#top";
}
</script>
<a class='reload' onClick='goToTop()'>Reload</a>
In this approach, it just adds "#top" to the URL and nothing else.
I was under the impression what if you change location.href it redirects to the new URL. It says here that "The href property sets or returns the entire URL of the current page." Why won't it set it in the above function?
This seems pretty simple so I'm not understanding what I'm missing?
You just need to add window.location.reload(true) after the line where you set [window.]location.href to your anchor.
Related
Posting answer by #epascarello embedded in comments, above, as it worked for me and is the simplest answer I've seen.
If you don't need/want any animated reload actions, simply use two different events - first to move the current page/scroll location to the top [window.scrollTo(0,0);] and then reload the page, either from the cache [window.location.reload();] or from the server if you've updated any of the needed data [window.location.reload(true);]. So,
// Moving page to top on forced reload in javascript
window.scrollTo(0,0);
window.location.reload(); //or window.location.reload(true) to use new/updated data from the server
Hope this helps others. Thanks, again, #epascarello!
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.
Using Javascript, how would I change the title of a page when a click a specific anchor tag.
Home
About Us
Possibly something like title="" in the anchor tags. Also, if possible, could you have it support history, so that if the back or forward button is pushed, it would change back to what it was.
Any help or suggestions is appreciated.
Revision: I have another plugin that dynamically changes the page, so clicking the links in the above statement doesn't actually change anything in the header.
The current HTML code you provided will actually take the user to a different page upon clicking. So depending on the title attribute of index.html and about.html, it may have different titles than the current page.
If you don't want the page to reload and add an entry into history, you could do something like this:
$('a').click(function(e) {
$('title').text($(this).attr('title'));
history.pushState({}, '', $(this).attr('href'));
e.preventDefault();
});
You can learn more about the .pushState(...) API here: https://developer.mozilla.org/en-US/docs/Web/API/History_API
Check out the History.pushState() method: https://developer.mozilla.org/en-US/docs/Web/API/History/pushState
You can use it to push a new state to the browsers history like so:
var state = { 'page_id': 1, 'user_id': 5 };
var title = 'Hello World';
var url = 'hello-world.html';
history.pushState(state, title, url);
This should change the title and url and allow you to use the browsers back and forward functionality
To use this without requiring any outside libraries like jQuery you could add this to the onclick of the links, for example:
Home
<html>
<head>
<title>sample Page</title>
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script>
$(document).ready(function () {
$("a").click(function (e) {
e.preventDefault()
$('title').text($(this).attr('title'));
window.pushState({}, '', 'about.html');
e.preventDefault();
});
});
</script>
</head>
<body>
Home
About Us
</body>
</html>
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.