user472285
user472285

Reputation: 2674

get url and add to href in jQuery or JS

on a page i have a link

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

I would like to get the current url of the page i'm on and add it to the href.

So if I am on http://mysite.com when I click the link contact above I would go to the page http://mysite.com/_l/R/Contact.aspx?source=http://mysite.com.

Any ideas on how to achieve that in jQuery or JS?

Upvotes: 2

Views: 2279

Answers (3)

Sameera Thilakasiri
Sameera Thilakasiri

Reputation: 9498

Try this fiddle,

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

UPDATED

<!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>

Get this code to your own html page and test, however same code is not working in fiddle, may there is issue in the fiddle in my network.

Upvotes: 3

Gaurav Shah
Gaurav Shah

Reputation: 5279

How about using document.referrer on your next page to get the source url of your previous page

Upvotes: 0

qiao
qiao

Reputation: 18219

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

Upvotes: 0

Related Questions