好的,出于某种原因,您想使用JavaScript重定向网页。 您可以使用jQuery或纯JavaScript来完成这项工作。 如果需要即时重定向,则可能是后者。
使用纯JavaScript重定向
不需要jQuery,并且window.location.replace(…)将最好地模拟HTTP重定向。
// simulates similar behavior as an HTTP redirect
window.location.replace("http://jquery4u.com");
// simulates similar behavior as clicking on a link
window.location.href = "http://jquery4u.com";
使用jQuery重定向
var url = "http://jquery4u.com";
$(location).attr('href',url);
它比使用window.location.href =更好,因为replace()不会将原始页面放在会话历史记录中,这意味着用户不会陷入永无休止的后退按钮惨败中。 如果要模拟某人单击链接,请使用location.href(适用于所有浏览器)。 如果要模拟HTTP重定向,请使用location.replace。