tinymce code fragment

D

deostroll

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

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

--deostroll
 
T

Thomas Allen

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

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

--deostroll

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
true:

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

http://www.regular-expressions.info/javascript.html

Thomas
 
R

RobG

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

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

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.
 
T

Thomas 'PointedEars' Lahn

RobG said:
RobG wrote:
[...]
The test of n.src is redundant because if n.src is undefined, the
RegExp test will result in undefined,

Ooops, it will result in false.

What you probably were to point out is that `RegExp.prototype.test(x)' (ES3F
15.10.6.3) is equivalent to `RegExp.prototype.exec(x) != null' which uses
the string representation of its argument (15.10.6.2). Therefore, the
string representation of `undefined' would be used (usually "undefined" or
"") which the RegExp does not match. Hence the observed return value.


PointedEars
 
D

deostroll

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
true:

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

http://www.regular-expressions.info/javascript.html

Thomas

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
 
T

Thomas Allen

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

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
 
D

deostroll

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

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? (--
An article link tht demonstrates this will do).

--deostroll.
 
T

Thomas Allen

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.

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...

Thomas
 
J

Jon Gómez

Thomas Allen wrote [trimmed quote]:
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.
 
T

Thomas Allen

Thomas Allen wrote [trimmed quote]:


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).

Thomas
 
D

Dr J R Stockton

In comp.lang.javascript message <f3de9c0a-60de-43ed-87e3-63900af1078f@r5
g2000prh.googlegroups.com>, Sun, 5 Apr 2009 23:46:45, RobG
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.

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.
 
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).

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
them?

HTH :)
 
T

Thomas Allen

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).

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
them?

HTH  :)

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
 
G

Garrett Smith

Thomas said:
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
true:

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

Are you making the same mistake as the author of forgetting what an
unescaped "." means in a regular expression literal?

Garrett
 
R

RobG

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...

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

<URL: http://groups.google.com/group/comp.lang.javascript/msg/0a50b3e4b5d4b580
 
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, bwthe
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).
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
them?

Cool, maybe I should remind you that I at times make use of the jQuery library.

Oh brother. How could I forget?
Then you can waste some more of your time rearranging the
paragraphs in your weary criticism of jQuery.fn.attr.

If you feel I have wasted my time explaining that idiotic and
omnipresent function, then you are either completely ignorant or
irretrievably stupid.
 
T

Thomas Allen

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.

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

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
 
J

Jorge

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;

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'
document.body.appendChild(script);

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

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top