I am tweaking with Twisty's script - Using Jquery UI Datepicker to redirect to URL: Change URL value based on the selected date
I am trying let user click on a date which will be redirect to a page according to the date selected.
If user click on 18 January it should be redirected to datpicker.com/18-01-2022. If the date 22 March is selected it should go to datpicker.com/22-03-2022.
But I am getting this alert - Redirect to undefined, no matter how I tweaked the script. Hence, I would appreciate your help. Thank you.
Pick a date:
<script>
$(function() {
function getDayOfYear(n) {
var s = new Date(n.getFullYear(), 0, 0);
var d = (n - s) + ((s.getTimezoneOffset() - n.getTimezoneOffset()) * 60 * 1000);
var oneDay = 1000 * 60 * 60 * 24;
var day = "00" + Math.floor(d / oneDay);
return day.substr(-3);
}
var targetUrl;
$("#datepicker")
.datepicker({
dateFormat: 'dd-mm-yy',
onSelect: function(dateText) {
datePicked = $.datepicker.parseDate('dd-mm-yy', dateText);
dayPicked = getDayOfYear(datePicked);
targetUrl = "https://datpicker.com/" + dayPicked;
$(this).change();
}
})
.change(function() {
alert("Redirect to " + targetUrl);
//window.location.href = targetUrl;
});
});
</script>
window.open('https://www.abdo-host.com', '_blank');
or
var link = document.createElement("a");
link.href = 'https://www.abdo-host.com';
link.target = '_blank';
var event = new MouseEvent('click', {
'view': window,
'bubbles': false,
'cancelable': true
});
link.dispatchEvent(event);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.