获取url并在jQuery或JS中添加到href

时间:2012-01-06 08:49:46

标签: jquery

在页面上我有一个链接

<a href="/_l/R/Contact.aspx" id="ctl00_SPLinkbutton1"><span class="ms-splinkbutton-text">Contact</span></a>

我想获取我正在打开的页面的当前网址并将其添加到href。

因此,如果我点击上面的链接联系人http://mysite.com,我会转到页面http://mysite.com/_l/R/Contact.aspx?source=http://mysite.com

关于如何在jQuery或JS中实现它的任何想法?

3 个答案:

答案 0 :(得分:3)

试试这个fiddle

$("#ct100_SPLinkbutton1").click(function(e) {  
  e.preventDefault();
  document.location.href = $(this).attr("href") + "?source=" + document.location;
})

<强>已更新

<!DOCTYPE html>
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<a href="/_l/R/Contact.aspx" id="ctl00_SPLinkbutton1">
    <span class="ms-splinkbutton-text">Contact</span>
</a>
<script>
$('a#ctl00_SPLinkbutton1 span').click(function(event) {
    event.preventDefault();
    document.location.href = $(this).attr("href") + "?source=" + document.location;
});
</script>
</body>
</html>

将此代码发送到您自己的html页面并进行测试,但是相同的代码不能在小提琴中工作,可能在我的网络中有小问题。

答案 1 :(得分:0)

$('#ct100_SPLinkbutton1').click(function() {    
  window.location += $(this).attr('href') + '?source=' + window.location;
}

答案 2 :(得分:0)

如何使用 在您的下一页上显示document.referrer以获取上一页的源网址