object expected error

M

Michael Winter

VK wrote:
[snip]
var x = {
y: function z() { alert("hello"); }
};
alert(z); // displays z function body
[snip]
Function z is treated as *function statement*.

No, it is a FunctionExpression, as produced through the
AssignmentExpression production (if you only read the specs once).

Yes, it is. However, with regard to variable instantiation, it acts like
a function declaration in IE.

A global variable, z, is created and initialized before the expression
is evaluated. That is,

if (typeof z == 'function') {
alert('The global variable, z, is defined.');
}
var x = {
y : function z() {}
};

will display the dialogue box in IE, but not in Fx or Op.

I believe that this is the point VK was trying to make, but failed.
Because of the identifier, which is optional per the
FunctionExpression production and is used here, there is a named
reference to a Function object.

Yes, but that identifier should not be available outside that function
body, as noted in section 13.

Of course, one could consider this "program ... syntax not described in
this specification."

[snip]

Mike
 
T

Thomas 'PointedEars' Lahn

Michael said:
VK said:
var x = {
y: function z() { alert("hello"); }
};
alert(z); // displays z function body
[snip]
Function z is treated as *function statement*.

No, it is a FunctionExpression, as produced through the
AssignmentExpression production (if you only read the specs once).

Yes, it is. However, with regard to variable instantiation, it acts
like a function declaration in IE.
ACK

[...]
I believe that this is the point VK was trying to make, but failed.

Which would still be disregarding the context completely of course,
because I was talking only about KJS, the ECMAScript/JavaScript/JScript
implementation in _KHTML_.
Yes, but that identifier should not be available outside that function
body, as noted in section 13.

And it is not in JavaScript 1.5+ (Gecko M3+), the Opera 6+ implementation,
or KJS (its buggy behavior with FunctionExpressions is restricted to their
being within ObjectLiterals).
Of course, one could consider this "program ... syntax not described in
this specification."

Full ACK. JavaScript 1.3, as implemented in Netscape versions 4.06 to 4.8,
exhibits the same behavior as described for IE here.


PointedEars
 
V

VK

Randy said:
Richard Cornford said the following on 3/14/2006 10:56 AM:

It's been so long since I looked into that I had forgotten that part of
the error. Thank you for reminding me, and yes, that would be the reason
for the error message.

It also gave me cause to read that part of the Notes. I glanced over it
quickly and have to agree with you that the way it is written is proper.

Fight even everything is lost ;-)

The relevant Notes' text is completely wrong, Yahoo! sets error page
headers properly, your test case contains some error or a patch you are
not willing to disclose.

Try this (and feel free to write your own for Gecko and others):

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<script type="text/Jscript">
var xmlhttp = new ActiveXObject('Msxml2.XMLHTTP');
var url = 'http://geocities.com/schools_ring/tmp/nonexisting.js';
xmlhttp.open('GET', url, false);
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4) {
if (xmlhttp.status==404) {
alert(xmlhttp.status);
}
}
}
xmlhttp.send('');
</script>
</head>

<body>
 
R

Randy Webb

VK said the following on 3/15/2006 2:55 AM:
Fight even everything is lost ;-)

The relevant Notes' text is completely wrong, Yahoo! sets error page
headers properly, your test case contains some error or a patch you are
not willing to disclose.

My test case? I gave you the exact script blocks. No patch to cause the
error either. Had you read the FAQ Notes, and understood them, it would
become obvious what is happening. I tested it offline. That should be
your first hint.

Browser encounters a script tag with a src attribute.
Browser retrieves that file.
File doesn't exist.
Browser gets a 404 page in response.
Browser inserts that 404 page - as is - into the script block.
Browser throws an invalid character error.

The script block would look something like this - in memory:

<script type="text/javascript">
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
.....
</script>

And that, my dear friend, is an "Invalid character" error message for you.

What the Notes describes:

Browser encounters a script tag with a src attribute.
Browser retrieves that file.
File can't be retrieved.
Server sends 404 response.
Browser realizes the file isn't available.
Browser executes the script block.

The major difference, that you missed, is in the "404 page" versus "404
response".
 
V

VK

Randy said:
What the Notes describes:

Browser encounters a script tag with a src attribute.
Browser retrieves that file.
File can't be retrieved.
Server sends 404 response.
Browser realizes the file isn't available.
Browser executes the script block.

The major difference, that you missed, is in the "404 page" versus "404
response".

And this is plain wrong from the first line to the last one.

Let's us go step by step:

1) The OP's problem: caused by the fact that it is impossible to
execute both external script and internal script:
<script src="file.js">
// statements
</script>

- all inner statements are ignored *if current UA supports src
attribute for <script> tag*.
If you have any problems with this common knowledge fact, please refer
to it as "Position 1" - so it wouldn't be mixed with other.

2) FAQ Notes statement:
<script src="file.js">
// statements
</script>
- statements will be executed if file.js is not available and this
fact is properly reported by server (over correct 404 page)

Plain wrong because if UA supports src attribute for <script> tag,
inner statements are ignored *on the page parsing stage* - so at the
moment script block is parsed by HTML parser, activated and making .js
file request - at this moment inner statements already skipped and do
not exist anymore. Parser do not hold them in some "buffer" in case if
the external resource is not available.
If you have any problems with this common knowledge fact, please refer
to it as "Position 2" - so it wouldn't be mixed with other.

3) Error page returned by server may affect on script behavior and even
cause errors. Plain wrong - as well as you seem to have a rather
specific idea about "proper 404 response". It is not a some kind of
bodyless error spirit sent by server - it is mainly the same
conventional HTML page with *headers set to right values*. The most
common on the Web Apache 404 page for example looks like:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /foobar.js was not found on this server.</p>
<hr>
<address>Apache/2.2.0 (Unix) Server at www.apache.org Port 80</address>
</body></html>

That brings back the problem with your testcase - you're trying to say
that there is nothing augmented in it, but there definitely is.

Here is a sample to try - I used Apache.org error page with guaranteed
proper headers:

<html>
<head>
<title>Myth Buster v1.0</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<script
type="text/javascript"
src="http://www.apache.org/foobar.js">
alert('VK is wrong!');
</script>
</head>
<body>
</body>
</html>

If you manage to bring the message "VK is wrong!" up by changing
response headers and content, please report your succes as "Position 3"
- so it wouldn't be mixed with other.
 
R

Randy Webb

VK said the following on 3/16/2006 2:49 AM:
And this is plain wrong from the first line to the last one.

Whatever you say VK. Whatever you say. Trying to explain anything
technical to you is a waste of time.
 
V

VK

Randy said:
Whatever you say VK. Whatever you say. Trying to explain anything
technical to you is a waste of time.

You don't need to explain anything. Just make the "VK is wrong!" alert
to appear by using/changing/patching the provided code:

<html>
<head>
<title>Myth Buster v1.0</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<script
type="text/javascript"
src="http://www.apache.org/foobar.js">
alert('VK is wrong!');
</script>
</head>
<body>
</body>
</html>

or by using the bogus code from
<http://jibbering.com/faq/faq_notes/script_tags.html#hsMix>

I wouldn't care if it was a "somewhere in the Web" article. But as it's
linked to the official group FAQ then this <q>utter nonsense</q> should
be corrected, don't you think?

It is not necessary to remove it completely. It could be re-written for
a very useful topic like say "Controlling external library load from
another script block". Say
<script src="external.js"></script>
<script> function internal(){} </script>
and what would be the means to detect in internal() that external.js
library is loaded or failed to load (of course 404 doesn't help here,
but at least it is doable by other means).
That would be a useful article:- I can provide the original variant for
the public "tear apart".
 
V

VK

Randy said:
if(isLoaded){
alert('External file loaded');
}
else{
alert('External file not loaded');
}

Where the variable isLoaded is defined in the last line of the external
file.

This code fails: if isLoaded is not defined at the moment of call it
will raise uncought error. You need to do either it in the old way:

if ('undefined' != typeof isLoaded) {
// library is loaded
// Operator typeof is the only operator to address
// non-existing variables without raising an error.
// A gift of "fathers" from the pre-try/catch epoch.
}

or in the new way:

try {
if (isLoaded) {...
}
catch(e) {
//
}
 
R

Randy Webb

VK said the following on 3/18/2006 3:12 AM:
You don't need to explain anything. Just make the "VK is wrong!" alert
to appear by using/changing/patching the provided code:

It has been done or it wouldn't be in the Notes.
<html>
<head>
<title>Myth Buster v1.0</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<script
type="text/javascript"
src="http://www.apache.org/foobar.js">
alert('VK is wrong!');
</script>
</head>
<body>
</body>
</html>

or by using the bogus code from
<http://jibbering.com/faq/faq_notes/script_tags.html#hsMix>

It is not Bogus. I do think it needs to be changed but not for the
reasons you give.
I wouldn't care if it was a "somewhere in the Web" article. But as it's
linked to the official group FAQ then this <q>utter nonsense</q> should
be corrected, don't you think?

Again, yes, but not for the reasons you are giving.
It is not necessary to remove it completely.

It needs to be removed.
It could be re-written for a very useful topic like say "Controlling
external library load from another script block". Say
<script src="external.js"></script>
<script> function internal(){} </script>

if(isLoaded){
alert('External file loaded');
}
else{
alert('External file not loaded');
}

Where the variable isLoaded is defined in the last line of the external
file.
and what would be the means to detect in internal() that external.js
library is loaded or failed to load (of course 404 doesn't help here,
but at least it is doable by other means).

You have the pure 100% complete code needed above. Nothing else needed.
It's quite simple and that simplicity is why I think the Notes should be
changed and no other reason.
That would be a useful article:- I can provide the original variant for
the public "tear apart".

No need, I gave it above.
 
V

VK

Randy said:
It has been done or it wouldn't be in the Notes.

It couldn't be done because never existed a browser to do it. In one of
posts in this thread I did a suggestion of how this erroneus text could
appear from reading wrongly the purpose and mechanics of some ancient
scripts (no "src" attribute fall-back).
It is not Bogus. I do think it needs to be changed but not for the
reasons you give.

You keep reffering on some misterious "background sense" of this text
while there is not such. It is simply a misunderstanding of the HTML
parser mechanics. A quote from my previous post:
"Plain wrong because if UA supports src attribute for <script> tag,
inner statements are ignored *on the page parsing stage* - so at the
moment script block is parsed by HTML parser, activated and making .js
file request - at this moment inner statements already skipped and do
not exist anymore. Parser do not hold them in some "buffer" in case if
the external resource is not available."

Then HTML parser meets opening <script> tag and sees src attribute set,
it skeeps any further parsing until it meets the closing </script> tag.
Then DOMScriptElement gets activated and making the call. At this
moment any internal statements do not exists already: they are skipped
on the previous (parsing) stage. This can be demonstrated by changing a
bit my original testcase:

<html>
<head>
<title>Myth Buster v1.0</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<script
type="text/javascript"
src="http://www.apache.org/foobar.js">
!@#$%^&*
These lines will never be parsed
if UA supports src attribute
so they will not cause an error.
*&^%$#@!
</script>
</head>
<body>
</body>
</html>
 
R

Randy Webb

VK said the following on 3/18/2006 4:31 AM:
This code fails: if isLoaded is not defined at the moment of call it
will raise uncought error.
True.

You need to do either it in the old way:

Or simply define it in the page. See below. Sometimes, you make things
harder than they have to be.
if ('undefined' != typeof isLoaded) {
// library is loaded
// Operator typeof is the only operator to address
// non-existing variables without raising an error.
// A gift of "fathers" from the pre-try/catch epoch.
}

or in the new way:

Should be interesting to see how you would determine which way to try :)
try {
if (isLoaded) {...
}
catch(e) {
//
}

Or in a better way all the way around.

<script type="text/javascript">
var isLoaded = false;
</script>
<script type="text/javascript" src="external.js"></script>
<script type="text/javascript">
if (isLoaded){
//file is loaded
}else{
//file is not loaded
}
</script>
 
V

VK

Randy said:
<script type="text/javascript">
var isLoaded = false;
</script>
<script type="text/javascript" src="external.js"></script>
<script type="text/javascript">
if (isLoaded){
//file is loaded
}else{
//file is not loaded
}
</script>

This is better :) but it still fails: now because of timing issues. By
the moment of
if (isLoaded)
check there are 99.9% of chance that external.js is not loaded yet or
not parsed, so it will never get a chance to set isLoaded to true on
time. To keep the above approach we need to write a real loader with a
timer and define how long do we want to try before to give up:

<script type="text/javascript" src="external.js"></script>
<script type="text/javascript">
function loader(i) {
var loaded = ((typeof isLoaded == 'boolean')&&(isLoaded));
if (i < 20) {
if (loaded) {
notifyObserver(true);
}
else {
window.setTimeout('loader('+(++i)+')',500);
}
}
else {
if (loaded) {
notifyObserver(true);
}
else {
notifyObserver(false);
}
}
}

loader(0);

notifyObserver(result) {
// your code
}
</script>

In the above code we are trying to load the external file for 10sec
checking the status every 500ms.
 
T

Thomas 'PointedEars' Lahn

Richard said:
Probably not, although it may benefit from more emphasis on the
non-universal browser support instead of just the issues with proper
HTTP 404 errors and the fact that the entire technique is unnecessary
in practice.

To spare us further diversion from the point that is in question here:

I have often said (rightfully) when VK was wrong, and I will also not
hesitate to say when he is right. So, he /is/ right here.

Let's face it: The FAQ Notes are badly outdated, if not utterly wrong,
regarding this:

| The browser should handle this formulation of the script element by
| attempting to load the external file, but in the even[t] of that attempt
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| failing instead the contents of the script element are executed.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

They will not benefit from more emphasis on the "non-universal browser
support", because no browser support of this is to be expected in the first
place. The HTML 4.01 Specification makes it very clear how this situation
MUST be handled, and so far all UAs I have tested with (which admittedly
does not include any IE-based one) follow the Specification in that regard
(and there is no `src' attribute in HTML 3.2, and previous versions are
outdated since RFC2854).

My test case[1], which was unfortunately somehow buried in discussions about
how FunctionExpressions should be handled within certain contexts, makes
that very clear. Any UA that does not follow the Specification (or the ISO
HTML Specification, which is based on it) here, is _violating_ it (there is
no SHOULD, there is a MUST). And relying on that unspecified behavior is
_potentially harmful_ (as the test results for NN4 and IE show).

This FAQ Notes section MUST be completely revised, and it MUST be made very
clear that the behavior displayed by some UAs (with naming them and their
versions explicitly) regarding this, is not standards compliant, and is not
to be expected from other UAs.


PointedEars
___________
[1] news:[email protected]
 
V

VK

Thomas said:
I have often said (rightfully) when VK was wrong, and I will also not
hesitate to say when he is right. So, he /is/ right here.

Score adjusted: was -1000, now -990

:-D
 
T

Thomas 'PointedEars' Lahn

VK said:
Thomas said:
I have often said (rightfully) when VK was wrong, and I will also not
hesitate to say when he is right. So, he /is/ right here.

Score adjusted: was -1000, now -990

:-D
Likewise.

[...]
(and there is no `src' attribute in HTML 3.2, and previous versions are
outdated since RFC2854).

Should have been "OBSOLETE", not "outdated".


PointedEars
 
R

Randy Webb

VK said the following on 3/18/2006 7:08 AM:
This is better :) but it still fails: now because of timing issues. By
the moment of
if (isLoaded)
check there are 99.9% of chance that external.js is not loaded yet or
not parsed, so it will never get a chance to set isLoaded to true on
time.

If your browser does that check before the external script file is
loaded then you have a seriously flawed browser.

Test HTML:
<URL: http://members.aol.com/_ht_a/askhikk/externalJSFile.html >

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN"
"http://www.w3.org/TR/REC-html40/strict.dtd">
<html>
<head>
<title>External Source File Test</title>
<script type="text/javascript">
var isLoaded = false;
</script>
<script type="text/javascript" src="external.js"></script>
<script type="text/javascript">
alert(isLoaded)
</script>
<style type="text/css">
</style>
</head>
<body>
</body>
</html>

external.js:
<URL: http://members.aol.com/_ht_a/askhikk/external.js>

var begin = new Date();
for (var i=0;i<10000;i++){
var k = new Date().toString();
}
var end = new Date();
alert(end - begin)
/*
200 kilobytes of the letter k
to make the file large enough
to introduce a timing issue.
*/
var isLoaded = true;


I added the Date code just to introduce more time delay.

There are 2 alerts. The time it takes to perform the loop in the
external file and the alert(isLoaded) in the HTML file. What you are
saying is going to happen is that I get the isLoaded alert and then the
alert from the external.js file. Test it.

IE6, Opera 7, Opera 8, Opera 9 and Firefox 1.5 all give the alerts in
this order:

Time alert.
isLoaded alert.

If you have a browser that gives them in a different order, let me know.
To keep the above approach we need to write a real loader with a
timer and define how long do we want to try before to give up:

No you don't. Test the above code.

In the above code we are trying to load the external file for 10sec
checking the status every 500ms.

And it's a wasted endeavor.
 
V

VK

Randy said:
If your browser does that check before the external script file is
loaded then you have a seriously flawed browser.

I guess it is going to be a myth busting thread all the way around :).
I'm shamefully admitting that the initial script block loading process
was not understood by me. In case of (pseudo-code):

<script>x</script>
<script src="y"></script>
<script>y</script>

it is not a set of consecutevely executed blocks as I thought. In the
reality browser creates one "big script" and inserts in this script the
sources of the declared script blocks in the order they've been
declared. Only when all blocks are retrieved (or failed to be
retrieved), browser forwards the joined source text to the script
engine.
I guess it is a right or close to the right explanation (?)

In my defence I can only tell that by the nature of my tasks I was
mainly concentrated on loaded-or-not check for dynamically added
scripts and (even more often) stylesheets. In case:

var script01 = document.createElement('SCRIPT');
script01.src = '.....';
roof.appendChild(script01);

the above spelled mechanics is not applicable (?) so one needs a timed
check out. Or of course the loaded block needs to notify somehow the
main block. (For dynamically added stylesheets that was not an option
though).

Some draft proposal could be useful (?) of standard ways of how
libraries have to notify the main block of their arrival, their name,
their version. Some standard function call or some standard object/var
check obligatory for all distributable libraries producers. (?)
 
R

Randy Webb

VK said the following on 3/19/2006 10:22 AM:
I guess it is going to be a myth busting thread all the way around :).

Looks that way.
I'm shamefully admitting that the initial script block loading process
was not understood by me. In case of (pseudo-code):

<script>x</script>
<script src="y"></script>
<script>y</script>

it is not a set of consecutevely executed blocks as I thought. In the
reality browser creates one "big script" and inserts in this script the
sources of the declared script blocks in the order they've been
declared. Only when all blocks are retrieved (or failed to be
retrieved), browser forwards the joined source text to the script
engine.
I guess it is a right or close to the right explanation (?)

Close enough :) It doesn't create "one big script block" though. If you
view the normalized source of the page after it loads you will see three
script blocks.
In my defence I can only tell that by the nature of my tasks I was
mainly concentrated on loaded-or-not check for dynamically added
scripts and (even more often) stylesheets. In case:

var script01 = document.createElement('SCRIPT');
script01.src = '.....';
roof.appendChild(script01);

the above spelled mechanics is not applicable (?) so one needs a timed
check out. Or of course the loaded block needs to notify somehow the
main block. (For dynamically added stylesheets that was not an option
though).

If I were doing something like that, then script01 would have a call as
the last line to continue execution. See the "How to call a PHP script
using Javascript" thread from today. I explained it twice already in
that thread :) It kills any need for knowing when the script is loaded
as the script itself tells you.
Some draft proposal could be useful (?) of standard ways of how
libraries have to notify the main block of their arrival, their name,
their version. Some standard function call or some standard object/var
check obligatory for all distributable libraries producers. (?)

See above :)

external.js:

//lots of JS code here
functionToProcessTheAboveCode()

Where the function resides in the document itself.
 
V

VK

VK said:
parser mechanics. A quote from my previous post:
"Plain wrong because if UA supports src attribute for <script> tag,
inner statements are ignored *on the page parsing stage* - so at the
moment script block is parsed by HTML parser, activated and making .js
file request - at this moment inner statements already skipped and do
not exist anymore. Parser do not hold them in some "buffer" in case if
the external resource is not available."

Leaving everything as it is (because it is total true) it may be
necessary (?) to mention at least one escape from the traditional
<script> declaration rules: namely it is possible to not have the
closing tag for external script declaration: but only for XHTML pages
and only served with the proper application/xhtml-xml Content-Type:

<http://groups.google.com/group/comp...6ff8329925aae8a6/?hl=en&#doc_99a53c0a96697191>
<quote>
So, anyway: one *can* use <script> without closing tag on a valid XHTML
page served with Content-Type application/xhtml-xml. It breaks the
search engine indexing and makes the page unavailable to IE users (at
least not available in the conventional way). But if it's a question of
life or death - then here is the way.
</quote>
 
R

Randy Webb

VK said the following on 4/13/2006 4:40 PM:
Leaving everything as it is (because it is total true) it may be
necessary (?) to mention at least one escape from the traditional
<script> declaration rules: namely it is possible to not have the
closing tag for external script declaration: but only for XHTML pages
and only served with the proper application/xhtml-xml Content-Type:

You forgot to mention that you have to be using a UA that honors them both.

Use both opening and closing tags and it becomes a moot point.
<http://groups.google.com/group/comp...6ff8329925aae8a6/?hl=en&#doc_99a53c0a96697191>
<quote>
So, anyway: one *can* use <script> without closing tag on a valid XHTML
page served with Content-Type application/xhtml-xml. It breaks the
search engine indexing and makes the page unavailable to IE users (at
least not available in the conventional way). But if it's a question of
life or death - then here is the way.
</quote>

If it is a life or death then you are dead for trying to use XHTML on
the web anyway.
<let the content negotiation debate start>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top