没有警告页面不是通过jquery重定向

without alert page is not redirecting via jquery

本文关键字:jquery 重定向 警告      更新时间:2023-09-26

我想重定向我的页面到url,这是在我的变量,但它不工作没有警报。

如果我输入alert,它会重定向,否则不会。

$(document).ready(function(){
    $("a").click(function(){
    var newurl = 'http://www.xyz.com/' + $(this).attr('href');
      //$(this).attr(newurl,'href');
      window.location.href = newurl;
       alert(newurl);
    });
});

thanx in advance

锚标记

<a href="includes/footer.jsp">new url</a>

试试下面的

$(document).ready(function () {
    $("a").click(function (event) {
        var newurl = 'http://www.xyz.com/' + $(this).attr('href');
        window.location.href = newurl;
        event.preventDefault()
    });
});

您需要使用preventDefault()来阻止默认事件的传播。浏览器重定向到href之前,jquery有机会改变它。通过使用警告,您延迟了浏览器重定向,因此它看起来有效。

preventDefault添加到事件处理程序中,以防止在链接中跟踪URL:

$(document).ready(function(){
    $("a").click(function(e){
      e.preventDefault();
      var newurl = 'http://www.xyz.com/' + $(this).attr("href");
      window.location.href = newurl;
    });
});