Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Oct 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    adding javascript to a link

    I have a simple problem. I want to add the current page's url to a link, like this:
    <code>
    <a href="http://www.somesite.com/somepage.aspx?a=">
    </code>
    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/newthrea...=newthread&f=2) or a partial one (example: /newthread.php?do=newthread&f=2).

    Can someone help me with an example?

  • #2
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Try: <a href="http://www.somesite.com/somepage.aspx?a=" onclick='location.href=this.href+window.location.href;return false;'>
    Last edited by Logic Ali; 10-19-2010 at 08:33 PM.

  • #3
    New to the CF scene
    Join Date
    Oct 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

    www.somesite.com/somepage.aspx?a=

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,992
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    Try this:-


    Code:
    <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;
    }
    </script>

  • #5
    New to the CF scene
    Join Date
    Oct 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you Philip! That did it!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •