f



Does all javascript (like location.href) require that javascript be declared?

Hi everyone,

I am in the process of learning javascript and have a question on
location.href.  Does javascript always require the  <script language =
"javascript">
(or script type="text/javascript">) to run?

I'm asking because the following chunk of code (drop down menu that
goes to different parts of the page) works without declaring anywhere
that it's javascript:

<form>
  <select name="Library"
onchange="location.href=this.form.Library[this.form.Library.selectedIndex].value">
    <option value="#First">First</option>
    <option value="#Second">Second</option>
    <option value="#Third">Third</option>
  </select>
</form>
<a name="First">First Section</a><br><br><br><br><br><br><br><br>
<a name="Second">Second Section</a><br><br><br><br><br><br><br><br>
<a name="Third">Third Section</a><br><br><br><br><br><br><br><br>

"location.href" is javascript, right??  (I learned it off the
www.dartmouth.edu page in their drop down menu).

I appreciate any help/explanation on this!!

Andrea
0
amaruky
7/4/2003 2:12:06 PM
comp.lang.javascript 38370 articles. 2 followers. javascript4 (1315) is leader. Post Follow

3 Replies
616 Views

Similar Articles

[PageSpeed] 7

javascript is the default language if none is declared.
however, is no harm in declaring it explicitly, and better safe than
sorry....
just fyi, at some time you may like to look into how declaring the language
version e.g. <script language="JavaScript1.1">
or even language syntax e.g. <script language="JScript">
can be used to run different blocks of code in different browsers.

-alu



"Andrea" <amaruky@yahoo.com> wrote in message
news:99c59139.0307040612.61875bd1@posting.google.com...
> Hi everyone,
>
> I am in the process of learning javascript and have a question on
> location.href.  Does javascript always require the  <script language =
> "javascript">
> (or script type="text/javascript">) to run?
>
> I'm asking because the following chunk of code (drop down menu that
> goes to different parts of the page) works without declaring anywhere
> that it's javascript:
>
> <form>
>   <select name="Library"
>
onchange="location.href=this.form.Library[this.form.Library.selectedIndex].v
alue">
>     <option value="#First">First</option>
>     <option value="#Second">Second</option>
>     <option value="#Third">Third</option>
>   </select>
> </form>
> <a name="First">First Section</a><br><br><br><br><br><br><br><br>
> <a name="Second">Second Section</a><br><br><br><br><br><br><br><br>
> <a name="Third">Third Section</a><br><br><br><br><br><br><br><br>
>
> "location.href" is javascript, right??  (I learned it off the
> www.dartmouth.edu page in their drop down menu).
>
> I appreciate any help/explanation on this!!
>
> Andrea


0
alu
7/4/2003 3:38:15 PM
amaruky@yahoo.com (Andrea) writes:

> I am in the process of learning javascript and have a question on
> location.href.  Does javascript always require the  <script language =
> "javascript">
> (or script type="text/javascript">) to run?

No, there are some cases where it is implicit that the content is
Javascript.

It is <script type="text/javascript">, btw, the language version is
deprecated.
 
> I'm asking because the following chunk of code (drop down menu that
> goes to different parts of the page) works without declaring anywhere
> that it's javascript:
> 
> <form>
>   <select name="Library"
> onchange="location.href=this.form.Library[this.form.Library.selectedIndex].value">

Yes, the onchage attribute value is *inline* Javascript, just like
 <div style="border:1px solid red;"> 
has inline CSS as value of the style attribute.

> I appreciate any help/explanation on this!!

The default script language for inline scripts is javascript in all
browsers. If you want to declare it explicitly, you can use the
Content-Script-Type meta header, either sent by the server or written
inline as

 <meta http-equiv="Content-Script-Type" content="text/javascript">
or 
 <meta http-equiv="Content-Script-Type" content="text/vbscript">

(and similar for Content-Style-Type, if there was any other option
than CSS)

What some people mistakenly do is to write
 onchange="javascript:..."
That does not declare the content as javascript, it merely parses
the "javascript:" as a Javascript lable.

/L
-- 
Lasse Reichstein Nielsen  -  lrn@hotpop.com
 Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
  'Faith without judgement merely degrades the spirit divine.'
0
Lasse
7/4/2003 3:56:37 PM
On 04 Jul 2003 17:56:37 +0200, Lasse Reichstein Nielsen
<lrn@hotpop.com> wrote:

>amaruky@yahoo.com (Andrea) write:
>
>No, there are some cases where it is implicit that the content is
>Javascript.

I don't think there are in any standard, only in de-facto standards of
what's implemented in browsers so far.  

e.g. http://www.w3.org/TR/html401/interact/scripts.html says
"Documents that do not specify default scripting language information
and that contain elements that specify an intrinsic event script are
incorrect."

(of course the meta hack is so badly specified, and a bad idea anyway
that I boycott it...)

>What some people mistakenly do is to write
> onchange="javascript:..."
>That does not declare the content as javascript, it merely parses
>the "javascript:" as a Javascript lable.

Except in IE where it does declare as javascript...  It's also
relatively trivial in IE to configure a different default scripting
language to javascript, I have an IE where perlscript is the default
for example.

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

0
jim
7/4/2003 4:00:54 PM
Reply: