de*******@no.sp am.com writes:
I see some code examples like this:
<DIV onmouseover="th is.style.backgr ound='blue'">
and other code examples like this:
<DIV onmouseover="ja vascript:this.s tyle.background ='blue'">
Which way is more proper?
The former. Personally, I would write:
<div onmouseover="th is.style.backgr oundColor='blue ';">
but that's leaving correct and passing on to pedantic :)
Or are both ways perfectly fine?
The "javascript :" is not necessary, doesn't mean what the author think
it means, and at best does no damage.
The author probably thinks that it specifies that what comes after is
Javascript. It doesn't [1]. Instead it is read by the Javascript
interpreter as a label with the name "javascript :". You can try this
(in a non-IE browser):
<div onclick="javasc ript:while(true ){break javascript;};al ert('exit');">
XXX
</div>
The "break javascript" statement breaks the loop labeled "javascript "
by the initial label. It doesn't work in IE [1].
Are there any specifications that discuss when "javascript :" should
be put in front of code?
Yes. The short summary is: Almost never.
The place where it belongs is in javascript
:-URL's. Example:
<a href="javascrip t:generatePage( )">generate new page</a>
The meaning of that is, that the result of the Javascript expression
after the "javascript :" becomes the new content of the page.
Anotheer example:
<a href="javascrip t:'<p>this page has no content</p>'">generate
new page</a>
That form of javascript
:-URL is often misused where the onclick
event handler is more appropriate (see the FAQ:
<URL:http://jibbering.com/faq/#FAQ4_24>).
These URLs are very rarely needed in actual pages.
javascript
: URLs are also very useful as bookmarks, where they can be
used to execute code in the context of the current page (I have
several, e.g.,
javascript
:aler t(document.comp atMode)
The other time when you might need it is if you write a page
specifically to IE, and where you have set the default script language
to, e.g., VBScript. Your page will only work in IE, so you might
as well use other IE-specific features, like the "javascript :" intrinsic
event language selection. Such a page doesn't belong on the internet,
but on intranets where you know only IE will access it, it can make
sense.
Summary: You can appropriately use "javascript :"
- in href/URL - when it generates the new page, or
- as bookmarks/favorites (aka. bookmarklets/favlets)
- in onclick etc - in IE-specific pages only.
You can most likely make a living as a web designer without ever
using "javascript :" again.
/L
[1] IE is special. It actually uses the "javascript :" to specify the
language of the following code. That is only necessary if the language
is different from the default script language of the page, which by
default is JScript/Javascript for IE. You set the default script
language for intrinsic events (e.g., onclick) using, e.g.,
<meta http-equiv="Content-Script-Type" content="text/javascript">
Not a bad idea in itself, and if other browsers supported more than
one language, it would probably become a standard. Right now it
isn't, and IE is the only browser with this behavior.
--
Lasse Reichstein Nielsen -
lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'