Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-12-2013, 12:11 PM   PM User | #1
Sam97
New to the CF scene

 
Join Date: Feb 2013
Posts: 2
Thanks: 2
Thanked 0 Times in 0 Posts
Sam97 is an unknown quantity at this point
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
Sam97 is offline   Reply With Quote
Old 02-12-2013, 02:00 PM   PM User | #2
Logic Ali
Regular Coder

 
Logic Ali's Avatar
 
Join Date: Sep 2010
Location: London
Posts: 961
Thanks: 0
Thanked 198 Times in 193 Posts
Logic Ali will become famous soon enoughLogic Ali will become famous soon enough
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>
Logic Ali is offline   Reply With Quote
Users who have thanked Logic Ali for this post:
Sam97 (02-13-2013)
Old 02-13-2013, 05:00 AM   PM User | #3
Sam97
New to the CF scene

 
Join Date: Feb 2013
Posts: 2
Thanks: 2
Thanked 0 Times in 0 Posts
Sam97 is an unknown quantity at this point
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>
Sam97 is offline   Reply With Quote
Old 02-13-2013, 07:44 AM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
__________________

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.

Last edited by Philip M; 02-13-2013 at 11:27 AM..
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
Sam97 (02-17-2013)
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:39 PM.


Advertisement
Log in to turn off these ads.