I am very new to javascript/jquery. Have written the following script, it works in Chrome 46.0.2490.80 m, but not in Firefox 42.0 or IE 11. Values in the format %<value_name>%
are predefined values from the content management system that I am using.
The script is supposed to hide a button if the boolean value "%asset_metadata_register_button%"
is set to 0
, or for one day after Date variable "eventDate"
has taken place. Otherwise show the Button.
The script also is supposed to change the text and href
value of the button after eventDate
has taken place.
HTML
<p id="show"><a id="change" class="button" href="firstURl">Register</a></p>
Javascript
$(document).ready(function() {
// Boolean value from Show Registration Button metadata field
var number = "%asset_metadata_register_button%";
// variable for start date of event
var eventDate = new Date('%asset_attribute_start_date%');
// variable for date at present time
var now = new Date();
// variable for one day after present time
var oneDay = new Date('%asset_attribute_start_date%');
oneDay.setDate(oneDay.getDate() + 1);
// Hide button if Show Registration Button metadata field is set to no, or for one day after Webinar took place. Otherwise show Button.
if (number == 1) {
if (eventDate < now && now < oneDay) {
document.getElementById("show").innerHTML = "(Webinar will be uploaded shortly)";
} else {
$('#show').show();
}
} else {
$('#show').hide();
}
// changes button text and link after webinar commences
if (eventDate > now) {
document.getElementById("change").innerHTML = "View Webinar";
document.getElementById("change").href = "secondURL";
}
});
As +Jaromanda X mentioned you get an error with certain browsers.
Where your code looks like this:
// variable for start date of event
var eventDate = new Date('%asset_attribute_start_date%');
// variable for date at present time
var now = new Date();
//variable for one day after present time
var oneDay = new Date('%asset_attribute_start_date%');
oneDay.setDate(oneDay.getDate() + 1);
You can replace it with this:
var dateTime = '%asset_attribute_start_date%';
var date = dateTime.substring(0, dateTime.indexOf(" "));
var time = dateTime.substring(dateTime.indexOf(" ")+1);
date = date.substring(date.indexOf("-")+1) + "-" + date.substring(0, date.indexOf("-"));
var eventDate = new Date(date + ' ' + time);
var oneDay = new Date();
oneDay.setDate(eventDate.getDate() + 1);
var now = new Date();
What it does is replace the date format from '2015-11-04' to '11-04-2015'.
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.