views:

220

answers:

1

function loadBookmarklet() { var scriptT = document.createElement("script"); scriptT.src = "http://abc.com/tinymce/jscripts/tiny_mce/tiny_mce.js"; scriptT.type = "text/javascript";

document.body.appendChild(scriptT); tinyMCE.init({ mode : "textareas", theme : "simple" }); };

i am getting the error tinyMCE is not defined, can anyone please help me with this?

New Error

Here is the new code that i have which is giving me another error

function loadBookmarklet() {
      var scriptT = document.createElement("script");
      scriptT.src = "http://abc.com/tiny_mce.js";
          scriptT.type = "text/javascript";
      scriptT.onload = function(){}
      document.body.appendChild(scriptT);
      tinyMCE.init({ mode : "textareas", theme : "simple" }); 
      newTM = tinyMCE;
      var scriptW = document.createElement("script");
      scriptW.src = "http://abc.com/widget.js";
      scriptW.type = "text/javascript";
      document.body.appendChild(scriptW);

    }

in my widget.js i am trying to set the textarea in the widget.js to the editor

newTM.activeEditor.setContent(selectedText);

the error is

newTM.activeEditor is null

+1  A: 

thats because you are not waiting for the tiny_mce.js to finish loading before you refer to the tinyMCE variable

You need to defer the tinyMCE.init code until the file is loaded. there are a couple of ways to do this

  1. Attach an onload / oncomplete handler to the injected script element, and then move the init code inside that handler. (This method may vary from browser to browser in terms of onload and oncomplete)
  2. set up a setTimeout function that checks for the existence of tinyMCE,
    1. if it does not exist, it calls itself again
    2. if it exists the timeout is cleared and the init script is called

I usually use the second method, so you code can be adjusted to

var scriptT = document.createElement("script"); 
scriptT.src = "http://abc.com/tinymce/jscripts/tiny_mce/tiny_mce.js"; scriptT.type = "text/javascript";
document.body.appendChild(scriptT);

//Your script loading code is above this line

function TM_wait() { //this function checks for existence of tinyMCE
  if(typeof tinyMCE == 'undefined') {
    window.setTimeout(TM_wait,100); 
    //calls itself if tinyMCE is not loaded
  }else{
    tinyMCE.init({ mode : "textareas", theme : "simple" }); 
    //calls the init function cos tinyMCE is loaded
  }

}

TM_wait(); // calls the checker function for the first time

EDIT

In response to your edit, try this

function loadBookmarklet() {
      var scriptT = document.createElement("script");
      scriptT.src = "http://abc.com/tiny_mce.js";
      scriptT.type = "text/javascript";
      scriptT.onload = function(){
        var scriptW = document.createElement("script");
        scriptW.src = "http://abc.com/widget.js";
        scriptW.type = "text/javascript";
        scriptW.onload = function(){
           newTM = tinyMCE;
           newTM.init({ mode : "textareas", theme : "simple" }); 
        }
        document.body.appendChild(scriptW);
      }
      document.body.appendChild(scriptT);
}

step by step of what's happening above:

  1. TinyMCE is loaded
  2. when tinyMCE is finished loading, your widget is loaded
  3. when your widget is finished loading, the variable newTM is created as an alias of tinyMCE
  4. tinyMCE is initialised

Because we wait for tinyMCE and the widget.js to finish loading, everything is set up properly and in order. Now your newTM should contain a reference to tinyMCE

pǝlɐɥʞ
i added the code that you have mentioned above, though it gives me that the variable is "undefined", it seems to be in some kind of infinite loop. new code is below, am i missing anything??function TM_wait() { if(typeof tinyMCE == 'undefined') { window.setTimeout(TM_wait,100); }else{ tinyMCE.init({ mode : "textareas", theme : "simple" }); }} function loadBookmarklet() { var scriptT = document.createElement("script"); scriptT.src = "http://abc./jscripts/tiny_mce/tiny_mce.js"; scriptT.type = "text/javascript"; document.body.appendChild(scriptT); TM_wait();}
Chitra
it should be in a loop until tinyMCE is loaded. Please check if the tiny_mce.js is not doing a 404
pǝlɐɥʞ
i was able to solve the error tinyMCE is not defined,but adding the line scriptT.onload = function(){}Now the error i come across is when i try to access this tinyMCE variable in my bookmarklet.js, its giving me an error tinyMCE null. Any ideas how to pass the tinyMCE variable from the main html page which has the bookmarklet function to the js file which processes what action when the bookmarklet is clicked?
Chitra
can you post your new code again as an edit to the question
pǝlɐɥʞ