Connecting Tech Pros Worldwide Forums | Help | Site Map

Include .js file inside HTML and call functions from another <script>

Iddo
Guest
 
Posts: n/a
#1: Apr 5 '06
Hi,
I am having a strange problem...
I have an HTML file which has 2 script tags:
1) <script language="javascript" id="ABC" src="ABC.js" />
2) <script id="general" language="javascript">
function foo()
{
alert("aaa");
}
</script>

I am trying to call the "foo" function and also functions from the
"ABC.js" file.

when calling the ".js" file - functions are called fine!
however, when calling the "foo" function - I get an error! ("Object
expected")

My HTML:
<HTML>
<head>
<script language="javascript" id="ABC" src="ABC.js" />
<script id="general" language="javascript">
function foo()
{
alert("aaa");
}
</script>
<title>Test</title>
</head>
<body>
<INPUT id="Button1" type="button" value="JS1"
onclick="JSFileFunction()"></P>
<INPUT id="Button2" type="button" value="JS2" onclick="foo()"></P>
</body>
</HTML>

My ABC.js file:
function JSFileFunction()
{
alert("ABC");
}


PLZ help me.... I'm stuck!!!


VK
Guest
 
Posts: n/a
#2: Apr 5 '06

re: Include .js file inside HTML and call functions from another <script>



Iddo wrote:[color=blue]
> Hi,
> I am having a strange problem...
> I have an HTML file which has 2 script tags:
> 1) <script language="javascript" id="ABC" src="ABC.js" />
> 2) <script id="general" language="javascript">
> function foo()
> {
> alert("aaa");
> }
> </script>[/color]

I have forgotten the closing tag for the external file (<script>
elements always requires closing tag).

Script element has type "text/javascript" (not language "javascript").

Script elements doesn't have id attribute.

<script type="text/javascript" src="ABC.js"></script>
<script type="text/javascript">
// your code here
</script>

jan.hancic@gmail.com
Guest
 
Posts: n/a
#3: Apr 5 '06

re: Include .js file inside HTML and call functions from another <script>


Change:
<script language="javascript" id="ABC" src="ABC.js" />

into
<script language="javascript" id="ABC" src="ABC.js"></script>


That solved the problem for me (in Firefox)

Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
#4: Apr 6 '06

re: Include .js file inside HTML and call functions from another <script>


Iddo wrote:
[color=blue]
> I have an HTML file which has 2 script tags:[/color]

There are no "script tags". There are (script) _elements_,
consisting of start and end tag, and optionally content.
[color=blue]
> 1) <script language="javascript" id="ABC" src="ABC.js" />[/color]

MUST be at least

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

HTML's SHORTTAG syntax is different from XHTML's. In HTML,

<script ... />

is equivalent to

<script ...>&gt;

First, text content is not allowed directly below the `head' element,
and second, the `script' element is not closed.
[color=blue]
> 2) <script id="general" language="javascript">[/color]

Must be at least

<script type="text/javascript" language="javascript">

The `type' attribute is mandatory, and the element has no `id' attribute
(nor would it need one). The `language' attribute is deprecated in HTML
4.01, and can be safely omitted. It MUST be omitted if you declare HTML
4.01 Strict.
[color=blue]
> [...]
> I am trying to call the "foo" function and also functions from the
> "ABC.js" file.
> when calling the ".js" file[/color]

Files cannot be called, they can be executed if they contain executable
code. But this is no file, it is a script resource (that can be saved
as a file). It can be (down)loaded, if that.
[color=blue]
> - functions are called fine!
> however, when calling the "foo" function - I get an error! ("Object
> expected")[/color]

It would appear that due to forced error-correction of your invalid markup,
the </script> (close) tag of the second `script' element is understood as
the close tag for the first `script' element, and any code in the second
`script' element is ignored. Hence there is no foo() method that can be
called.
[color=blue]
> <HTML>[/color]

Although not required in HTML, element type identifiers and attribute
identifiers should be lowercase always. A DOCTYPE declaration is missing
before this start tag of the `html' element.
[color=blue]
> <INPUT id="Button1" type="button" value="JS1"
> onclick="JSFileFunction()"></P>
> <INPUT id="Button2" type="button" value="JS2" onclick="foo()"></P>[/color]

It does not seem as if you would need an ID for either button.
There is no <P> open tag, so nothing that needs to be closed with </P>.

<URL:http://validator.w3.org/>
[color=blue]
> PLZ help me.... I'm stuck!!![/color]

It would be best if you understood the basics of Web authoring before you
started with Web programming.


PointedEars
John G Harris
Guest
 
Posts: n/a
#5: Apr 6 '06

re: Include .js file inside HTML and call functions from another <script>


In article <286092203.PkbqXVLH7G@PointedEars.de>, Thomas 'PointedEars'
Lahn <PointedEars@web.de> writes[color=blue]
>Iddo wrote:[/color]

<snip>[color=blue][color=green]
>> <HTML>[/color]
>
>Although not required in HTML, element type identifiers and attribute
>identifiers should be lowercase always.[/color]
<snip>

Why? Do you have a technical reason, or are you just being an art
critic?

John
--
John Harris
Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
#6: Apr 6 '06

re: Include .js file inside HTML and call functions from another <script>


John G Harris wrote:
[color=blue]
> [...] Thomas 'PointedEars' Lahn [...] writes[color=green]
>>Iddo wrote:[color=darkred]
>>> <HTML>[/color]
>>
>> Although not required in HTML, element type identifiers and attribute
>> identifiers should be lowercase always.[/color]
>
> Why? Do you have a technical reason, or are you just being an art
> critic?[/color]

Yes. It compresses better, is less error-prone, and is good to be developed
as a habit when taking more recent markup languages into account.


PointedEars
Tony
Guest
 
Posts: n/a
#7: Apr 6 '06

re: Include .js file inside HTML and call functions from another <script>


Thomas 'PointedEars' Lahn wrote:[color=blue]
> John G Harris wrote:
>[color=green]
>>[...] Thomas 'PointedEars' Lahn [...] writes[color=darkred]
>>>
>>>Although not required in HTML, element type identifiers and attribute
>>>identifiers should be lowercase always.[/color]
>>
>>Why? Do you have a technical reason, or are you just being an art
>>critic?[/color]
>
> Yes. It compresses better, is less error-prone, and is good to be developed
> as a habit when taking more recent markup languages into account.[/color]

Not sure how it would "compress better" (or why that would be relevant)

Consistency, in either direction, would be less error-prone, IMO.

The third point is probably the best case to make: I'm not up on all the
specs, but I understand that some of them REQUIRE lower-case tags. If
you're already in that habit, it's less likely you'll screw something up
if you ever have need to use another doctype.
Bruno Desthuilliers
Guest
 
Posts: n/a
#8: Apr 7 '06

re: Include .js file inside HTML and call functions from another <script>


Tony a écrit :[color=blue]
> Thomas 'PointedEars' Lahn wrote:
>[color=green]
>> John G Harris wrote:
>>[color=darkred]
>>> [...] Thomas 'PointedEars' Lahn [...] writes
>>>
>>>>
>>>> Although not required in HTML, element type identifiers and attribute
>>>> identifiers should be lowercase always.
>>>
>>>
>>> Why? Do you have a technical reason, or are you just being an art
>>> critic?[/color]
>>
>>
>> Yes. It compresses better, is less error-prone, and is good to be
>> developed
>> as a habit when taking more recent markup languages into account.[/color]
>
>
> Not sure how it would "compress better"[/color]

Probably because most the text of a page being lowercase, you may have
better compression with tag names also in lowercase... (wild guess) (and
totally ot).

Ivan Marsh
Guest
 
Posts: n/a
#9: Apr 7 '06

re: Include .js file inside HTML and call functions from another <script>


On Sat, 08 Apr 2006 00:57:16 +0200, Bruno Desthuilliers wrote:
[color=blue]
> Tony a écrit :[color=green]
>> Thomas 'PointedEars' Lahn wrote:[color=darkred]
>>> John G Harris wrote:
>>>> [...] Thomas 'PointedEars' Lahn [...] writes
>>>>>
>>>>> Although not required in HTML, element type identifiers and attribute
>>>>> identifiers should be lowercase always.
>>>>
>>>> Why? Do you have a technical reason, or are you just being an art
>>>> critic?
>>>
>>> Yes. It compresses better, is less error-prone, and is good to be
>>> developed
>>> as a habit when taking more recent markup languages into account.[/color]
>>
>> Not sure how it would "compress better"[/color]
>
> Probably because most the text of a page being lowercase, you may have
> better compression with tag names also in lowercase... (wild guess) (and
> totally ot).[/color]

Huh? In ASCII all characters are seven bit.


--
The USA Patriot Act is the most unpatriotic act in American history.

Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
#10: Apr 7 '06

re: Include .js file inside HTML and call functions from another <script>


Bruno Desthuilliers wrote:
[color=blue]
> Tony a écrit :[color=green]
>> Thomas 'PointedEars' Lahn wrote:[color=darkred]
>>> John G Harris wrote:
>>>> [...] Thomas 'PointedEars' Lahn [...] writes
>>>>> Although not required in HTML, element type identifiers and attribute
>>>>> identifiers should be lowercase always.
>>>> Why? Do you have a technical reason, or are you just being an art
>>>> critic?
>>> Yes. It compresses better, is less error-prone, and is good to be
>>> developed as a habit when taking more recent markup languages into
>>> account.[/color]
>> Not sure how it would "compress better"[/color]
>
> Probably because most the text of a page being lowercase, you may have
> better compression with tag names[/color]

and attribute names (but not attribute values, of course)
[color=blue]
> also in lowercase... (wild guess)[/color]

Exactly. Redundancy counts.
[color=blue]
> (and totally ot).[/color]

ACK


PointedEars
Ivan Marsh
Guest
 
Posts: n/a
#11: Apr 7 '06

re: Include .js file inside HTML and call functions from another <script>


On Fri, 07 Apr 2006 22:27:21 +0200, Thomas 'PointedEars' Lahn wrote:
[color=blue]
> Bruno Desthuilliers wrote:
>[color=green]
>> Tony a écrit :[color=darkred]
>>> Thomas 'PointedEars' Lahn wrote:
>>>> John G Harris wrote:
>>>>> [...] Thomas 'PointedEars' Lahn [...] writes
>>>>>> Although not required in HTML, element type identifiers and attribute
>>>>>> identifiers should be lowercase always.
>>>>> Why? Do you have a technical reason, or are you just being an art
>>>>> critic?
>>>> Yes. It compresses better, is less error-prone, and is good to be
>>>> developed as a habit when taking more recent markup languages into
>>>> account.
>>> Not sure how it would "compress better"[/color]
>>
>> Probably because most the text of a page being lowercase, you may have
>> better compression with tag names[/color]
>
> and attribute names (but not attribute values, of course)
>[color=green]
>> also in lowercase... (wild guess)[/color]
>
> Exactly. Redundancy counts.[/color]

What compression utilizing redundancy occurs between the web server and
the browser?

--
The USA Patriot Act is the most unpatriotic act in American history.

VK
Guest
 
Posts: n/a
#12: Apr 8 '06

re: Include .js file inside HTML and call functions from another <script>



Ivan Marsh wrote:[color=blue]
> Huh? In ASCII all characters are seven bit.[/color]

Huh? The US may have some defaults, but not up to the point of using
seven-bit bytes :-)

ASCII characters are the conventional 8-bit bytes, but only 7 bits are
used, which brings the total amount of possible combinations to 127
(low part of ASCII table).

Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
#13: Apr 8 '06

re: Include .js file inside HTML and call functions from another <script>


VK wrote:
[color=blue]
> Ivan Marsh wrote:[color=green]
>> Huh? In ASCII all characters are seven bit.[/color]
>
> Huh? The US may have some defaults, but not up to the point of using
> seven-bit bytes :-)[/color]

And even if they had, the statement would be irrelevant regarding data
compression. For it was not argued that a lowercase character required
less bits to be encoded (on the contrary, if ASCII was not the fixed-width
encoding that it is, lowercase characters would require _more_ bits to be
encoded than uppercase characters, because uppercase characters have
_lower_ code points than lowercase characters in ASCII).
[color=blue]
> ASCII characters are the conventional 8-bit bytes, but only 7 bits are
> used,[/color]

False. The eighth bit has been (is?) used, too, but not for the character
code.
[color=blue]
> which brings the total amount of possible combinations to 127
> (low part of ASCII table).[/color]

False. The number is 2^7 = 128, of course. From 0 to 127 decimal
(7F hexadecimal).

<URL:http://en.wikipedia.org/wiki/ASCII>


PointedEars
Closed Thread