2013-05-01 41 views
0

我想将我的页面重定向到即将到达我的变量的url,但它不工作时没有提示。没有提醒页面没有通过jquery重定向

如果我把警报然后重定向否则不。

$(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); 
    }); 
}); 

提前感谢名单

锚标记

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

您无法阻止默认操作,该操作位于链接中的URL后面。这将优先于手动设置URL(因为默认操作稍后发生并因此覆盖更改)。请参阅http://api.jquery.com/event.preventDefault/。为什么它使用'alert',我不知道。它可能以某种方式取消默认操作。 – 2013-05-01 09:10:59

回答

0

添加preventDefault到事件处理程序,以防止下面的链接网址:

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

thnx buddy ...其工作正常 – supersaiyan 2013-05-01 09:14:54

+0

接受你,因为我喜欢这两种解决方案 – supersaiyan 2013-05-01 09:19:11

1

请尝试以下

$(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有机会改变它。通过使用警报,你延迟浏览器重定向,因此它似乎工作。

+0

thnx buddy..upvote for u – supersaiyan 2013-05-01 09:19:33

+0

thnx哥们现在罚款.. – supersaiyan 2013-05-01 09:20:55