473,569 Members | 2,901 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Putting "javascript :" in front of code?

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? Or are both ways perfectly fine? Are there any
specifications that discuss when "javascript :" should be put in front of code?

Jul 20 '05 #1
25 3716

<de*******@no.s pam.com> schreef in bericht
news:3f******** *******@news.md .comcast.gigane ws.com...
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? Or are both ways perfectly fine? Are there any
specifications that discuss when "javascript :" should be put in front of code?


The first one is the proper way, because the onmouseover attribute (and
other event handlers) expects the value to contain JS code only.

What you will also see very often, is the usage of the javascript: pseudo
protocol in anchors:
<a href="javascrip t:popup('somepa ge')">...</a>

which should be replaced with:
<a href="somepage" onclick="popup( href); return false">...</a>

because the link will still be clickable when JS is disabled. When JS is
enabled, the function will be called with the href value as an argument and
the 'return false' at the end prevents that the default action will be
taken.

See also: http://jibbering.com/faq/#FAQ4_24
JW

Jul 20 '05 #2
Janwillem Borleffs wrote on 06 Dec 2003:

<de*******@no.s pam.com> schreef in bericht
news:3f******** *******@news.md .comcast.gigane ws.com...
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? Or are both ways perfectly fine?
Are there any specifications that discuss when "javascript :"
should be put in front of code?

The second way is incorrect. "JavaScript :" is a URI protocol
specifier, so it is only valid in a src or href attribute. However
(as J Borleffs stated), JavaScript URIs should not be used in case
JavaScript has been disabled by the user: the URI will be malformed
and useless to the user.
The first one is the proper way, because the onmouseover
attribute (and other event handlers) expects the value to
contain JS code only.


WHAT?!?! That is so WRONG it's untrue! The intrinsic events can be
used by ANY scripting language: that's why they are part of the HTML
specification, not solely JavaScript.

The language used in intrinsic events is specified either with a HTTP
header or, more commonly, a META element placed in the document's
HEAD:

<META http-equiv="Content-Script-Type" type="script_MI ME_type">

For JavaScript, the MIME type is text/javascript, so the above should
read:

<META http-equiv="Content-Script-Type" type="text/javascript">

If you omit either the header or META element, but use intrinsic
events, your HTML document is invalid. The only reason why it would
work is because the browser makes assumptions about what the
intrinsic events contain (they usually assume JavaScript). However,
not all may assume JavaScript as there are no 'default' languages. In
fact, the HTML specification states clearly that NO assumption needs
to be made and any events can be ignored, if no default language has
been specified by the author (using META or HTTP header).

<snipped the rest of the reply, which is OK>

Mike

--
Michael Winter
M.******@blueyo nder.co.invalid (replace ".invalid" with ".uk")
Jul 20 '05 #3

"Michael Winter" <M.******@bluey onder.co.invali d> schreef in bericht
news:Xn******** *************** ********@193.38 .113.46...

WHAT?!?! That is so WRONG it's untrue! The intrinsic events can be
used by ANY scripting language: that's why they are part of the HTML
specification, not solely JavaScript.


Yeah, you're right of course.
JW

Jul 20 '05 #4

"Michael Winter" <M.******@bluey onder.co.invali d> schreef in bericht
news:Xn******** *************** ********@193.38 .113.46...

WHAT?!?! That is so WRONG it's untrue! The intrinsic events can be
used by ANY scripting language: that's why they are part of the HTML
specification, not solely JavaScript.

The language used in intrinsic events is specified either with a HTTP
header or, more commonly, a META element placed in the document's
HEAD:

<META http-equiv="Content-Script-Type" type="script_MI ME_type">


Did some research on this header, and it seems that at least IE totally
ignores it and always defaults to JS.

Try:

<html>
<head>
<title> New Document </title>
<meta http-equiv="Content-Script-Type" content="text/vbscript" />
</head>

<body>
<a href="#" onclick="MsgBox ('Hello')">Say. ..</a>
</body>
</html>

Read:

http://www.bauser.com/websnob/meta/browsers.html
JW

Jul 20 '05 #5
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.'
Jul 20 '05 #6
"Michael Winter" <M.******@bluey onder.co.invali d> wrote in message
news:Xn******** *************** ********@193.38 .113.46...
<snip>
<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? Or are both ways perfectly fine?
Are there any specifications that discuss when "javascript :"
should be put in front of code?

The second way is incorrect.


Incorrect might be a bit strong as it implies something contrary to some
criteria of validity or syntactic correctness. Though that may just be a
matter of interpretation (of English).
"javascript: " is a URI protocol specifier, so it
is only valid in a src or href attribute.
In JavaScript source code (which is what the string provided as the
value for an event handling attribute is (assuming JavaScript is the
default language for such attributes) a character sequence that
qualifies as an identifier followed by a colon is a label[1] (ECMA 262
3rd Ed. Section 12.12). And the character sequence "javascript " fulfils
the requirements to be considered an identifier so "javascrpt: " will be
interpreted as a label by conforming interpreters.

As a result its inclusion at the start of the code for an event
handeling attribute string is completely valid, in the sense of being
completely legal JavaScript source code, it is just utterly pointless.
Unless, of course, there was a corresponding - break - or - continue -
statement that employed that label. However, IE unfortunately does place
a special interpretation on the character sequence "javascrpt: "
appearing in that context and does not use it as a label within the
resulting event handling function. So while prefixing the string
provided for an event handling attribute with "javascript :" normally
represents the valid (but pointless) introduction of a label into the
function the fact that IE treats it differently means that if the code
should have a label in that context "javascript :" would be an unwise
label to use (along with, for example, "vbscript:" ).

So the inclusion of "javascript :" as a prefix for event handling
attributes is incorrect (when used in a non-IE specific (Intranet)
context) in the sense that including a label that is never used in
source code is pointless and if a label was required "javascrpt: " would
be predictably unsuitable as a label because of the way that it is
handled by IE. Just 11 extra bytes of download which contribute nothing
but to potentially induce confusion in the minds of inexperienced
readers.
However (as J Borleffs stated), JavaScript URIs should not be
used in case JavaScript has been disabled by the user: the URI
will be malformed and useless to the user.

<snip>

JavaScript URIs are known causers of problems even when JavaScript is
enabled/available (just another reason for never using them).

Richard.

[1] Except possibly when it occurs within a switch statement or the
conditional ( ? : ) operator.
Jul 20 '05 #7
Richard Cornford wrote on 06 Dec 2003:
"Michael Winter" <M.******@bluey onder.co.invali d> wrote in
message news:Xn******** *************** ********@193.38 .113.46...
<snip>
The second way is incorrect.


Incorrect might be a bit strong as it implies something contrary
to some criteria of validity or syntactic correctness. Though
that may just be a matter of interpretation (of English).


Incorrect, meaning: the wrong way of specifying the language of an
intrinsic event.
"JavaScript :" is a URI protocol specifier, so it
is only valid in a src or href attribute.


In JavaScript source code (which is what the string provided as
the value for an event handling attribute is (assuming
JavaScript is the default language for such attributes) a
character sequence that qualifies as an identifier followed by a
colon is a label[1] (ECMA 262 3rd Ed. Section 12.12). And the
character sequence "javascript " fulfils the requirements to be
considered an identifier so "javascrpt: " will be interpreted as
a label by conforming interpreters.


[moved] [1] Except possibly when it occurs within a switch statement or
the conditional ( ? : ) operator.
That's true, but the topic isn't (quite) relating to JavaScript
source code. This is regarding the use of the string, "JavaScript :",
to actually perform some specific action. The OP assumed that using
javascript: in an intrinsic event specified the language of that
event handling code (which is incorrect). The only special nature of
the string should be as (defined by Netscape - so possibly not a
global standard) a protocol specifier in the context of a URI.

<snip>
JavaScript URIs are known causers of problems even when
JavaScript is enabled/available (just another reason for never
using them).


Agreed

Mike

--
Michael Winter
M.******@blueyo nder.co.invalid (replace ".invalid" with ".uk")
Jul 20 '05 #8
On Sat, 06 Dec 2003 12:10:04 GMT, Michael Winter
<M.******@bluey onder.co.invali d> wrote:
For JavaScript, the MIME type is text/javascript, so the above should
read:
Could you cite the relevant RFC which indicates this please? for
JavaScript (capatilised like that) the relevant one would be
application/x-javascript surely?
If you omit either the header or META element, but use intrinsic
events, your HTML document is invalid.


No, invalid has a technical meaning in HTML, and that does not make it
invalid, it may make it a non-conforming HTML 4 document, but then
that's only a tiny subset of HTML. ( RFC 2854 blesses all tag-soup to
be html)

Jim.
--
comp.lang.javas cript FAQ - http://jibbering.com/faq/

Jul 20 '05 #9
Lee
Michael Winter said:

Richard Cornford wrote on 06 Dec 2003:
"Michael Winter" <M.******@bluey onder.co.invali d> wrote in
message news:Xn******** *************** ********@193.38 .113.46...
<snip>
The second way is incorrect.


Incorrect might be a bit strong as it implies something contrary
to some criteria of validity or syntactic correctness. Though
that may just be a matter of interpretation (of English).


Incorrect, meaning: the wrong way of specifying the language of an
intrinsic event.


The OP didn't ask how to specify the language, he asked
which syntax was more proper.
That's true, but the topic isn't (quite) relating to JavaScript
source code. This is regarding the use of the string, "JavaScript :",
to actually perform some specific action.


That's not how I interpret the topic. I suspect the OP
didn't know that other languages were even available.

Jul 20 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
9927
by: Yvan J. Gagnon | last post by:
I am currenly developing a web site using Macromedia fireworks, and am trying to figure out a way (through hand-coding) of attaching a javascript function (onClick="doit=false") to each of the links in my fireworks-generated dhtml popup menus. Does anyone here know where I would add this javascript fucntion in the code so that it would be...
9
13386
by: David D. | last post by:
Does the file extension matter when including a JavaScript file in an HTML page? Normally, one would include a JavaScript file in an HTML page using <script src="foo.JS" type="text/javascript"> However, I have found that I can use an alternate file extension, such as <script src="foo.HTML" type="text/javascript"> It works fine with my...
3
1946
by: TC | last post by:
Hello, I am using the script below to open a new window and once opened, redirect to that open window from the original window: private void btnNewPDFWindow_Click(object sender, System.EventArgs e) { string NewPage = "NewPageZoom.aspx";
1
2393
by: Slavo Smutny | last post by:
Hi, which is better approach to store my JavaScript code, to store it in separate .js file or to embed the code within HTML attributes (e.g. <p onclick="javascript:submit();">click this</p>)? I am interested in performance, i.e. how quick is page loaded in each case? I think the better one is to store the code in separate .js file so that...
2
3156
by: TastyKarma | last post by:
Was hoping somebody might have experienced this before. I have client-side javascript in my aspx files. For some reason when I place the "debugger;" keyword in my javascript, Visual Studio no longer breaks into the code. It used to, and I can't figure out what could have changed to disable the feature. 1. My javascript code is definitely...
3
1615
by: kaston3 | last post by:
I use screen.width to check escreen resolution and then document.write to write different width tables accordingly. It all works OK with Mozilla Firefox, however it seems that in IExplorer it only works when I reload the page. I think it can be cache handling in IE. I tried putting the javascript code in an extrenal js.file. Then i tried...
1
2682
by: morgan.chengmo | last post by:
Hi, I am wandering whether all javascript code in one page is executed in one thread. I know that javascript has no threading mechansim. No way to tell which thread is running by code iteself. For below code <html> <head> </head>
10
539
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I protect my javascript code? ----------------------------------------------------------------------- With clientside Javascript you can't as your code is distributed in source form and is easily readable. With JScript, there is the Script Encoder...
2
4381
drhowarddrfine
by: drhowarddrfine | last post by:
When I visit this board, I see this frequently: <script language=javascript> but "language=javascript" was deprecated in 1996. So, why do people still use it? It doesn't do anything.
0
8118
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7665
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7962
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6277
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5501
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2105
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
933
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.