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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Feb 2013
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    How can I create a dynamic hyperlink with JavaScript?

    I have the following links that I would like to create a dynamic hyperlink in Java to point to current month and year in the hyperlink.

    http://www.somesite.com/site_media/u...f_jan_2012.pdf

    http://www.somesite.com/site_media/u...f_feb_2012.pdf

    http://www.somesite.com/site_media/u...f_mar_2012.pdf

    http://www.somesite.com/site_media/u...f_apr_2012.pdf

    http://www.somesite.com/site_media/u...f_jan_2013.pdf

    The pdf file name within each hyperlink is based on the month and the year.. jan, feb, mar ..etc and for the year 2012 then 2013.

    How can I create a dynamic hyperlink that will always display the current month and year?

    I appreciate any help.

    Thanks

  • #2
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by Sam97 View Post
    How can I create a dynamic hyperlink that will always display the current month and year?
    Where possible this is a server-side task.

    Insert this where you want the link generated. If JS isn't available, you get nothing:
    Code:
    <script type="text/javascript">
    
    (function( content, title )
    {
     var dt = new Date();
     
     document.write( "<a href='http://www.somesite.com/site_media/u...f_" + ['jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec' ][ dt.getMonth() ] + "_" + dt.getFullYear() + ".pdf' title='" + title +"'>" + content + "<\/a>"); 
    })
    ( "Click me", "Goes to some site" ); /* <-- Link text and link title */
    
    </script>

  • Users who have thanked Logic Ali for this post:

    Sam97 (02-13-2013)

  • #3
    New to the CF scene
    Join Date
    Feb 2013
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts
    This is perfect except for one thing I am trying to display reports for the previous month when adding -1 for the getMonth, and if the current month is Jan it will calculate 0 -1 = -1, and it will not display the previous month which is Dec.

    Thanks

    Code:
    <script type="text/javascript">
    
    (function( content, title )
    {
     var dt = new Date();
     
     document.write( "<a href='http://www.somesite.com/site_media/u...f_" + ['jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec' ][ dt.getMonth() -1 ] + "_" + dt.getFullYear() + ".pdf' title='" + title +"'>" + content + "<\/a>"); 
    })
    ( "Click me", "Goes to some site" ); /* <-- Link text and link title */
    
    </script>

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,318
    Thanks
    203
    Thanked 2,566 Times in 2,544 Posts
    Get the month (and year) of the previous month with:-


    Code:
    <script type="text/javascript">
    
    var months = ['jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec' ];
    var d = new Date();
    d.setDate(1);
    d.setMonth(d.getMonth() - 1);
    var mth = d.getMonth();
    var monthname = months[mth];
    var yr = d.getFullYear();
    alert (mth + "  "  + monthname + "  " + yr);
    
    </script>
    From when I was a baby, I said that I would be the heavyweight world champion. - Dave Haye, BBC website.
    Last edited by Philip M; 02-13-2013 at 12:27 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    Sam97 (02-17-2013)


  •  

    Posting Permissions

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