How do I redirect to another webpage?
One does not simply redirect using jQuery
jQuery is not necessary, and window.location.replace(...)
will best simulate an HTTP redirect.
window.location.replace(...)
is better than using window.location.href
, because replace()
does not keep the originating page in the session history, meaning the user won't get stuck in a never-ending back-button fiasco.
If you want to simulate someone clicking on a link, use
location.href
If you want to simulate an HTTP redirect, use location.replace
For example:
// similar behavior as an HTTP redirect
window.location.replace("http://stackoverflow.com");
// similar behavior as clicking on a link
window.location.href = "http://stackoverflow.com";
How do I redirect to another webpage?
One does not simply redirect using jQuery
jQuery is not necessary, and window.location.replace(...)
will best simulate an HTTP redirect.
window.location.replace(...)
is better than using window.location.href
, because replace()
does not keep the originating page in the session history, meaning the user won't get stuck in a never-ending back-button fiasco.
If you want to simulate someone clicking on a link, use
location.href
If you want to simulate an HTTP redirect, use location.replace
For example:
// similar behavior as an HTTP redirect
window.location.replace("http://stackoverflow.com");
// similar behavior as clicking on a link
window.location.href = "http://stackoverflow.com";
How do I redirect with JavaScript?
To redirect to another page, you can use:
window.location = "http://www.yoururl.com";
Redirect from an HTML page
Try using:
<meta http-equiv="refresh" content="0; url=http://example.com/" />
Note: Place it in the <head>
section.
Additionally for older browsers if you add a quick link in case it doesn't refresh correctly:
<p><a href="http://example.com/">Redirect</a></p>
Will appear as
Redirect
This will still allow you to get to where you're going with an additional click.
How to redirect to another page with a button?
It's normal, you should use the a
tag.
Example:
<div class = "btn-bot">
<a href="{{ url_for('home') }}">
<button class="button" type="button" role="button">Return</button>
</a>
</div>
How can I make a button redirect my page to another page?
Just add an onclick
event to the button
:
<button onclick="location.href = 'www.yoursite.com';" id="myButton" class="float-left submit-button" >Home</button>
But you shouldn't really have it inline like that, instead, put it in a JS block and give the button
an ID:
<button id="myButton" class="float-left submit-button" >Home</button>
<script type="text/javascript">
document.getElementById("myButton").onclick = function () {
location.href = "www.yoursite.com";
};
</script>
Related Topics
Uploading 'Canvas' Image Data to the Server
JavaScript At Bottom/Top of Web Page
Angularjs - Ng-Disabled Not Working For Anchor Tag
How to Get Query String Value from Script Path
How to Refer to JavaScript Variables Across Webpages in a Browser Session
Html5 Dragleave Fired When Hovering a Child Element
Does Ie8 Out-Of-The-Box Have Support For 'Localstorage'
PHP+Js: How to Do Fileuploads in HTML Form as Content-Type Multipart (Via Js)
How to Make an HTML Text Box Show a Hint When Empty
How to Prevent JavaScript Injection Attacks Within User-Generated Html
Angular: Conditional Class With *Ngclass
Jquery Svg, Why Can't I Addclass
Want to Add "Addeventlistener" on Multiple Elements With Same Class
Input Value Is a String Instead of a Number
How to Set Background Color of HTML Element Using CSS Properties in JavaScript