Erik Norgaard

Navigation

Skip navigation.

Search

Site navigation

Email conversation

FromErik Norgaard
ToMe
SubjectJavaScript tutorial: when are script executed and the object element
Date13 April 2007 12:20
Hi:

Your guide is great! But there are two things that are not clear to me:

1) When is a script executed.

I assume that if a script is inlined in the html it is executed as each line
is loaded? But then, can you reference a function that is not defined till
later down the script?

Secondly, if a script is loaded from a separate file, with a script element
in the head, will it be executed before the page is completely loaded?

Say in my script I have

  someElement = document.getElementById(eltId)

Can I be sure if the element is found? If javascript files are loaded and
executed after the docuement has loaded, then the onload action for the body
element is not required.

By the way: I have found that

  <scriptt type="text/javascript" src="script.js"/>

doesn't work in xhtml documents in Firefox, but

  <scriptt type="text/javascript" src="script.js"></script>

does.

2) Integration with objects:

The object element can be used to embed just about any kind of object
handled by the browser or a plugin, images, video, java or flash. But I
would like to integrate java script controls with these.

For example, to embed video, I would like to first show a still image and
have the user click the image. Then the video would start. The idea is to
present info on the file size so the user can choose not to run the video.
Also, I would like to cache the video ahead of playing.

Secondly, I have a project embedding a java applet that must be integrated
with java script. There are various problems:

First, the object element is different whether in Firefox or IE. The general
suggestion is to have one object element containing the alternative, but I
have found this doesn't work properly when integrating with java script. So,
actual browser check is necesary. I understand you discurrage browser
checks, but in this case there is no alternative. You did not post the code
to do such a check.

Secondly, there is a security problem. The applet should work as a gateway
accessing local resources on behalf of the java script. But I have found
that data from the java script is untrusted by the applet and causes a
security error. Is there any way to circumvent this?

Thank you for any help on these issues. I understand and respect that you
may have other stuff to do.

Cheers, Erik
FromMe
ToErik Norgaard
SubjectRe: JavaScript tutorial: when are script executed and the object element
Date14 April 2007 12:48
Erik,

> I assume that if a script is inlined in the html it is executed as each
> line is loaded?

It loads everything between the <script ...> and </script> tags, and if that
can be parsed as valid syntax, then it is executed. This execution takes
place just after the parser reaches the </script> tag.

> But then, can you reference a function that is not defined till later down
> the script?

Only if it is inside the same <script> block (and only if it is defined as a
normal function, not one assigned to a variable, etc.).

> if a script is loaded from a separate file, with a script element in the
> head, will it be executed before the page is completely loaded?

Yes. The browser will stop parsing the page, wait for that script to load,
then execute it at the same time as if the script were written in between
the <script ...> and </script> tags.

>   someElement = document.getElementById(eltId)
>
> Can I be sure if the element is found?

Only if that element exists in the page _before_ the <script ...> tag, or if
the script is running inside an event handler or timeout thread after the
element has been created on the page.

>   <scriptt type="text/javascript" src="script.js"/>
>
> doesn't work in xhtml documents in Firefox, but
>
>   <scriptt type="text/javascript" src="script.js"></script>
>
> does.

It does work, but you must serve the document as application/xhtml+xml and
not text/html (since using text/html actually tells the browser to treat it
as HTML, not XHTML, no matter what DOCTYPE you use). More info here:
http://webkit.org/blog/?p=68

> The object element can be used to embed just about any kind of object
> handled by the browser or a plugin, images, video, java or flash. But I
> would like to integrate java script controls with these.

Note that it is JavaScript, not Java Script - the latter is too easy to
confuse with the unrelated Java.

I think Java applets in object tags does not work properly in IE, due to its
broken object support. Should work in the others though, if the user has
Java enabled. Bear in mind that on some systems - my own included - starting
a Java applet can take upto a minute while completely hanging up the
browser. Several people, such as myself, keep Java disabled, even if it is
available.

> I would like to first show a still image and have the user click the
> image. Then the video would start.

Well, each plugin has its own API, and I do not touch them, since I despise
plugins (best not to ask). But you can either use that API to start the
video (if available), or not create the object until the user clicks
something, then create it using DOM or innerHTML, and put it in the page.

> Also, I would like to cache the video ahead of playing.

That is handled by the plugin itself, so you will need to use the API of
your chosen plugin. See their own documentation for more details. Note that
most plugin versions also need Java to be enabled as a bridge (called
LiveConnect) between JavaScript and the plugin. There are a few that can use
the newer APIs that do not need Java, such as Flash 9.

> Secondly, I have a project embedding a java applet that must be integrated
> with java script.

Use an applet element, not an object. That supposedly works properly in all
of them (though I have never tested this).

> You did not post the code to do such a check.

I did, but I do not encourage its use, and in fact my license terms forbid
its use for this sort of thing - yes, I really do think browser detects are
evil. In any case, if you still need to perform a check, you can check for
the objects, not the browsers. If you find the method you expect on the
object, then assume it works. If not, assume you need to check the
alternative:
if(document.getElementById('foo').someMethod) {
  document.getElementById('foo').someMethod();
  ...etc...
} else {
  document.getElementById('bar').someMethod();
}
(Adjust that according to whatever APIs are made available.)

> Secondly, there is a security problem. The applet should work as a gateway
> accessing local resources on behalf of the java script. But I have found
> that data from the java script is untrusted by the applet and causes a
> security error. Is there any way to circumvent this?

Thankfully not with normal applets, no. That would be a major security
issue. If Java applets could simply access data wherever they pleased, the
user's computer would be at risk, and so would any web sites they log into.

Supposedly signed Java applets can extend their privileges, but their use is
strongly discouraged. And of course, I have never looked into how to use
them.


Mark 'Tarquin' Wilton-Jones - author of http://www.howtocreate.co.uk/
This site was created by Mark "Tarquin" Wilton-Jones.
Don't click this link unless you want to be banned from our site.