Strange javascript bug!

David Faure david at mandrakesoft.com
Wed Oct 30 10:52:38 GMT 2002


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Friday 04 October 2002 10:10, Anders Lund wrote:
> Hi,
> 
> I have discovered a really strange javascript bug in konqueror.
> 
> My bank (http://amagerbanken.net) is using a javaapplet for home banking, 
> which is running some javascripts to get values from a stylesheet linked to 
> the document for the applets appearence (the applet is developed by Bankernes 
> EDBCentral, http://www.bec.dk, and is used by many small banks in Denmark, so 
> setting the font, color and other values from style sheets makes sense).
> 
> They use this javascript function:
> function BECReadIE(klasse, element)
> {
>   var s, i;
> 
>   for (s = 0; s < document.styleSheets.length; s++)
>   {
>     for (i = 0; i < document.styleSheets[s].rules.length; i++)
>     {
>       if ( document.styleSheets[s].rules.item(i).selectorText == klasse )
>       {
>         return eval("document.styleSheets[s].rules.item(i).style." + element);
>       }
>     }
>   }
>   return "undefined";
> }
> 
> which gets called by the applets during initialization.
> 
> If the styles are defined in a style tag in the document header this works 
> fine, but if the styles are in a linked stylesheet it does not work.
> 
> However, if I put an alert in the function, like this:
> function BECReadIE(klasse, element)
> {
>   var s, i;
>   
>   for (s = 0; s < document.styleSheets.length; s++)
>   {
>     for (i = 0; i < document.styleSheets[s].rules.length; i++)
>     {
>       if ( document.styleSheets[s].rules.item(i).selectorText == klasse )
>       {
>         return eval("document.styleSheets[s].rules.item(i).style." + element);
>       }
>     }
>     // NOTE alert inserted below
>     alert( "checked " + s + "stylesheets - still no luck" );
>   }
>   return "undefined";
> }
> 
> it works fine (except the alerts are disturbing the bankish feel, of cause) if 
> and only if there is a stylesheet defined in a tag with at least one line in 
> it, even if that line is a comment or empty - strange or what???
> 
> I tried replacing the alert() with some other statement, with no luck. If the 
> alert isn't there, the stylesheets count of the document is wrong!
> 
> Conclusion: document.styleSheets.length is not correctly calculated under all 
> circumstances(?).
> 
> The enclosed HTML and CSS files examplifies the problem.

Isn't this simply because the loading of the CSS stylesheet is done asynchronously
(in the background), so when the JS code runs, the CSS hasn't been loaded yet?
I don't think there's a bug here, only unreliable code (might work in other browsers,
on the local filesystem. But on a real website, which could be slow to deliver the CSS,
it would give the same problem in all browsers, I think.
I guess the website should at least wait for onLoad before running this JS code.

PS: better use bugs.kde.org, you'll get faster response times :)

- -- 
David FAURE, david at mandrakesoft.com, faure at kde.org
http://people.mandrakesoft.com/~david/
Contributing to: http://www.konqueror.org/, http://www.koffice.org/
Get the latest KOffice - http://download.kde.org/stable/koffice-1.2/
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.7 (GNU/Linux)

iD8DBQE9v7n272KcVAmwbhARAromAJ9eYqIfNss8JKnV/6w2b38GQK6xLQCgggvg
EF4G54OC2MSy5MR3dfduB2E=
=+4Gk
-----END PGP SIGNATURE-----





More information about the kfm-devel mailing list