// similar behavior as an HTTP redirectwindow.location.replace("http://stackoverflow.com");
// similar behavior as clicking on a linkwindow.location.href = "http://stackoverflow.com";
简单的跨浏览器测试解决方案(回退到Internet Explorer 9+和所有其他浏览器的window.location.href)
用法:redirect('anotherpage.aspx');
function redirect (url) {var ua = navigator.userAgent.toLowerCase(),isIE = ua.indexOf('msie') !== -1,version = parseInt(ua.substr(4, 2), 10);
// Internet Explorer 8 and lowerif (isIE && version < 9) {var link = document.createElement('a');link.href = url;document.body.appendChild(link);link.click();}
// All other browsers can use the standard window.location.href (they don't lose HTTP_REFERER like Internet Explorer 8 & lower does)else {window.location.href = url;}}
<!-- Place this snippet right after opening the head tag to make it work properly -->
<!-- This code is licensed under GNU GPL v3 --><!-- You are allowed to freely copy, distribute and use this code, but removing author credit is strictly prohibited --><!-- Generated by http://insider.zone/tools/client-side-url-redirect-generator/ -->
<!-- REDIRECTING STARTS --><link rel="canonical" href="https://yourdomain.example/"/><noscript><meta http-equiv="refresh" content="0;URL=https://yourdomain.example/"></noscript><!--[if lt IE 9]><script type="text/javascript">var IE_fix=true;</script><![endif]--><script type="text/javascript">var url = "https://yourdomain.example/";if(typeof IE_fix != "undefined") // IE8 and lower fix to pass the http referer{document.write("redirecting..."); // Don't remove this line or appendChild() will fail because it is called before document.onload to make the redirect as fast as possible. Nobody will see this text, it is only a tech fix.var referLink = document.createElement("a");referLink.href = url;document.body.appendChild(referLink);referLink.click();}else { window.location.replace(url); } // All other browsers</script><!-- Credit goes to http://insider.zone/ --><!-- REDIRECTING ENDS -->
// window.locationwindow.location.replace('http://www.example.com')window.location.assign('http://www.example.com')window.location.href = 'http://www.example.com'document.location.href = '/path'
// window.historywindow.history.back()window.history.go(-1)
// window.navigate; ONLY for old versions of Internet Explorerwindow.navigate('top.jsp')
// Probably no buenoself.location = 'http://www.example.com';top.location = 'http://www.example.com';
// jQuery$(location).attr('href','http://www.example.com')$(window).attr('location','http://www.example.com')$(location).prop('href', 'http://www.example.com')
window.location.replace('yourPage.aspx');// If you're on root and redirection page is also on the root
window.location.replace(window.location.host + '/subDirectory/yourPage.aspx');
// If you're in sub directory and redirection page is also in some other sub directory.
<!DOCTYPE html><html><head><title>example</title></head><body><p>You will be redirected to google shortly.</p><script>setTimeout(function(){window.location.href="http://www.google.com"; // The URL that will be redirected too.}, 3000); // The bigger the number the longer the delay.</script></body></html>
不同的选择如下:
window.location.href="url"; // Simulates normal navigation to a new pagewindow.location.replace("url"); // Removes current URL from history and replaces it with a new URLwindow.location.assign("url"); // Adds new URL to the history stack and redirects to the new URL
window.history.back(); // Simulates a back button clickwindow.history.go(-1); // Simulates a back button clickwindow.history.back(-1); // Simulates a back button clickwindow.navigate("page.html"); // Same as window.location="url"
<script>function Redirect(){window.location="http://www.adarshkr.com";}
document.write("You will be redirected to a new page in 10 seconds.");setTimeout('Redirect()', 10000);</script>
你也可以这样做,点击一个按钮使用location.assign:
<input type="button" value="Load new document" onclick="newPage()"><script>function newPage() {window.location.assign("http://www.adarshkr.com")}</script>
<!DOCTYPE html><html><head><title>JavaScript and jQuery example to redirect a page or URL </title></head><body><div id="redirect"><h2>Redirecting to another page</h2></div>
<script src="scripts/jquery-1.6.2.min.js"></script><script>// JavaScript code to redirect a URLwindow.location.replace("http://stackoverflow.com");// window.location.replace('http://code.shouttoday.com');
// Another way to redirect page using JavaScript
// window.location.assign('http://code.shouttoday.com');// window.location.href = 'http://code.shouttoday.com';// document.location.href = '/relativePath';
//jQuery code to redirect a page or URL$(document).ready(function(){//var url = "http://code.shouttoday.com";//$(location).attr('href',url);// $(window).attr('location',url)//$(location).prop('href', url)});</script></body></html>
<script type="text/javascript">$(function () {//It's similar to HTTP redirectwindow.location.replace("http://www.Technomark.in");
//It's similar to clicking on a linkwindow.location.href = "Http://www.Technomark.in";})</script>
// Form with stepsdocument.getElementById('#next').onclick = function() {window.location.href='/step2' // Iteration of steps;}
// Go to next stepdocument.getElementById('#back').onclick = function() {window.history.back();}
// Finishdocument.getElementById('#finish').onclick = function() {window.location.href = '/success';}
// On success pagewindow.onload = function() {setTimeout(function() {window.location.replace('/home'); // I can't go back to success page by pressing the back button},3000);}
window.location.href; // Returns the href (URL) of the current pagewindow.location.hostname; // Returns the domain name of the web hostwindow.location.pathname; // Returns the path and filename of the current pagewindow.location.protocol; // Returns the web protocol used (http: or https:)window.location.assign; // Loads a new documentwindow.location.replace; // RReplace the current location with new one.