Javascript works in Chrome, not firefox or ie

I am very new to javascript / jquery. I wrote the following script, it works in Chrome 46.0.2490.80 m, but not in Firefox 42.0 or IE 11. Values ​​in the %<value_name>% format are predefined values ​​from the content management system I use.

The script is supposed to hide the button if the logical value "%asset_metadata_register_button%" set to 0 , or one day after the Date variable "eventDate" . Otherwise, press the button.

The script should also change the text and href value of the button after eventDate .

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"; } }); 
+7
javascript jquery
source share
1 answer

As + Jaromanda X mentioned that you get an error message with some browsers.

Where your code is as follows:

 // 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 the following:

 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 he does is change the date format from '2015-11-04' to '11-04-04-2015 '.

0
source share

All Articles