views:

57

answers:

4

I have a question and an issue wrt the code below:

My question is what is the scope of the variable loaded here. The reason why i ask this is the onload="if(loaded==1)inittextarea() code is working fine on Firefox and not IE8. Why is this happening? Is there something specific i need to do here? Or is it not a valid practice?

<html>
<head>
<title>Some Page</title>
<link rel="stylesheet" href="../css/default.css" type="text/css">
<script type="text/javascript"> 
    var loaded = 0; /*Point of interest*/
    function jsLoaded() {
    loaded =1; 
}
</script>
<script type="text/javascript">
    function inittextarea() {
        alert("test")
        tinyMCE.init({  
            elements : "content",
            theme : "advanced",
            readonly : true,
            mode : "exact",
            theme : "advanced",
            readonly : true,
            setup : function(ed) {
                ed.onInit.add(function() {
                tinyMCE.activeEditor.execCommand("mceToggleVisualAid");
                });
            }
        });
    }
</script>
<script src="../js/tiny_mce/tiny_mce.js" onload="jsLoaded()" type="text/javascript"></script>
</head>
<body onload="if(loaded==1)inittextarea()"><!--Works on Firefox only-->
    *Usual stuff*
</body></html>

Any pointers please?

+2  A: 

The use of onload in the body tag is frowned upon now as best practice. Perhaps you should revise they way your initialisation happens.

This article explains.

starskythehutch
Actually, using the `onload` attribute of the `<body>` element is the only standardised way of capturing the fact that the document has loaded so could be seen as best practice.
Tim Down
@tarskythehutch @Tim As of now i have a few limitations in using the approach mentioned in http://www.onlinetools.org/articles/unobtrusivejavascript/chapter4.html . So, is there a way i could mod something in the current approach to make this work in IE?
dkris
See my updated answer.
Tim Down
+1  A: 

The problem is that Script onLoad doesnt work in ie.

Mongus Pong
What do you think is the alternative for this? Any workarounds?
dkris
I always use JQuery. Then I initialize TinyMCE in the document.load. Works well and you get a lot extra with the library that will be useful! And you don't need to worry about individual browser differences as much. Everyones a winner! ;-)
Mongus Pong
+3  A: 

onload on <script> elements isn't generally supported. You could work around this by simply putting a <script> element below the TinyMCE <script> element:

<script src="../js/tiny_mce/tiny_mce.js" type="text/javascript"></script>
<script type="text/javascript">
    jsLoaded();
</script>

Although actually this is all unnecessary: by the time <body>'s onload fires, all the JavaScript will be guaranteed to have loaded, so there's no need for the check:

<body onload="inittextarea()">
Tim Down
Best answer. Why use `jsLoaded` when you're already handling the `load` event?
Alsciende
A: 

Why don't you just initialize TinyMCE on document load?

bogdanvursu
the `inittextarea` in the `onload` does that. Doesn't it?
dkris