javascript bug in firefox with string manipulation - javascript

I have the following function in javascript. It works in every browser except firefox. There seems to be some problems with the substring keyword in this browser.
function EvalMonthYear() {
var RawMonth = $.trim( $('#MonthList').val() );
var SpacePosition = RawMonth.lastIndexOf(' ') + 1;
var TheYear = $.trim(RawMonth.substring(SpacePosition, RawMonth.lenght));
var TheMonth = IndexOfMonth($.trim(RawMonth.substring(0, SpacePosition)));
};
MonthList contains a month/year string such as January 2011 or May 2009. The goal is to fill the variables TheYear and TheMonth so that it works in every browser.
If you've run into this problem and can think of a good solution this would be helpful.
Thanks.

Maybe you want to rewrite
RawMonth.lenght
as
RawMonth.length
Your original code works in Firefox 4b11 for me, so it might be an issue with 3.6. I'm guessing that your typo works in most browsers because RawMonth.lenght is undefined, which is similar to not passing in the argument. (It's a little different, if you inspect the arguments array.)

Try this:
function EvalMonthYear() {
var RawMonth = $.trim( $('#MonthList').val() );
var MonthYear = RawMonth.split(" ");
var TheYear = MonthYear[1];
var TheMonth = MonthYear[0];
};

Related

BIRT: Weird behaviour of parseInt

I am using a report parameter in BIRT.
It a string, which contains the month/year, like: 08/2018
To test the value, I am using the following code. It is located in a Dynamic Text:
var dateStringArray = params["monthYear"].value.split("/");
var date = new Date(parseInt(dateStringArray[1]), parseInt(dateStringArray[0]) - 1, 1);
var t = parseInt(dateStringArray[0]);
t;
If I fill the parameter with 08/2018, I get a NaN, see:
But if I fill the parameter with 07/2018, it is working correctly:
I have tested it with several numbers. It is just not working with 08 and 09. All other number til 10 are working...
This seems to be a weird scenario. Need to raise a bug on this.
But for your workaround, you can make use of the code below in Dynamic Text which is working fine:
var dateStringArray = params["monthYear"].value.split("/");
var monNum;
if (BirtStr.charLength(dateStringArray[0]) == 2)
{monNum = BirtStr.right(dateStringArray[0],1);}
else {monNum = dateStringArray[0];}
monNum;
//var date = new Date(parseInt(dateStringArray[1]), parseInt(dateStringArray[0]) - 1, 1);
var t = parseInt(monNum);
t;

How to determine thousands separator in JavaScript

In order for toLocaleString to work, the browser/JavaScript must know the user's locale and whether or not the specific locale uses "," or "." for a thousands separator. Is it possible to access this data so that we can determine what the thousands separator is?
If not, we can use a function like this...
var thousandsSeparator = (function(){
if (typeof Number.prototype.toLocaleString === 'function') {
var num = 1000;
var numStr = num.toLocaleString();
if (numStr.length == 5) {
return numStr.substr(1, 1);
}
}
return ","; // fall-back
})();
...but it feels like an unnecessary hack.
A little further digging and I found Intl.NumberFormat. I think this is more elegant...
const thousandsSeparator = (function(){
if (typeof Intl !== 'object') {
return ','; // fallback
}
// Get the formatting object for your locale
const numFormat = new Intl.NumberFormat();
// The resolved.pattern will be something like "#,##0.###"
return numFormat.resolved.pattern.substr(1,1);
})();
Or if you really need it ultra-concise...
const thousandsSeparator = (Intl) ? (new Intl.NumberFormat()).resolved.pattern.substr(1,1) : ",";
Compatibility warning (2015):
The Intl object may not be supported in Safari for some reason -- http://caniuse.com/#feat=internationalization -- despite it being part of standard ECMAScript.
While the Intl object may exist in some ECMAScript-standard browsers, the code above will only work in Chrome.
Sadly Firefox 40 and IE 11 currently do not have a resolved property in numFormat.
An elegant cross-browser solution is still out there...
Update (2021):
Intl, and numFormat.resolved may have better browser support in non-Chrome browsers now. See comments for latest information.
Easy, I guess. This should help you to start with this ES6 solution
function getThousandSeparator(locale){
const mapperRE = /(.{1})\d{3}(\D)\d$/,
digitRE = /\d/,
formattedValue = (new Intl.NumberFormat(locale?? navigator.language)).format(1000.1);
let [_,thousand, decimal] = mapperRE.exec(formattedValue) ?? [null,null,null];
//In case the captured position is number it means there's no thousand separator
if(digitRE.test(thousand))
thousand = '';
return {thousand, decimal}
}

Manipulation data on javascript

I have some codes like this
var f = document.frmR5B075;
var str = f.cbo_BilServProvCodeHidden.value;
var afterDash = str.substr(str.indexOf("-") + 1);
And i want put afterDash value back to f.cbo_BilServProvCodeHidden.value. I tried use f.cbo_BilServProvCodeHidden.value = afterDash; seems not working / cannot get the value. Any Solution ? Thanks
f.cbo_BilServProvCodeHidden.value = afterDash seems to work for me.
Here is the demo

firefox crash with angular ( Garbage Collector troll? )

----EDIT the question was to long and to hard to understand------
Here is a working exemple : http://codepen.io/anon/pen/Mwapgb
WARNING : This will make firefox CRASH ! , if you dare, click around 500 time on any div (i suggest to use a tool to simulate it. Rip mouse)
But This http://codepen.io/anon/pen/eNNqde
Wont make firefox crash
What is the difference :
function GetDateFromDatetimePython(stringdate){ // IT CRASH
var partieDate = stringdate.split(' ');
var ymd = partieDate[0].split('-');
var hms = partieDate[1].split(':');
return new Date(Date.UTC(ymd[0],ymd[1]-1,ymd[2],hms[0],hms[1],hms[2],0));
}
function GetDateFromDatetimePython(stringdate){ // IT DON'T
var partieDate = stringdate.split(' ');
var tmp = partieDate[0]; // add this
var tmp2 = partieDate[1]; // add this
var ymd = tmp.split('-'); // change this
var hms = tmp2.split(':'); // change this
return new Date(Date.UTC(ymd[0],ymd[1]-1,ymd[2],hms[0],hms[1],hms[2],0));
}
I'm going crazy. What is wrong with the first practice ?
Ok , it's stupid to make 3 split, i could combined in 1 . Whenever. WHY does this blow up firefox , Why aren't Chrome nor IE affect ?
May be you are trapped into some infinite loop or a process is instantiated which involves heavy processing which drains all the memory available with your browser.

Code works in code tester but not in adressbar

If i input my code here:
http://writecodeonline.com/javascript/
It works as intended, but if i input it in my adressbar with "javascript:" infront the alert box just shows the original string.
What is going wrong?
var string = "Sunshine & Whiskey";
var stringFeedback;
var i = 0;
string = string.replace("&","%26");
do {
stringFeedback = string.search(/[ ]/);
string = string.replace(/[ ]/,"%20");
i += 1;
} while (i < 5);
alert(string);
Edit:
If i input in my Chromium console it works fine, but if i make a bookmark with the same code it doesn't.
Any suggestions on how to fix that?
Try initialising i before the loop:
var i = 0;
do {
stringFeedback = string.search(/[ ]/);
string = string.replace(/[ ]/,"%20");
i += 1;
} while (i < 5);
Whatever, I recommend you use your browser console to test these code snippets.
You can use
encodeURIComponent("Sunshine & Whiskey");
That returns
Sunshine%20%26%20Whiskey
without any loop, it's a native method of javascript that is supported by all Browser.
MDN documentation

Categories