使用jQuery或纯 JavaScript 如何将用户从一页转到另一页?


当前回答

<script type="text/javascript">
var url = "https://yourdomain.com";

// IE8 and lower fix
if (navigator.userAgent.match(/MSIE\s(?!9.0)/))
{
    var referLink = document.createElement("a");
    referLink.href = url;
    document.body.appendChild(referLink);
    referLink.click();
}

// All other browsers
else { window.location.replace(url); }
</script>

其他回答

var url = 'asdf.html';
window.location.href = url;

您可以在脚本部分的按钮点击事件下写入 Url. 动作 。

function onclick() {
    location.href = '@Url.Action("Index", "Home")';
}

使用 JavaScript :

方法1:

window.location.href="http://google.com";

方法2:

window.location.replace("http://google.com");

使用 j 查询 :

方法1:美元(地点)

$(location).attr('href', 'http://google.com');

方法2:可再使用功能

jQuery.fn.redirectTo = function(url){
    window.location.href = url;
}

jQuery(window).redirectTo("http://google.com");

原始问题:“如何使用 jQuery 重新定向?” , 因此, 答案执行 jQuery 补充使用案例 。


重定向到 JavaScript 的页面 :

window.location.href = "/contact/";

或者,如果您需要延迟:

setTimeout(function () {
  window.location.href = "/contact/";
}, 2000);   // Time in milliseconds

jjQuery 允许您从网页中轻松选择元素。 您可以在页面中找到任何您想要的内容, 然后使用 jQuery 添加特殊效果, 对用户动作作出反应, 或者在您选中的元素内或外显示内容并隐藏内容。 所有这些任务都以知情开始如何选择元素或事件.

$('a,img').on('click',function(e){
  e.preventDefault();
  $(this).animate({
    opacity: 0 //Put some CSS animation here
  }, 500);
  setTimeout(function(){
    // OK, finished jQuery staff, let's go redirect
    window.location.href = "/contact/";
  },500);
});

想象一下有人写了一个脚本/ 插件, 包含 10000 行代码 。 如果有 jQuery , 您可以用一两行代码连接到这个代码 。

首先正确写入。 您想要在应用程序中浏览另一个链接, 从应用程序中浏览另一个链接。 以下是代码 :

window.location.href = "http://www.google.com";

如果您想要浏览您应用程序中的页面, 我也有代码, 如果您想要的话 。