PDA

View Full Version : Javascript Code doesnt work in Opera



Crash1hd
Aug 30th, 2003, 05:00 AM
I am having trouble getting this code to work in Opera!




/* Count the number of words in the textarea */

function count_words(x){
var tempstr = trimtext(x.value);
var temparray = tempstr.split(/\s+/);
var arylen = temparray.length;
var detailsfield

alert(arylen + " Words Total");

}



Well it works but I could have 100 words in the field box and the alert says 1 word so it works and it doesnt its like its not adding up the text in the textbox?

beetle
Aug 30th, 2003, 10:34 AM
Shot in the dark: maybe Opera doesn't like the pattern literal?


function count_words(x){
var tempstr = trimtext(x.value);
var pattern = new RegExp( "\\s+" );
var temparray = tempstr.split( pattern );
var arylen = temparray.length;
var detailsfield

alert(arylen + " Words Total");

}And, for clarification, which version of Opera?

liorean
Aug 30th, 2003, 10:44 AM
Heh - I guess, if you want it do match more than once, you better add the global flag. That's what's missing. :)

Crash1hd
Aug 30th, 2003, 12:37 PM
Ok any version of Opera and what global flag??

and the changed code didnt work?

liorean
Aug 30th, 2003, 12:53 PM
Oh, just change /\s+/ to /\s+/g and it should work.

beetle
Aug 30th, 2003, 07:30 PM
Don't think so ... split() already operates across the entire string.

Maybe Opera isn't receiving the string properly (i.e. x.value isn't working) ??

cheesebag
Aug 30th, 2003, 09:34 PM
What is trimtext()?

Crash1hd
Aug 30th, 2003, 10:08 PM
trimtext just trims excess space around words like so

example 1

the dog walks

example 2

the dog walks

the output of example 2 with trim text would be example 1:)


oh and the g in /\s+/g still doesnt work?

liorean
Aug 31st, 2003, 12:02 PM
Well, why don't you alert tempstr and temparray, to see what they really contains? Then we can work it out from there.

Crash1hd
Aug 31st, 2003, 09:32 PM
ok so this is what happens!

if I use the string on all examples below of " One Two Three " removes quotes!

now useing: alert(tempstr);

In Opera I get

One Two Three

and in IE I get

One Two Three

now useing:alert(temparray);

In Opera I get

One Two Three

In IE I get

One,Two,Three

so the difference is the comma!

liorean
Aug 31st, 2003, 09:41 PM
Hmm, what version of op are you using? In op7.11w, I get "one two three".split(/\s+/); to return one,two,three. In your case, it seems like no match has been found.

What happens if you do typeof temparray or temparray instanceof Array?

Crash1hd
Aug 31st, 2003, 10:12 PM
I was trying with 5 and 6

how would you do a typeof temparray? or temparray instanceof Array??

liorean
Aug 31st, 2003, 10:22 PM
Eh - well, alert(typeof temparray); and alert(temparray instanceof Array); should do.

Hmm, 5 and 6, you say? Have you tried it with 7? 7 improved the scripting engine greatly, and moved it up a level in conformacy to the ECMA standard...
It might be that <7 versions aren't very strong at regular expression handling.

Crash1hd
Sep 1st, 2003, 02:50 AM
ok so useing the same one two three

alert(typeof temparray);

gives object

and

alert(temparray instanceof Array);

gives true :)

liorean
Sep 1st, 2003, 10:07 AM
That confirms it. For some reason, the regex doesn't match anything in the string in op5-6. I can't say I understand why, though.