tinymce code fragment

D

David Mark

Thomas Allen wrote [trimmed quote]:
So you can't load a function that resides in another file (in another
location) which is not included in your html page? (You know, bw the
script tag).
Can we write our script in such a way that it is dependent on another
script file being present? Else it should show some error message?(--
The only way that I know of to do this is to insert a <script> tag
pointing to the target script:
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'path/to/your/script.js'
document.body.appendChild(script);
I'd be curious if anybody knows of better ways to do this...
How cross-browser safe is that?  I've been wondering since I first saw
it in this group.
Jon.
I'm not sure, which is why I've never used it in my own code. Testing
locally, one problem is timing the actions that depend on the loaded
script. In Mozilla/Opera, I can give the sensitive calls a low
setTimeout (50-200) which allows for the script to load, but this does
not work in IE6/IE7 (it appears to check for the methods' presence
immediately, throwing an error before the extra script has time to
load).

That's the way most JSON (cross-domain) apis are being done nowadays.
It's 100% cross-browser compatible, and 100% dangerous: don't ever do
this if you're building a page for e.g. online banking !

This is the way of doing it by polling: suppose that this is the
script's source code:

//useful code here, plus:
window.scriptOneLoaded= true;

Is this how you normally create global variables?
then you insert it in the page... :

if (window.scriptOneLoaded) {
  //already loaded} else {

You want to try that again?
  var script = document.createElement('script');
  script.type = 'text/javascript';
  script.src = 'scriptNumberOne.js'
  document.body.appendChild(script);

...and enter a loop that checks for it:

  (function check () {
    if (! window.scriptOneLoaded) {
      setTimeout(check, 500);
    } else {
      //loaded, do whatever.
    }
  })();

}

Don't do that.
or you can do it via a callback:

1.- You choose a name for the callback (e.g.'callBackFunction') and
define it:

window.callBackFName= function (parameters) {
  //will execute upon loading of scriptOne.

};

2.- Insert the <script> in your page... :

var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'example.com/path/script.js?callBack=callBackFName';

Why would you have to pass the name? It is hard-coded into the script
in the previous example. Would work just as well here.
                                        ^^^^^^^^^^^^^^^^^^^^^^^
document.body.appendChild(script);

3.- The server receives the request and extracts the callBack
parameter from the search part of the URL, and appends this to the
script's source:

//the source, plus:
window.callBackFName(parameters);

When the script gets loaded, the callBack 'callBackFName' gets called.
(that's the way many of Yahoo's json apis work currently).

Each one has pros/cons: the former doesn't require passing the
callback as a parameter, the latter requires some server-side
programming but is more 'elegant'...

No, except that the latter is superior to the former. Neither
requires server-side programming.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top