Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > really dumb error but it's stumped me

Reply
Thread Tools

really dumb error but it's stumped me

 
 
Joe Nine
Guest
Posts: n/a
 
      06-02-2010
I had a big page and I was getting an error. To make it easier to show
here and recreate I trimmed and trimmed until there was almost nothing
left but the error remains.

On IE8 and Opera 10 I get no errors. On Firefox 3.5, Safari 4 and Chrome
4 I get a different error on each. Here's the entire html page. As
you'll see I've removed tags and attributes that weren't relevant to the
problem so it's an absolute bare minimum.

<html>
<body>
<script>
document.write("<scr"+"ipt src=''><\/scr"+"ipt>");
</script>
</body>
</html>

Earlier it had a head section, the script tags had type on them and
there was a src URL for the script but none of that had anything to do
with the error so I left it out.

On XP/Firefox 3.5 I get: "invalid XML tag syntax" with an error pointing
to the first plus symbol.

On XP/Safari 4 I get: "Syntax Error: Parse Error"

On XP/Chrome 4 I get: "Uncaught SyntaxError: Unexpected token <"

In an earlier version, I had an extra line of code before the
document.write:

var abc;if(true){abc=true;}

When that's in (and remember that's trimmed and trimmed from what it
originally was, which wasn't just if(true)) instead of the errors
mentioned, I get a different error in Firefox. Instead I get "missing }
in XML expression" with an arrow pointing to the semi-colon after abc=true
 
Reply With Quote
 
 
 
 
Scott Sauyet
Guest
Posts: n/a
 
      06-02-2010
On Jun 2, 3:00*pm, Joe Nine <j...@yahoo.com> wrote:
> <html>
> <body>
> <script>
> document.write("<scr"+"ipt src=''><\/scr"+"ipt>");
> </script>
> </body>
> </html>


I think the error is because of the empty src attribute in the
generated SCRIPT element.

It's requesting the current document as the source of the script
element, and when it finds HTML instead of Javascript, it chokes. If
you said "src='abc'" does the error disappear?

--
Scott
 
Reply With Quote
 
 
 
 
Joe Nine
Guest
Posts: n/a
 
      06-02-2010
Scott Sauyet wrote:
> On Jun 2, 3:00 pm, Joe Nine <j...@yahoo.com> wrote:
>> <html>
>> <body>
>> <script>
>> document.write("<scr"+"ipt src=''><\/scr"+"ipt>");
>> </script>
>> </body>
>> </html>

>
> I think the error is because of the empty src attribute in the
> generated SCRIPT element.
>
> It's requesting the current document as the source of the script
> element, and when it finds HTML instead of Javascript, it chokes. If
> you said "src='abc'" does the error disappear?
>
> --
> Scott


No, the error was there also when I was loading a real file. But I have
to say, the file it was loading did itself also do a document.write of a
n empty script tag. The error wasn't inside the JS file though, it was
still in the parent page. Before I'd trimmed the JS file down to being
just a document.write of an empty script tag, it was trying to load a JS
file that was getting a 404. Perhaps that was the problem. Anytime you
load either a blank src, or a src that results in a 404, the parent page
exhibits a weird XML or parse error in 3 out of the top 5 browsers.
 
Reply With Quote
 
Scott Sauyet
Guest
Posts: n/a
 
      06-02-2010
Joe Nine wrote:
> Scott Sauyet wrote:
>> Joe Nine wrote:


>>> [ ... ]
>>> document.write("<scr"+"ipt src=''><\/scr"+"ipt>");
>>> [ ... ]

>
>> I think the error is because of the empty src attribute in the
>> generated SCRIPT element.

>
>> It's requesting the current document as the source of the script
>> element, and when it finds HTML instead of Javascript, it chokes. *If
>> you said "src='abc'" does the error disappear?

>
> No, the error was there also when I was loading a real file. But I have
> to say, the file it was loading did itself also do a document.write of a
> n empty script tag. The error wasn't inside the JS file though, it was
> still in the parent page.


Don't count on the error location being reported properly when scripts
are including other scripts...

> Before I'd trimmed the JS file down to being
> just a document.write of an empty script tag, it was trying to load a JS
> file that was getting a 404. Perhaps that was the problem. Anytime you
> load either a blank src, or a src that results in a 404, the parent page
> exhibits a weird XML or parse error in 3 out of the top 5 browsers.


I don't think a 404 would be an issue. The issue is when the page
referenced is not proper JS. If you use a blank "src" attribute, then
it's requesting the current HTML file for the JS. That will certainly
cause problems. If you requested "myDoc.html", it would probably also
cause problems (assuming that file included HTML.)

-- Scott
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      06-02-2010
Joe Nine wrote:

> <script>
> document.write("<scr"+"ipt src=''><\/scr"+"ipt>");


You do not need to separate the start and end tag of the element,
respectively. What matters is that the `</' in the end tag is escaped.
Otherwise, by SGML parsing rules, it delimits the content of the script
element prematurely. (This has been discussed here ad nauseam, and is
also pointed out by the W3C Validator when it encounters `</' there.)

Therefore, you can use double quotes for attributes in dynamically generated
markup, too (makes it easier to make static markup dynamic and vice-versa):

document.write('<script src="..."><\/script>');

Not that loading a script like this would be a good idea to begin with.

> Earlier it had a head section, the script tags had type on them and


It not logical to assume that introducing errors would remove other errors.
The `type' attribute is #REQUIRED. Furthermore, but this is not an error,
a `script' element in the `body' element may work differently than in the
`head' element, depending on the script code thus executed.


PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
 
Reply With Quote
 
Joe Nine
Guest
Posts: n/a
 
      06-02-2010
Scott Sauyet wrote:
> Joe Nine wrote:
>> Scott Sauyet wrote:
>>> Joe Nine wrote:

>> Before I'd trimmed the JS file down to being
>> just a document.write of an empty script tag, it was trying to load a JS
>> file that was getting a 404. Perhaps that was the problem. Anytime you
>> load either a blank src, or a src that results in a 404, the parent page
>> exhibits a weird XML or parse error in 3 out of the top 5 browsers.

>
> I don't think a 404 would be an issue. The issue is when the page
> referenced is not proper JS. If you use a blank "src" attribute, then
> it's requesting the current HTML file for the JS. That will certainly
> cause problems. If you requested "myDoc.html", it would probably also
> cause problems (assuming that file included HTML.)
>
> -- Scott


Thanks Scott (and Thomas in his reply). I hadn't realized (and find it
quite strange) that creating a tag with a blank src attribute would
request the current HTML file for the JS. How unusual, but at least it
explains it, especially when I think now of the original larger page.

It was loading a JS file, the name of which was in a variable. I was
allowing the variable to have a blank name thinking it wouldn't do any
harm, as I expected document.write'ing a script tag with no src would
simply create a script element that had nothing in it (no harm no foul).
Clearly that's wrong. I won't be doing that again.

This place is great. I doubt the answer to this particular peculiarity
is documented anywhere on the web.
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      06-02-2010
Joe Nine wrote:

> Scott Sauyet wrote:
>> I don't think a 404 would be an issue. The issue is when the page
>> referenced is not proper JS. If you use a blank "src" attribute, then
>> it's requesting the current HTML file for the JS. That will certainly
>> cause problems. If you requested "myDoc.html", it would probably also
>> cause problems (assuming that file included HTML.)

>
> Thanks Scott (and Thomas in his reply). I hadn't realized (and find it
> quite strange) that creating a tag with a blank src attribute would
> request the current HTML file for the JS. How unusual, [...]


Only to the uninitiated, I am afraid. The value of the `src' attribute of
SCRIPT elements is specified to be of type URI⁽¹⁾, that is, a URI or URI-
reference as specified in RFC 3986⁽²⁾ (which obsoletes RFC 2396, which
updates RFC 1738, which is therefore referred in the HTML 4.01
Specification⁽³⁾).

An empty URI-reference is specified to be a same-document URI-reference.⁽⁴⁾
You can find that, for example, with the `action' attribute of FORM
elements, too, primarily if the document is dynamically generated by a
server-side application (like PHP).⁽⁵⁾

You're welcome


Regards,

PointedEars
___________
⁽¹⁾ <http://www.w3.org/TR/REC-html40/interact/scripts.html#h-18.2.1>
⁽²⁾ <http://tools.ietf.org/html/rfc3986>
⁽³⁾ <http://www.w3.org/TR/REC-html40/types.html#type-uri>
⁽⁴⁾ <http://tools.ietf.org/html/rfc3986#section-4.4>
⁽⁵⁾ <http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.3>
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
 
Reply With Quote
 
David Mark
Guest
Posts: n/a
 
      06-02-2010
On Jun 2, 3:40*pm, Scott Sauyet <scott.sau...@gmail.com> wrote:
> Joe Nine wrote:
> > Scott Sauyet wrote:
> >> Joe Nine wrote:
> >>> [ ... ]
> >>> document.write("<scr"+"ipt src=''><\/scr"+"ipt>");
> >>> [ ... ]

>
> >> I think the error is because of the empty src attribute in the
> >> generated SCRIPT element.

>
> >> It's requesting the current document as the source of the script
> >> element, and when it finds HTML instead of Javascript, it chokes. *If
> >> you said "src='abc'" does the error disappear?

>
> > No, the error was there also when I was loading a real file. But I have
> > to say, the file it was loading did itself also do a document.write of a
> > n empty script tag. The error wasn't inside the JS file though, it was
> > still in the parent page.

>
> Don't count on the error location being reported properly when scripts
> are including other scripts...
>
> > Before I'd trimmed the JS file down to being
> > just a document.write of an empty script tag, it was trying to load a JS
> > file that was getting a 404. Perhaps that was the problem. Anytime you
> > load either a blank src, or a src that results in a 404, the parent page
> > exhibits a weird XML or parse error in 3 out of the top 5 browsers.

>
> I don't think a 404 would be an issue.


A missing script will be problem if the server sends an error page.
 
Reply With Quote
 
Joe Nine
Guest
Posts: n/a
 
      06-03-2010
Thomas 'PointedEars' Lahn wrote:
> Joe Nine wrote:
>
>> Scott Sauyet wrote:
>> Thanks Scott (and Thomas in his reply). I hadn't realized (and find it
>> quite strange) that creating a tag with a blank src attribute would
>> request the current HTML file for the JS. How unusual, [...]

>
> Only to the uninitiated, I am afraid. The value of the `src' attribute of
> SCRIPT elements is specified to be of type URI⁽¹⁾, that is, a URI or URI-
> reference as specified in RFC 3986⁽²⁾ (which obsoletes RFC 2396, which
> updates RFC 1738, which is therefore referred in the HTML 4.01
> Specification⁽³⁾).
>
> An empty URI-reference is specified to be a same-document URI-reference.⁽⁴⁾
> You can find that, for example, with the `action' attribute of FORM
> elements, too, primarily if the document is dynamically generated by a
> server-side application (like PHP).⁽⁵⁾
>
> You're welcome
>
> Regards,
>
> PointedEars


Wouldn't the same logic apply to an IMG tag with src="" that's being
written using document.write? As far as can be observed, it doesn't seem
to suffer from the same behavior and simply sits there waiting for
someone to come along and set it's src attribute.
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      06-03-2010
Joe Nine wrote:

> Thomas 'PointedEars' Lahn wrote:
>> An empty URI-reference is specified to be a same-document
>> URI-reference.⁽⁴⁾ You can find that, for example, with the `action'
>> attribute of FORM elements, too, primarily if the document is dynamically
>> generated by a server-side application (like PHP).⁽⁵⁾

>
> Wouldn't the same logic apply to an IMG tag with src="" that's being
> written using document.write?


IMG _element_: Yes, it would.

> As far as can be observed, it doesn't seem to suffer from the same
> behavior and simply sits there waiting for someone to come along and
> set it's src attribute.


Do not rely on that; a user agent may prune pointless requests, but it does
not need to. Do not use empty URI-references anywhere unless you mean it.


PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm> (404-comp.)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dumb, Dumb Vista Au79 Computer Support 4 02-11-2007 03:40 PM
Dumb, dumb dumb Qestion David Napierkowski Digital Photography 6 10-31-2004 11:14 PM
About C books- dumb question, but I don't really know who to ask.. sap6210@rit.edu C Programming 1 07-16-2004 02:24 PM
Really stumped on router connected to time warner road runner internet Eugene Cisco 4 04-26-2004 01:50 AM
dumb newbie question (or newbie dumb question) Jerry C. Perl Misc 8 11-23-2003 04:11 AM



Advertisments