Bojak Posted January 13, 2014 Share Posted January 13, 2014 <!DOCTYPE html > <head> <script> function myFunction() { var currentDate = new Date(); var dateTime = currentDate.getDay() + “/“ + currentDate.getMonth() + “/“ + currentDate.GetFullYear(); } </script> </head> <body> <a href =“myPage.html” onclick="myFunction">test</a> </body> </html> this code isnt posting the date under the link. i cant seem to make the date add at all. the href works though. any suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/285338-show-date-under-link/ Share on other sites More sharing options...
.josh Posted January 13, 2014 Share Posted January 13, 2014 1) You have "smartquotes" in various places of your code (inside your link and your function). It should be regular single or double quotes. Perhaps this was just some copy/paste fiasco to the forums, but if those smartquotes are actually in your code, it's going to throw a js error and not work in general. 2) Your onclick just shows a reference to myFunction. It's not actually calling it. You need to do onclick="myFunction()" to have it actually invoke the function on click. 3) You have a target url in your link's href attribute. Even if you get the date to show under the link, at best you will see it for all of a fraction of a second before you get redirected to myPage.html. If you do not want this to happen, you have to do something to keep it from happening. For example, remove the href attribute, or return false in the onclick to prevent the redirect. 4) There are a number of things wrong with the date methods you are using. 4.a) The most immediate thing is that you have a typo: it should be .getFullYear() not .GetFullYear() (lowercase 'g'). 4.b) The 2nd issue is that the way you have it now, you will get (today) the following: "1/0/2014". The "1" is from your .getDay() method, which returns an integer value for the day of the week. The day of the week starts on Sunday "0". Today is Monday "1". I suspect what you really want is .getDate() which returns the actual date (e.g. "13" for today). 4.c) The 3rd issue is that the "0" portion (the month) also shows that .getMonth() returns the month, starting at 0. So if you want to show the more recognizable "1" for January convention, you need to +1 it. 5) Overall, your function is creating a date object and creating a formatted date based on that date object, but it isn't actually doing anything to display the date anywhere. Here is an example of how to make it display the date under the link. This example also stops the redirect from happening, though you didn't specify what you are trying to make happen overall: <!DOCTYPE html > <head> <script type='text/javascript'> function myFunction() { var currentDate = new Date(); var dateTime = currentDate.getDate() + '/' + (currentDate.getMonth()+1) + '/' + currentDate.getFullYear(); document.getElementById('currentDate').innerHTML = dateTime; } </script> </head> <body> <a href ='myPage.html' onclick="myFunction();return false;">test</a> <br/> <span id='currentDate'></span> </body> </html> So to reiterate the notes: 1) Don't use smartquotes in js. They don't work. Period. 2) The function without the () after it is just a reference. To actually call the function you have to have the () after it. 3) Is situational and depends on what your overall goal is. But in general, "Make the date show under the link when clicked" doesn't really jive with the link redirecting you to its target when it is clicked. So in my example, I added return false; to the onclick to prevent the browser from redirecting to myPage.html when the link is clicked. 4) Understand the date methods and what they actually return. 5) Setting javascript values does not by itself put things on your page. You need to set your page up to actually display it. There are many ways to do this. In this example, I added a "placeholder" span tag under the link, and in the function I change its contents. Quote Link to comment https://forums.phpfreaks.com/topic/285338-show-date-under-link/#findComment-1465111 Share on other sites More sharing options...
Bojak Posted January 13, 2014 Author Share Posted January 13, 2014 would taking out false allow me to go to the link and keep the date under it? i think i may need to set a cookie to save the date as well. just so you know it wasnt a copy and paste job i was typing it out on my phone. thats why i had so many errors. Quote Link to comment https://forums.phpfreaks.com/topic/285338-show-date-under-link/#findComment-1465118 Share on other sites More sharing options...
.josh Posted January 13, 2014 Share Posted January 13, 2014 Yes, taking out the return false; will allow you to go to the link.. however, as mentioned, what will happen is when you click the link, your function will execute, but then the browser is going to redirect to the myPage.html url, since you clicked on a link that has a target url. So at best you will only see it appear for all of a fraction of a second, before you are redirected. Why are you wanting to do this? I don't really see the point in doing this from a usability PoV. I don't really see the point in changing something on the page like that, when it's going to immediately be wiped and new page loaded. Now here's a question.. ARE you wanting it to only show the date onclick, or are you wanting it to show on page load so that it's there underneath the link before the link is actually clicked? I'm just trying to figure out what it is you're actually trying to do here, because as-is, it makes no sense to do this.. especially when you turn around and mention throwing a cookie into the mix which at face value has nothing to do with showing the current date underneath a link.. Quote Link to comment https://forums.phpfreaks.com/topic/285338-show-date-under-link/#findComment-1465121 Share on other sites More sharing options...
Bojak Posted January 13, 2014 Author Share Posted January 13, 2014 well im using the cookie to save the date and time. so when i go back to that page the date is still there. im creating a search engine and i wanted to add this as a feature. Quote Link to comment https://forums.phpfreaks.com/topic/285338-show-date-under-link/#findComment-1465122 Share on other sites More sharing options...
Bojak Posted January 13, 2014 Author Share Posted January 13, 2014 i tried making the cookie i failed badly lol. i shall try again i guess! Quote Link to comment https://forums.phpfreaks.com/topic/285338-show-date-under-link/#findComment-1465130 Share on other sites More sharing options...
Bojak Posted January 14, 2014 Author Share Posted January 14, 2014 (edited) how do you switch it so that its month/day/year ? Edited January 14, 2014 by Bojak Quote Link to comment https://forums.phpfreaks.com/topic/285338-show-date-under-link/#findComment-1465146 Share on other sites More sharing options...
.josh Posted January 14, 2014 Share Posted January 14, 2014 just swap the order in your variable assignment... var dateTime = (currentDate.getMonth()+1) + '/' + currentDate.getDate() + '/' currentDate.getFullYear(); Quote Link to comment https://forums.phpfreaks.com/topic/285338-show-date-under-link/#findComment-1465204 Share on other sites More sharing options...
Bojak Posted January 14, 2014 Author Share Posted January 14, 2014 (edited) <HTML> <head> <script type='text/javascript'> function myFunction() { var currentDate = new Date(); var dateTime = (currentDate.getMonth()+1) + "/" + currentDate.getDate() + "/" + currentDate.getFullYear(); document.write(dateTime); //document.getElementById('currentDate').innerHTML = dateTime; } </script> </head> <body> <div id="showDate" style="display:none"> <script type="text/javascript"> myFunction(); </script> </div> <a href="#" onclick="document.getElementById('showDate').style.display = 'block';"> Show Date </a> </body> </HTML> yeah i experimented and tried that after i posted. it worked beautifully. I guess I am a little OCD when it comes to certain things. I am working setting up a cookie now. do i call myFunction to setup the cookie? im not new to scripting but i still have trouble learning how to do things. i usually break more than i fix things lol. Edited January 14, 2014 by Bojak Quote Link to comment https://forums.phpfreaks.com/topic/285338-show-date-under-link/#findComment-1465255 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.