tinymce code fragment

Discussion in 'Javascript' started by deostroll, Apr 6, 2009.

  1. deostroll

    deostroll Guest

    if (n.src && /tiny_mce(|_dev|_src|_gzip|_jquery|_prototype).js/.test

    What does the above mean? I mean those thing in '|'s.

    deostroll, Apr 6, 2009
    1. Advertisements

  2. deostroll

    Thomas Allen Guest

    If n.src returns true and one of the following filenames (see below)
    is in the return value of n.src, do stuff.

    The /tiny_mce part.../ is a regular expression. The part '|' that you
    were interested in is an "or" operator in regex, so the presence of
    any of the following filenames would cause the second part to return

    * tinymce_dev.js
    * tinymce_src.js
    * tinymce_gzip.js
    * tinymce_jquery.js
    * tinymce_prototype.js


    Thomas Allen, Apr 6, 2009
    1. Advertisements

  3. deostroll

    RobG Guest

    Others have answered that, but the first part of the test is redundant
    and insufficient, it would be better as:

    if (n && ...)

    It is insufficient as it does not protect the expression from throwing
    an error - if n is not defined, undefined or null, attempting to
    evaluate n.src will throw an error.

    The test of n.src is redundant because if n.src is undefined, the
    RegExp test will result in undefined, which is equivalent to false.
    There may be a small performance gain in testing n.src before applying
    the RegExp test, but it is likely insignificant.
    RobG, Apr 6, 2009
  4. deostroll

    RobG Guest

    Ooops, it will result in false.
    RobG, Apr 6, 2009
  5. What you probably were to point out is that `RegExp.prototype.test(x)' (ES3F is equivalent to `RegExp.prototype.exec(x) != null' which uses
    the string representation of its argument ( Therefore, the
    string representation of `undefined' would be used (usually "undefined" or
    "") which the RegExp does not match. Hence the observed return value.

    Thomas 'PointedEars' Lahn, Apr 6, 2009
  6. deostroll

    deostroll Guest

    So u are essentially calling the test() which might reside in one of
    those above mentioned files...? Can we interpret this as linking to an
    external script at runtime?

    deostroll, Apr 6, 2009
  7. deostroll

    Thomas Allen Guest

    No, "test" is a method of the Regex object. It accepts one argument
    which is the string to test (the "subject" in Regex parlance). You're
    right that n.src probably refers to a file name, but this code in no
    way interacts with any files, it just searches the string n.src to see
    if it includes any of the above filenames.

    Thomas Allen, Apr 6, 2009
  8. deostroll

    deostroll Guest

    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? (--
    deostroll, Apr 6, 2009
  9. deostroll

    Thomas Allen Guest

    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'

    I'd be curious if anybody knows of better ways to do this...

    Thomas Allen, Apr 6, 2009
  10. deostroll

    Jon Gómez Guest

    Thomas Allen wrote [trimmed quote]:
    How cross-browser safe is that? I've been wondering since I first saw
    it in this group.
    Jon Gómez, Apr 6, 2009
  11. deostroll

    Thomas Allen Guest

    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

    Thomas Allen, Apr 6, 2009
  12. In comp.lang.javascript message <[email protected]
    g2000prh.googlegroups.com>, Sun, 5 Apr 2009 23:46:45, RobG
    I don't think it's right to say, seeing that one statement, that the
    existence etc. of n needs to be tested in it; immediately preceding code
    might have established that n can at this stage be safely referred to,
    if only by being similar enough to have failed already otherwise.

    And the explicit test for n.src protects the programmer against doubt as
    to what a RegExp test on a non-existent item might return : discovering
    that from the ISO standard is non-trivial, and both that and testing
    presumes that all browsers get it right.
    Dr J R Stockton, Apr 6, 2009
  13. deostroll

    David Mark Guest

    As usual, you are stabbing around in the dark. Scripts load
    asynchronously. Using a timeout to race the script download is
    madness and doomed to fail.

    Time spent in that jQuery group has retarded your development.
    Misunderstanding very basic concepts is the norm among jQuery users,
    authors, technical writers and the rest of their "large and active
    community" (see attributes vs. properties.) Who would put a penny on

    HTH :)
    David Mark, Apr 6, 2009
  14. deostroll

    Thomas Allen Guest

    Cool, maybe I should remind you that I at times make use of the jQuery
    library. Then you can waste some more of your time rearranging the
    paragraphs in your weary criticism of jQuery.fn.attr.

    Thomas Allen, Apr 6, 2009
  15. Are you making the same mistake as the author of forgetting what an
    unescaped "." means in a regular expression literal?

    Garrett Smith, Apr 6, 2009
  16. deostroll

    RobG Guest

    You might want to browse the thread started by the following message:

    <URL: http://groups.google.com/group/comp.lang.javascript/msg/0a50b3e4b5d4b580
    RobG, Apr 6, 2009
  17. deostroll

    David Mark Guest

    Oh brother. How could I forget?
    If you feel I have wasted my time explaining that idiotic and
    omnipresent function, then you are either completely ignorant or
    irretrievably stupid.
    David Mark, Apr 6, 2009
  18. deostroll

    Thomas Allen Guest

    Thomas Allen, Apr 6, 2009
  19. deostroll

    Thomas Allen Guest

    I don't know about that, as this line doesn't capture the return

    Here's the full function that OP is referring to:

    function getBase(n) {
    if (n.src && /tiny_mce(|_dev|_src|_gzip|_jquery|
    _prototype).js/.test(n.src)) {
    if (/_(src|dev)\.js/g.test(n.src))
    t.suffix = '_src';

    if ((p = n.src.indexOf('?')) != -1)
    t.query = n.src.substring(p + 1);

    t.baseURL = n.src.substring(0, n.src.lastIndexOf('/'));

    // If path to script is relative and a base href was found add
    that one infront
    if (base && t.baseURL.indexOf('://') == -1)
    t.baseURL = base + t.baseURL;

    return t.baseURL;

    return null;

    Thomas Allen, Apr 6, 2009
  20. deostroll

    Jorge Guest

    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;

    then you insert it in the page... :

    if (window.scriptOneLoaded) {
    //already loaded
    } else {
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = 'scriptNumberOne.js'

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

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

    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';

    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:

    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'...
    Jorge, Apr 7, 2009
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.