View Full Version : adding javascript to a link

Oct 19th, 2010, 05:51 PM
I have a simple problem. I want to add the current page's url to a link, like this:
<a href="http://www.somesite.com/somepage.aspx?a=">
After the 'a=' I need to add javascript that will return the current url. It can be the full url (example: http://www.codingforums.com/newthread.php?do=newthread&f=2) or a partial one (example: /newthread.php?do=newthread&f=2).

Can someone help me with an example?

Logic Ali
Oct 19th, 2010, 06:33 PM
Try: <a href="http://www.somesite.com/somepage.aspx?a=" onclick='location.href=this.href+window.location.href;return false;'>

Oct 19th, 2010, 07:05 PM
Thank you. This isn't sending the querystring though.

<a href="http://www.somesite.com/somepage.aspx?a=" onclick='location.href=this.href+window.location.href'>

goes to


Philip M
Oct 19th, 2010, 07:31 PM
Try this:-

<a href="#" onclick="goURL()" return false;>Click here</a>

<script type = "text/javascript">
function goURL() {
var x = window.location.href;
var url = "http://www.somesite.com/somepage.aspx?a=" + x
window.location = url;

Oct 19th, 2010, 08:14 PM
Thank you Philip! That did it!