0

我有一个 asp.net mvc 应用程序,我需要在其中重定向到另一个页面:

<script>
    $(function () {
            $('.Enregistrer').click(function () {
           window.location.href ="www.google.com";
                 });
    });
  </script>

我使用window.location.href, 和尝试重定向,但它在两个不同的浏览器(Chrome && IE)中location.href不起作用location.replacelocation

原因是什么?我该如何解决?

4

2 回答 2

1

试试这个,有些浏览器不理解语言

<script type="text/javascript">
$(function () {
        $('.Enregistrer').click(function () {
       window.location.href ="www.google.com";
             });
});
</script>
于 2013-10-08T12:36:51.150 回答
1

您需要http://在域中包含协议(例如。),否则假定为本地请求:

<script type="text/javascript">
    $(function () {
        $('.Enregistrer').click(function (e) {
            e.preventDefault(); // this may be required
            window.location.assign("http://www.google.com");
        });
    });
</script>

注意我还将type属性添加到script标签中,并使用window.location.assign这些是常见的最佳实践。

此外,如果.Enregistrer是一个a元素,您可能需要包含event.preventDefault以停止正常的链接行为。

于 2013-10-08T12:37:37.480 回答