What could be causing this compatibility issue between IE and Chrome? It's strange that the Table is displaying correctly only in Chrome

Recently, I encountered a challenge with my code in Chrome and realized that it also needs to work in Internet Explorer. Can someone assist me in making it fully functional in IE?

I suspect that there might be specific code adjustments needed for IE as the table formatting seems off and rows are not aligning properly every 7 items.

<div id="calendar"></div>
<script>
    function calendar(month) {

        //Variables to be used later.  Place holders right now.
        var padding = "";
        var totalFeb = "";
        var i = 1;
        var testing = "";

        var current = new Date();
        var cmonth = current.getMonth(); // current (today) month
        var day = current.getDate();
        var year = current.getFullYear();
        var tempMonth = month + 1; //+1; //Used to match up the current month with the correct start date.
        var prevMonth = month - 1;

        //Determing if Feb has 28 or 29 days in it.  
        if (month == 1) {
            if ((year % 100 !== 0) && (year % 4 === 0) || (year % 400 === 0)) {
                totalFeb = 29;
            } else {
                totalFeb = 28;
            }
        }

        // Setting up arrays for the name of the months, days, and the number of days in the month.
        var monthNames = ["Jan", "Feb", "March", "April", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec"];
        var dayNames = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thrusday", "Friday", "Saturday"];
        var totalDays = ["31", "" + totalFeb + "", "31", "30", "31", "30", "31", "31", "30", "31", "30", "31"];

        // Temp values to get the number of days in current month, previous month. Also getting the day of the week.
        var tempDate = new Date(tempMonth + ' 1 ,' + year);
        var tempweekday = tempDate.getDay();
        var tempweekday2 = tempweekday;
        var dayAmount = totalDays[month];

        // Padding the other days for the first week with the previous months days based on the first day of the current month.
        while (tempweekday > 0) {
            padding += "<td class='premonth'></td>";
            tempweekday--;
        }
        // Filling in the calendar with the current month days in the correct locations.
        while (i <= dayAmount) {
            if (tempweekday2 > 6) {
                tempweekday2 = 0;
                padding += "</tr><tr>";
            }
            if (i == day && month == cmonth) {
                padding += "<td class='currentday("+ i + ")'   onMouseOver='this.style.background=\"#00AA00\"; this.style.color=\"#FFFFFF\"' onMouseOut='this.style.background=\"aqua\"; this.style.color=\"black\"'>" + i + "</td>";
            } else {
                padding += "<td id='currentmonth("+ i +")' class='currentmonth' onclick='DaySelection(" + i + ");' onMouseOver='this.style.background=\"#00FF00\"' onMouseOut='this.style.background=\"#FFFFFF\"'>" + i + "</td>";
            }
            tempweekday2++;
            i++;
        }

        // Outputting the calendar onto the site along with the month name and days of the week.
        var calendarTable = "<table class='calendar'> <tr class='currentmonth'><th colspan='7'>" + monthNames[month] + " " + year + "</th></tr>";
        calendarTable += "<tr class='weekdays'>  <td>Sun</td>  <td>Mon</td> <td>Tues</td> <td>Wed</td> <td>Thurs</td> <td>Fri</td> <td>Sat</td> </tr>";
        calendarTable += "<tr>";
        calendarTable += padding;
        calendarTable += "</tr></table>";
        document.getElementById("calendar").innerHTML += calendarTable;
    }

    function go12() {
        var my_date = new Date();
        alert(my_date.getMonth());
        var MonthOFYear = my_date.getMonth();
        calendar(MonthOFYear);

    }

    if (window.addEventListener) {
        window.addEventListener('load', go12, false);
    } else if (window.attachEvent) {
        window.attachEvent('onload', go12);
    }

    function DaySelection(a) {
        var selection = document.getElementById('currentmonth(' + a + ')').textContent;
        alert(selection);
    }

</script>

Answer №1

let currentDate = new Date(currentMonth + ' 1 ,' + currentYear)

The line above is causing the issue specifically in Internet Explorer 11 where it is not recognized as a valid date format. This results in subsequent problems within your loop.

 if (currentWeekday > 6) {
     currentWeekday = 0;
     padding += "</tr><tr>";
 }

Since currentWeekday is not being properly evaluated as a number, the condition inside the if statement will never be met and consequently, a new row will not be generated.

To prevent this problem, it is recommended to follow either RFC 2822 or ISO 8601 standards when formatting dates in your code.

To fix this issue in your scenario, you can replace:

let currentDate = new Date(currentMonth + ' 1 ,' + currentYear)

with:

let currentDate = new Date(currentMonth + '-1-' + currentYear)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

I would appreciate your assistance in understanding how to utilize the Array concat() method in my code, and I am

I need help understanding how to use the Array concat() method and how to write pure JavaScript code according to the ECMA-262 standard. Assume O is the object we are working with. Let A be the new array created based on O with a length of 0. Set the ini ...

Assistance requested for integrating Remy Sharp's JQuery Marquee Plugin

I'm currently working on a project to display the latest tweets for a specific hashtag. I came across this example at http://exscale.se/__files/uploads/scrolling-twitter-feed.htm which shows tweets from user timelines rather than using the search api ...

Concealing a div depending on the price variation

I'm looking for a way to dynamically show or hide a div based on the price of selected variations in my online store. Let's take a product with options priced at £359 and £455 as an example. In addition, there is a finance plugin with a minim ...

Utilizing GSAP for crafting a dynamic horizontal scrolling section

I am currently working on creating a unique section that transforms into a horizontal scroller once the items (in this case, images) are visible, and then reverts to a vertical scroller once all the items have been scrolled through. My inspiration and ada ...

Leverage a single property from a CSS class within another CSS class

I am facing an issue where I need to utilize one property from a CSS class in another CSS class. .class_a {margin:10px; width: 360px; float: left; color:#ffffff; ...etc...} .class_b { use the margin property of .class_a } Only utilize the margin propert ...

Is there a way to make the div visible with just one click instead of two?

var sections = ["intro", "content"]; var visibleSectionId = null; function toggleVisibility(sectionId) { if (visibleSectionId == sectionId) { visibleSectionId = null; } else { visibleSectionId = sectionId; } hideInvisibleSe ...

Add to Firebase reference using AngularFire

Imagine I'm constructing a never-ending scroll of articles. The article's ID is obtained from the URL: var id = $stateParams.id; I aim to startAt that specific index in my Firebase collection and restrict it to 10 items: var limit = 10; var a ...

Troubleshooting a Malfunctioning AJAX Request in a WordPress Plugin

After carefully reviewing this post about a jQuery Ajax call in a Wordpress plugin page, I found that it closely matched my current issue. My basic Wordpress plugin is designed to offer a specific membership form that passes payment details to PayPal for p ...

Delete the span element if the password requirements are satisfied

I am implementing password rules using span elements. I aim to dynamically remove each span that displays a rule once the input conditions are met. Currently, I have succeeded in removing the span related to the minimum length requirement but I am unsure h ...

(Spotify Web API) Issue with Creating New Playlist - Received Error 403 (Forbidden) upon POST request

Looking for guidance on creating a new playlist using the Web API? Check out the notes here: When making a POST request to https://api.spotify.com/v1/users/{user_id}/playlists, make sure you include the access token and data with a content type of 'a ...

Searching for a specific data point within the latest entry of a JSON file using Javascript

I am currently in the process of utilizing a sensor to measure temperature, which is then stored in a mongo database. This data can be accessed as a JSON file by visiting ('/data'). My goal is to determine the latest entry and extract the temper ...

Retrieving AJAX Response and Assigning it to a JavaScript Variable

I've dedicated countless hours searching for assistance on how to implement an AJAX call in Javascript to communicate with a PHP script. My goal is to add a value to a table, retrieve the ID of the newly inserted row, and use that ID in a form submis ...

Creating a variable in JavaScript

In my HTML body, I have set up a global variable named index with the value <%var index = 0;%>. This allows me to access it throughout the code. I'm trying to assign the value of $(this).val() to <% index %>, but I can't seem to get ...

Ways to have MongoDB present nested JSON data as an array?

I recently came across some interesting data: { "_id" : ObjectId("5461e16ee7caf96f8f3584a2"), "num_marcacao" : "100", "sexo" : "Fêmea", "idade" : "20", "bigdata" : { "abortos" : [ { "data_aborto" : ...

What could be causing DataTable to insert a row at the beginning of my table?

Similar Question: Is the fnAddAdd() function of jquery datatable plugin used to add rows to the top or bottom of an html table? I am currently working on implementing datatables in my application and encountered an issue when dynamically adding rows. ...

Tips on restricting users to choose dates that are later than the current date

Currently, I am working with Vue3 using the options API. After reviewing this StackBlitz, my question is regarding how to correctly set the :max value for a date-picker. Even though I have assigned :max as new Date(), I am still able to select dates that ...

Reload a forum in a div without refreshing the entire page

I'm interested in finding out if it's possible for a div to reload similar to how a window does. I have a forum set up, but I don't want the entire page to reload after each form submission. Instead, I'm wondering if only the elements w ...

Tips on waiting for Vue component's asynchronous mount to complete before proceeding with testing

In my Vue2 component, I have an asynchronous lifecycle hook method: // create report when component is loading async mounted(): Promise<void> { await this.createReport(); } Now, I want to test my component using jest and vue/test-utils, but the te ...

Benefits of using props destructuring in React - beyond just being a syntactic shortcut

This idea might not be exclusive to React, but I've struggled to discover a compelling reason beyond concise and easier-to-read code. ...

"Learn how to securely redirect to a custom URI scheme with a fail-safe option to display alternative content if not supported

In short: Can a visitor be redirected to a custom URI scheme or shown alternate content if the scheme is not supported? My specific scenario involves developing a mobile app that utilizes a custom URI scheme for users to invite others to actions within th ...