Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error at this line return s.replace(/\([^)]*\)|[\n\t]/g, ' ').replace(/(\s\s+)/g, ' ').trim(); #4547

Closed
chartierpw opened this issue Apr 11, 2018 · 11 comments

Comments

@chartierpw
Copy link

All of a sudden, today, after using moment.js for about a month I now get an error, every time, at line# 2280, return s.replace(/([^)]*)|[\n\t]/g, ' ').replace(/(\s\s+)/g, ' ').trim();, in the moment.js file, I receive the following error:

0x800a01b6 - JavaScript runtime error: Object doesn't support property or method 'trim'

Why, all of a sudden, am I getting this?

@marwahaha
Copy link
Member

How are you using moment? What command are you running to get this error? Where are you running moment?

@chartierpw
Copy link
Author

I'm using moment.js with jQuery in an asp.net application where I'm calculating monthly payments, my code:

                totalmonths = moment(paymentbydate).diff(startdate, 'month') + 1; // add extra month because 1st payment is today and that takes away a month
                var totalamount = ((amountdue / totalmonths) + ((amountdue / totalmonths) * processingfee));

                totalmonths = totalmonths + 1;

                numpayments = totalmonths;
                var weeklyamount = toFixed(amountdue / totalmonths, 2);


                var paymentprocessingfee = toFixed((amountdue / totalmonths) * processingfee, 2);
                var lastpayment = 0;
                totalamount = toFixed(parseFloat(weeklyamount) + parseFloat(paymentprocessingfee), 2); 

                // Add first payment to grid
                if (totalmonths == 1) {
                    $("#<%=GridView_PaymentSchedule.ClientID%>").append("<tr><td>" + todaydate + "</td><td>" + weeklyamount + "</td><td>" + paymentprocessingfee + "</td><td>" + totalamount + "</td></tr>");
                    lastpayment = totalamount;
                } else {
                    $("#<%=GridView_PaymentSchedule.ClientID%>").append("<tr><td>" + todaydate + "</td><td>" + weeklyamount + "</td><td>" + paymentprocessingfee + "</td><td>" + totalamount + "</td></tr>");
                }

                var nextdate = moment(datevalue, 'MM/DD/YYYY').add(1, 'months').format('MM/DD/YYYY');
                var totalpaymentsmade = parseFloat(totalamount) * 2;

                if (totalmonths >= 2) {
                    // Add second payment if number of payments > 2
                    if (totalmonths == 2) {
                        $("#<%=GridView_PaymentSchedule.ClientID%>").append("<tr><td>" + datevalue + "</td><td>" + weeklyamount + "</td><td>" + paymentprocessingfee + "</td><td>" + totalamount + "</td></tr>");
                        lastpayment = totalamount;
                    } else {
                        $("#<%=GridView_PaymentSchedule.ClientID%>").append("<tr><td>" + datevalue + "</td><td>" + weeklyamount + "</td><td>" + paymentprocessingfee + "</td><td>" + totalamount + "</td></tr>");
                    }

                    for (i = 0; i < (totalmonths - 2) ; i++) {

                        totalpaymentsmade = totalpaymentsmade + parseFloat(totalamount); // Total up the next payment
                        var nextpayment = toFixed(amountduewithfee - totalpaymentsmade, 2);
                        if (parseFloat(nextpayment) == 0) {
                            if (moment(nextdate, "MM/DD/YYYY") > moment(paymentbydate, "MM/DD/YYYY")) {
                                $("#<%=GridView_PaymentSchedule.ClientID%>").append("<tr><td>" + nextdate + "</td><td>" + toFixed(amountduewithfee - totalpaymentsmade, 2) + "</td><td>0</td><td>" + toFixed(amountduewithfee - totalpaymentsmade, 2) + "</td></tr>");
                                lastpayment = totalamount;
                                break;
                            } else {
                                $("#<%=GridView_PaymentSchedule.ClientID%>").append("<tr><td>" + nextdate + "</td><td>" + weeklyamount + "</td><td>" + paymentprocessingfee + "</td><td>" + totalamount + "</td></tr>");
                            }
                            lastpayment = totalamount;
                            break;
                        } else if (parseFloat(nextpayment) < 0) {
                            $("#<%=GridView_PaymentSchedule.ClientID%>").append("<tr><td>" + nextdate + "</td><td>" + toFixed(parseFloat(totalamount) - Math.abs(amountduewithfee - totalpaymentsmade), 2) + "</td><td>0</td><td>" + toFixed(parseFloat(totalamount) - Math.abs(amountduewithfee - totalpaymentsmade), 2) + "</td></tr>");
                            lastpayment = toFixed(parseFloat(totalamount) - Math.abs(amountduewithfee - totalpaymentsmade), 2);
                            break;
                        } else {
                            if (moment(nextdate, "MM/DD/YYYY") > moment(paymentbydate, "MM/DD/YYYY")) {
                                $("#<%=GridView_PaymentSchedule.ClientID%>").append("<tr><td>" + nextdate + "</td><td>" + toFixed(amountduewithfee - totalpaymentsmade, 2) + "</td><td>0</td><td>" + toFixed(amountduewithfee - totalpaymentsmade, 2) + "</td></tr>");
                                lastpayment = totalamount;
                                break;
                            } else {
                                $("#<%=GridView_PaymentSchedule.ClientID%>").append("<tr><td>" + nextdate + "</td><td>" + weeklyamount + "</td><td>" + paymentprocessingfee + "</td><td>" + totalamount + "</td></tr>");
                            }
                        }
                        nextdate = moment(nextdate, 'MM/DD/YYYY').add(1, 'months').format('MM/DD/YYYY');
                    }

javascript does not have a trim function, so I edited the moment.js file to remove the .trim on line 2280. My code had been running just fine with no errors when all of a sudden I was getting this error. I had added moment.js to my project, Visual Studios 2013, via NuGet. This code was developed about a month ago and right up until my message had been running just fine. I uninstalled the NuGet packge and downloaded the moment.js file from GitHub and edited the line. Everything works as expected now. I just posted the code that I was executing, there's additional code for weekly intervals and bi-weekly intervals. So some variables set earlier in the code arer missing here. I was testing the payment process (end to end) with a monthly payment when the error occurred.

@ashsearle
Copy link
Contributor

@chartierpw trim() was first defined in the December 2009 version of JavaScript - and it's been in Internet Explorer since IE9.

The line using trim() in moment was added 9 months ago, and has been in all releases since moment 2.19.0

You say you installed moment via NuGet, it's used in an ASP.NET application, and it was running just fine.

It seems likely that you (or someone else) has updated the NuGet packages, run the application on an older platform (with an older JS runtime), or, you've only just encountered a situation where you're using/parsing RFC2822 dates.

What O/S are you running this application on?

@chartierpw
Copy link
Author

chartierpw commented Apr 16, 2018

Wow ... not running on Windows 7 in IE v11. I add the .trim() back in the moment.js and the script fails, take it out and it runs.. I literally ran the code just fine on one run and then the very next run, 10 seconds later, it failed. I had made no changes to my javascript (jQuery) code, only a procedural change in the process I was testing. I use Visual Studios 2013 v12.0.40629.00 Update 5, JQuery JavaScript Library v1.11.2. The code fails on this line in my code:

totalmonths = moment(paymentbydate).diff(startdate, 'month') + 1;

paymentbydate = "07/23/2018"

Still fails this morning, line 2280 in moment.js, with the error:

0x800a01b6 - JavaScript runtime error: Object doesn't support property or method 'trim'

So if trim() was added in 2009, and this line in moment.js 9 months ago, why is it now suddenly failing?

@ashsearle
Copy link
Contributor

ashsearle commented Apr 16, 2018

See MSDN

Not supported in the following document modes: Quirks, Internet Explorer 6 standards, Internet Explorer 7 standards, Internet Explorer 8 standards.

Perhaps something else is triggering the page to render in quirks mode instead of standards. You might be able to check this according to stackoverflow doing:

 javascript:window.alert('You are in ' + (document.compatMode==='CSS1Compat'?'Standards':'Quirks') + ' mode.')

(Checking if IE is in one of the backwards compatible standards modes is a bit more complicated - you might try another stackoverflow answer or maybe follow more MSDN info for Compatibility View)

@chartierpw
Copy link
Author

Added that line of code and it reports that I am in Standards mode ....

@ashsearle
Copy link
Contributor

Do you see the Compatibility View icon in the address bar?

Can you add <meta http-equiv="X-UA-Compatible" content="IE=edge"> to your HTML (in the <head>) and see if that helps things?

@chartierpw
Copy link
Author

chartierpw commented Apr 16, 2018

I'm not using the Edge browser, I'm using IE 11 .... I didn't think to test it with Chrome or Firefox, so I did. It fails only with IE v11 11.0.49 (KB4052978), set to update automatically ... strange ....

Setting compatibility mode for localhost did nothing ... still fails at the same line .... seems like the problem is with IE 11.

Haven't tested with Edge yet, my development environment is on Windows 7, so Edge isn't available. I'll try Edge from my Windows 10 machine later ...

I any event, I still cannot use moment.js with the trim() on line 2280 as IE 11 is still widely used ...

@chartierpw
Copy link
Author

Just so you know, the error occurs whether I'm in comparability mode or not. IE 11 v11 11.0.49 (KB4052978) apparently has an issue which will never be fixed by Microsoft as, as far as I know, they have discontinued updates. Correct me if I'm wrong ....

@ashsearle
Copy link
Contributor

I'm not sure about MS approach to updates for IE11. They still seem to push out the occasional random patch (e.g. KB4089187 published a month ago.)

However... moment docs (still) claim to support IE 8, which definitely doesn't have trim() - so we shouldn't be using it.

I've raised PR #4564 to remove it again.

@chartierpw
Copy link
Author

Thanks for you help ...

I tried to test using Edge, but Windows 10 is such a POS, I cannot type anything into the Edge address bar and so far I haven't been able to fix that, so testing with Edge is not going to happen soon, I just don't have time to mess with Windows 10, did I say what a POS Windows 10 is, oh ... yeah .... I did .... :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants