Why isn´t this possible?

O

Oscar Monteiro

I´m trying to make an application with if ...something
else if ... something else to dinamically change a src script,
can't this be done ? and if so why not ?
Thanks in advance , Oscar Monteiro
<span id="script">
<script type="text/javascript" >
script.innerHTML="script src='a.js' ";
</script>
 
S

Stewart Gordon

Oscar said:
I´m trying to make an application with if ...something
else if ... something else to dinamically change a src script,
can't this be done ? and if so why not ?

JavaScript defined in a script block is executed as and when the HTML
page is initially interpreted. This includes reading in any functions
defined in the script block. If you try programmatically changing the
content of a script block, I don't think the behaviour is defined.

However, a possibility is to do something like

<script type="text/javascript"><!--

document.write('<script type="text/javascript" src="a.js"><\/script>');

// --></script>

Note the backslash, which makes extra sure that the closing script tag
that you write out isn't interpreted as the end of this code.

The exact time at which the included script is processed in relation to
the script that includes it isn't well defined. You'll need to end this
script block and start another before you can use functions defined in
the .js file.
<span id="script">
<script type="text/javascript" >
script.innerHTML="script src='a.js' ";
</script>

If this worked, you would end up with

<span id="script">script src='a.js' </span>

which obviously isn't what you intended. Before you count the fact that
you have nothing with a _name_ (as opposed to id) of script.

Stewart.
 
V

VK

IMHO the question was:

Why JavaScript (at least in its ISO-blessed ECMA version) still refuses to
add standard precompiler instructions like
#if
#elseif
#const
etc.

The sincere answer would be: "God knows: why!".
Maybe they just like to see us being stoke in "90's forever" with these ugly
....
else (document.write("<scr"+"ipt src='some.js'>"+"</scr"+"ipt>");)
....
 
M

Michael Winter

[snip]
<span id="script">
<script type="text/javascript" >
script.innerHTML="script src='a.js' ";
[snip]

Before you count the fact that you have nothing with a _name_ (as
opposed to id) of script.

Even so, an identifier matching the name of an element does not, by
itself, access that element. The appropriate collection, whether it be
document.forms, .links, .images, should also be used.

Mike
 
M

Michael Winter

[snip]
Why JavaScript (at least in its ISO-blessed ECMA version) still refuses
to add standard precompiler instructions like
#if
#elseif
#const
etc.

The sincere answer would be: "God knows: why!".

ECMAScript makes no assumptions as to the environment in which it runs.
Such extensions would be just that: extensions. Besides, you can simulate
conditional compilation with standard language constructs, which is
probably why it isn't an extension to an existing implementation.

By the way, you never know what might be added in future as ECMAScript has
a large list of reserved keywords (most from Java):

abstract enum int short
boolean export interface static
byte extends long super
char final native synchronized
class float package throws
const goto private transient
debugger implements protected volatile
double import public

[snip]
else (document.write("<scr"+"ipt src='some.js'>"+"</scr"+"ipt>");)

At the very least, that last part should be changed to:

'<\/src' + 'ipt>'

Although there is no current evidence to suggest that browsers do
interpret a </ sequence to mean the end of a SCRIPT element, for the sake
of an extra character, you shouldn't take that risk.

Mike
 
K

kaeli

I=3Fm trying to make an application with if ...something
else if ... something else to dinamically change a src script,
can't this be done ? and if so why not ?
Thanks in advance , Oscar Monteiro
<span id="script">
<script type="text/javascript" >
script.innerHTML="script src='a.js' ";
</script>

See here for an example of dynamic script elements. DOM browsers only.
http://www.ipwebdesign.net/kaelisSpace/useful_serverClientValidation.html

--
--
~kaeli~
Murphy's Law #2000: If enough data is collected, anything
may be proven by statistical methods.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
 
R

Randy Webb

Oscar said:
I´m trying to make an application with if ...something
else if ... something else to dinamically change a src script,
can't this be done ? and if so why not ?

Yes, you can dynamically change the src attribute of a script tag. But
only in IE. In any other browser you have to create a new script tag and
load the src file. Do a search of the c.l.j archives for "dynamically
loading .js file" and you can find threads where, among other people, I
have talked about doing just what you want to do.
 
V

VK

ECMAScript makes no assumptions as to the environment in which it runs.

But it should. It never pretended on the Java's WORA slogan, so this
pharossy is escaping my understanding.
Besides, you can simulate conditional compilation with standard language
constructs,

Anything can be done in more than one way. Just look at NN4/IE4 - compatible
historic scripts: A monument to the human creativity non-surrender spirit.
But why to keep it this way?
By the way, you never know what might be added in future as ECMAScript >

Why it is not done now?
"God knows: why" - my answer remains the most profound one.
At the very least, that last part should be changed to:

'<\/src' + 'ipt>'

.... and so on: thank you for an illustration of how ugly the current
situation is with the pseudo-precompiled instructions.
 
S

Stewart Gordon

VK said:
IMHO the question was:

Why JavaScript (at least in its ISO-blessed ECMA version) still refuses to
add standard precompiler instructions like
#if
#elseif
#const
etc.

Because JavaScript has no standard precompiler.

And because some consider the preprocessor to be an abomination. This
is part of the design of D:

http://www.digitalmars.com/d/overview.html
(scroll down to 'Features to Drop')

But then, I'm not sure what this is to do with anything....
The sincere answer would be: "God knows: why!".
Maybe they just like to see us being stoke in "90's forever" with these ugly
...
else (document.write("<scr"+"ipt src='some.js'>"+"</scr"+"ipt>");)
...

That doesn't look like valid JS syntax.

Stewart.
 
M

Michael Winter

But it should.

No, it shouldn't. It is a scripting language with applications outside of
browsers, so there cannot be any assumptions about the environment.
It never pretended on the Java's WORA slogan, so this pharossy is
escaping my understanding.
Pharossy?

[snip]
By the way, you never know what might be added in future as ECMAScript >

Why it is not done now?

Because there's no need. If the precondition can be evaluated on the
server, it can insert the required script. If not, a script replace
functions at runtime to adapt to its environment. That, in fact, provides
more functionality. Preprocessing instructions are executed at parse-time,
which may not be an appropriate moment to evaluate the environment, so
again, I fail to see the need.

[snip]
... and so on: thank you for an illustration of how ugly the current
situation is with the pseudo-precompiled instructions.

The correction had nothing to do with the "current situation", as you see
it. Escaping the backslash should be done with *every* closing tag, not
just closing SCRIPT tags. Perhaps you should read
<URL:http://www.w3.org/TR/html4/appendix/notes.html#h-B.3.2>. Of course,
this only applies within a HTML document. If the script is in an external
file, there's no problem.

Mike
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top