I want to be able to click on a link running on a page on my local computer (not on a server) that automatically suffixes today's date to the link in question. I've seen another discussion on this site (How to insert today's date into a URL?) as to how to put today's date into a URL. However, I can't get any of the code in that stack to work on my page. What exactly do I need to put into the head (all of it, please; assume I know nothing) and how do I format the link so that a link of the form "some.domain.com/?today's_date=" gets today's date added after the = in the form yyyy-mm-dd? Thanks, all.
Asked
Active
Viewed 2,718 times
-1
-
you may want to refer to javascript ajax http get method. – Mox May 20 '15 at 03:29
1 Answers
0
$(function(){
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
if(dd<10){
dd='0'+dd
}
if(mm<10){
mm='0'+mm
}
var today = yyyy+'-'+mm+'-'+dd;
$('a.link').each(function() {
var url = $(this).attr('href');
if (url.indexOf("?") >= 0) {
$(this).attr("href",url+"&feature_date="+today);
} else {
$(this).attr("href",url+"?feature_date="+today);
}
});
});
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<big> <big> <a class="link" style="font-family: Helvetica,Arial,sans-serif; font-weight: bold;" target="_blank" href="http://www.gocomics.com/">GoComics.com</a> </big> </big>
<a class="link" style="font-family: Helvetica,Arial,sans-serif; font-weight: bold;" target="_blank" href="http://www.gocomics.com/2cowsandachicken">(Two
Cows and a Chicken)</a><big><big>
<br style="font-family: Helvetica,Arial,sans-serif; font-weight: bold;">
<a class="link" style="font-family: Helvetica,Arial,sans-serif; font-weight: bold;"
target="_blank"
href="http://www.oregonlive.com/comics-kingdom/">Comics Kingdom Favorites</a>
<br>
</big></big><a class="link"
style="font-family: Helvetica,Arial,sans-serif; font-weight: bold;"
target="_blank" href="http://www.oregonlive.com/comics-kingdom/?feature_id=Beetle_Bailey">Beetle
Bailey (Oregon Live)</a><big><big>
<br
style="font-family: Helvetica,Arial,sans-serif; font-weight: bold;">
<a class="link" style="font-family: Helvetica,Arial,sans-serif; font-weight: bold;"
target="_blank" href="http://www.dilbert.com/">Dilbert</a>
<br>
Try this one:
Html code:
<HTML>
<HEAD>
<TITLE>Your Title Here</TITLE>
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
</HEAD>
<BODY BGCOLOR="FFFFFF">
<a class="link" target="_blank" href="http://www.gocomics.com/">GoComics.com</a>
<a class="link" target="_blank" href="http://www.gocomics.com/2cowsandachicken">(Two
Cows and a Chicken)</a>
<a class="link" target="_blank" href="http://www.oregonlive.com/comics-kingdom/">Comics Kingdom Favorites</a>
<a class="link" target="_blank" href="http://www.oregonlive.com/comics-kingdom/?feature_id=Beetle_Bailey">Beetle
Bailey (Oregon Live)</a>
<a class="link" target="_blank" href="http://www.dilbert.com/">Dilbert</a>
</BODY>
</HTML>
Css code :
.link {
font-family: Helvetica, Arial, sans-serif;
font-weight: bold;
}
Js code:
$(function(){
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
if(dd<10){
dd='0'+dd
}
if(mm<10){
mm='0'+mm
}
var today = yyyy+'-'+mm+'-'+dd;
$('a.link').each(function() {
var url = $(this).attr('href');
if (url.indexOf("?") >= 0) {
$(this).attr("href",url+"&feature_date="+today);
} else {
$(this).attr("href",url+"?feature_date="+today);
}
});
});
Example: click here

RSKMR
- 1,812
- 5
- 32
- 73
-
When I click on your example link, the resultant page throws the string "?today_date=2015-05-21" twice, which I'm pretty sure won't do what I want it to. And when I plug your code into my page, the date doesn't insert into the URL. Not sure what's going on. But this seems to be heading in the right direction; it just needs tweaking. – kgm4714 May 21 '15 at 04:38
-
-
-
I checked my code that is not problem in code. they redirected the website. now i updated the example site name. its now working good. check this link : http://jsfiddle.net/uXn2p/140/ – RSKMR May 21 '15 at 06:59
-
I see where the new link does work to append today's date to the URL. But I don't see a js plugin in your code; to what do you refer? And how do you use jsfiddle? Never used it before. – kgm4714 May 23 '15 at 04:17
-
In jsfiddle have option to include the js and other plugin.check the left side under the "Frameworks & Extensions" text. there i loaded the js plugin. if you still you have doubt means please add your code. i will check it. – RSKMR May 23 '15 at 05:09
-
Follow the steps to add your code. change your code in this link http://jsfiddle.net/uXn2p/140/ and click the update link in top mean. then it you will get new url. then send to me that url. i will check it. – RSKMR May 23 '15 at 05:10
-
OK, now I see. Being unfamiliar with jsfiddle, I didn't realize that a plugin was involved. I found code at blog.jquery.com to add the plugin to my page; now that I'm running the plugin, the code does what I wanted it to do. Thank you so much for your help. – kgm4714 May 25 '15 at 19:51
-
OK, spoke too soon. The code now redirects every link to this code; I only needed one link redirected. There's something strange going on when every link on the page gets redirected to the same location, regardless of the fact that the rest of the links are hard-coded. – kgm4714 May 25 '15 at 19:55
-
Hi, If my code is really useful to you means click the tick icon and like that near to my answer. – RSKMR May 26 '15 at 06:34
-
-
Hi, I changed little bit your code and update in this url. check it. http://jsfiddle.net/4erw6L5z/2/ – RSKMR May 27 '15 at 04:42
-
I see what you did above with taking out all the inline styles and using a style tag in the head. What I can't figure out is why your code on jsfiddle appends the date as I desire, yet when I copy and paste your code into my file, all I get is the URL at the intended link (the Comics Kingdom Favorites link). I found this in the head of the Result frame: Does that have anything to do with it? – kgm4714 May 28 '15 at 19:22
-
I think you completely misunderstanding. I removed the inline style because of code clean up. I added the common class for all href( link) and I added the logic in js only. please check the js code. $('a.link').each(function() { var url = $(this).attr('href'); if (url.indexOf("?") >= 0) { $(this).attr("href",url+"&feature_date="+today); } else { $(this).attr("href",url+"?feature_date="+today); } }); – RSKMR Jun 01 '15 at 04:23
-
To borrow a line from "Dr. Strangelove," still negative function. When I mouse over the Comics Kingdom Favorites link, all I get is the link as defined in the tag. I don't get the date appending to it. Are you running your test code from your computer instead of a server of any kind? That was one of the original stipulations, that this code has to run locally, not on a server. – kgm4714 Jun 02 '15 at 17:51
-
RKSMR: Are you there? Still trying to figure out why your code runs on jsfiddle and not on my computer. Hope you see this. – kgm4714 Aug 01 '15 at 02:15
-
OK, we still have a problem. I updated the code to call the latest version of the jquery plug-in; now all the links are having the date code appended to them again. In fact, when I roll over your code on jsfiddle, I see all the links are having the date code appended to them. I need to isolate it to that one link. – kgm4714 Aug 01 '15 at 03:00
-
@kgm4714 - Please check the browser console. You get any error ? I checked in my local its working good. I updated the code. please check it. Its working good. – RSKMR Aug 03 '15 at 04:42
-
Browser console... Don't see anything in my browser labeled as such. (Using a Chromium-based browser.) Suggestions as to where it might hide? And I've been running the file as is without your latest mod; amazingly (and perhaps I'm showing my ignorance of the process), appending the date code to the rest of the links in the file is not affecting those links; they still do what I want them to do. But the one link to which I wanted to append today's date is functioning as I desired. I'll check out your code (same jsfiddle link, I presume?) and see how it's changed compared to what I'm running. TY – kgm4714 Aug 03 '15 at 20:46
-
Sorry. I didn't get you. Add your code in Jsfiddle and list out the what you tried and what you expect. – RSKMR Aug 04 '15 at 05:33
-
For some reason, the code isn't working in jsfiddle at all, much less the way it is on my computer. Is there a more reliable way we can communicate over this code? – kgm4714 Aug 06 '15 at 02:10