I wrote this code:
<script type="text/javascript">
$('#updateAssignment').on('click', function(event){
event.preventDefault();
var currentdate = new Date();
var datetime = "Last Sync: " + currentdate.getDate() + "/"
+ (currentdate.getMonth()) + "/"
+ currentdate.getFullYear() + " # "
+ currentdate.getHours() + ":"
+ currentdate.getMinutes() + ":"
+ currentdate.getSeconds();
$('#updateAssignment').text(datetime);
});
</script>
With this html:
<p class="card-text" id="updateAssignment">Last update: <?php echo date('d-m-Y H:i:s')?> <i class="fa fa-refresh"></i></p>
What I want is the following:
I want the date to update when I click on the refresh button without the complete page to refresh. Somehow it doesn't work with this code, I don't have any error's in the console.
Could anyone help me out?
Replace
href="?update=true"
With
href="#"
The previous line makes your browser initiate a GET request, which you don't want.
You can achieve your need at multi ways, below added the one way with your code.
you added event.preventDefault(); so click event prevented.
or you can remove href link as per BeetleJuice suggest.
Html
<p class="card-text" id="updateAssignment">
<span>Last update: <?php echo date('d-m-Y H:i:s')?></span>
<i class="fa fa-refresh"></i>Sync Date
</p>
Scripts
$('#updateAssignment a').on('click', function(event){
event.preventDefault();
var currentdate = new Date();
var datetime = "Last Sync: " + currentdate.getDate() + "/"
+ (currentdate.getMonth()) + "/"
+ currentdate.getFullYear() + " # "
+ currentdate.getHours() + ":"
+ currentdate.getMinutes() + ":"
+ currentdate.getSeconds();
$('#updateAssignment span').text(datetime);
});
DEMO
This works :
$('#updateAssignment').click(function(){
var currentdate = new Date();
var datetime = "Last Sync: " + currentdate.getDate() + "/"
+ (currentdate.getMonth()) + "/"
+ currentdate.getFullYear() + " # "
+ currentdate.getHours() + ":"
+ currentdate.getMinutes() + ":"
+ currentdate.getSeconds();
$('#updateAssignment').text(datetime);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p class="card-text" id="updateAssignment">Last update: PHP_HERE <i class="fa fa-refresh"></i></p>
Replace
href="?update=true"
to
href="#null"
and
$(function() {
$('#updateAssignment').on('click', function(event){
event.preventDefault();
var currentdate = new Date();
var datetime = "Last Sync: " + currentdate.getDate() + "/"
+ (currentdate.getMonth()) + "/"
+ currentdate.getFullYear() + " # "
+ currentdate.getHours() + ":"
+ currentdate.getMinutes() + ":"
+ currentdate.getSeconds();
$('#updateAssignment').text(datetime);
});
});
Wrap it by $(function() {
Related
I try to write a bookmarklet which creates email, but there is an issue about using newline character.
When I execute the below code from Chrome Console, it works fine. However, this code doesn't work when executing from bookmarklet. I checked the code and the cause seems var body1 includes newline character(%0D%0A).
Anybody knows how to insert newline character in bookmarklet JS?
javascript:(function(){
var today = new Date();
var url = 'https://mail.google.com/mail/?view=cm';
var to = 'emailfrombookmarklet#example.com';
var subject = '【weather%20report】【' + today.getFullYear() + '-' + ("00" + (today.getMonth() + 1)).slice(-2) + '-' + ("00" + today.getDate()).slice(-2) + '%20bar】%20email%20from%20bookmarklet';
var body1 = 'Hi%2C%0D%0A%0D%0AThis is Kim Kardashian%2E%0D%0AIt%27s%20sunny%20today%2E%0D%0A%0D%0Adate%3A';
var targetDate = '%20' + today.getFullYear() + '-' + ("00" + (today.getMonth() + 1)).slice(-2) + '-' + ("00" + today.getDate()).slice(-2);
var body2 = '%0D%0AName%3A%20Kim%20Kardashian%0D%0A';
var body3 = '%0D%0ABest%20Regards%2C';
url += '&to=' + to + '&su=' + subject + '&body=' + body1 + targetDate + body2 + body3;
window.open(url);})();
It would be better to get rid of using encoded characters at all:
javascript:(function() {
var today = new Date();
var url = 'https://mail.google.com/mail/?view=cm';
var to = 'emailfrombookmarklet#example.com';
var subject = '【weather report】【' + today.getFullYear() + '-' + ('00' + (today.getMonth() + 1)).slice(-2) + '-' + ('00' + today.getDate()).slice(-2) + ' bar】 email from bookmarklet';
var body1 = 'Hi,\n\nThis is Kim Kardashian.\nIt\'s sunny today.\n\ndate:';
var targetDate = ' ' + today.getFullYear() + '-' + ('00' + (today.getMonth() + 1)).slice(-2) + '-' + ('00' + today.getDate()).slice(-2);
var body2 = '\nName: Kim Kardashian\n';
var body3 = '\nBest Regards,';
url += '&to=' + encodeURIComponent(to) + '&su=' + encodeURIComponent(subject) + '&body=' + encodeURIComponent(body1 + targetDate + body2 + body3);
window.open(url);
})();
I found this Javascript script online which refreshes the time every second and displays it. I want the program to display digits with leading zero. Currently it's showing 6/8/2017 - 19:8:54 but I want it to show 06/08/2017 - 19:08:54.
<script type="text/javascript">
function display_c(){
var refresh=1000; // Refresh rate in milli seconds
mytime =setTimeout('display_ct()',refresh)
}
function display_ct() {
var strcount
var x = new Date()
var x1=x.getDate() + "/" + x.getMonth() + "/" + x.getYear();
x1 = x1 + " - " + x.getHours( )+ ":" + x.getMinutes() + ":" +
x.getSeconds();
document.getElementById('ct').innerHTML = x1;
tt=display_c();
}
</script>
You can use the following:
var x1 = ('0' + x.getDate()).slice(-2) + '/'
+ ('0' + (x.getMonth()+1)).slice(-2) + '/'
+ x.getFullYear() + '-'
+ ('0' + x.getHours()).slice(-2) + ':'
+ ('0' + x.getMinutes()).slice(-2) + ':'
+ ('0' + x.getSeconds()).slice(-2);
I have tried many ways mentioned below but none of them is working
var currentTime = Date.parse(date + ' ' + currentDate.getHours() + ':' + currentDate.getMinutes() + ':' + currentDate.getSeconds());
var t = DateTime.Now.ToString("h:mm:ss tt")
DateTime.Now.TimeOfDay etc
thanks in advance
try using this function..
function $dateTime() {
var d = new Date();
var dd = d.getDate();
var mm = d.getMonth()+1;//January is 0!
var yyyy = d.getFullYear();
var h=d.getHours();
var m=d.getMinutes();
var s=d.getSeconds();
if(dd<10){dd='0'+dd}
if(mm<10){mm='0'+mm}
if(h<10){h='0'+h}
if(m<10){m='0'+m}
if(s<10){s='0'+s}
return yyyy + '-' + mm + '-' + dd + ' ' + h + ':' + m + ':' + s;
}
returns the value 2015-01-06 and current time..
I want to prepend this variable "logText" and make it fade in at the same time.
var logText = today + ":" + " " + document.getElementById("textBox").value + '<br>\n';
$("#logContent").prepend(logText);
Here is what I got but it doesn't work.
var logText = today + ":" + " " + document.getElementById("textBox").value + '<br>\n';
$("#logContent").prepend($(logText).fadeIn('slow'));
$("<p>" + today + ": " + $("#textBox").val() + "</p>") // new DOM Node
.css("display", "none") // hide it
.prependTo("#logContent") // prepend it to #logContent
.fadeIn("slow"); // fade it in slowly
fiddle
$("#logContent").prepend(today + ": " + $("#textBox").val() + '<br>').fadeIn('slow');
I am using the following javascript to set a cookie but it sets the expire to session rather than the date specified
var d1 = new Date (),
d2 = new Date ( d1 );
d2.setMinutes ( d1.getMinutes() + timedelay );
document.cookie=itemid + "=" + itemid; + 'expires=' + d2 + '; path=/';
Where am I going wrong here?
You've lost one ;:
document.cookie=itemid + "=" + itemid + ';expires=' + d2 + '; path=/';
syntax seem to be wrong
try
document.cookie=itemid + "=" + itemid + ';expires=' + d2 + '; path=/';