1

I believe that I've identified a bug in Opera (version 12.01 running on Windows 7), but am looking for assistance with a possible workaround (presuming that others can confirm that this is a bug).

If I have an HTML page containing the following:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Test</title>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" />
  </head>
  <body onload="window.alert(0);">
  </body>
</html>

The "onload" event is never fired. Oddly, if I close the script tag instead of self closing it, and change:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" />

to:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

(which unfortunately I can't do), the event will fire and my alert will be shown.

Had anyone else run into this problem? If so, have they found a way around it besides changing the script tag from <script /> to <script></script>?

NOTE: Yes, I have opened a bug report with Opera. No response back from them yet.

userx
  • 3,769
  • 1
  • 23
  • 33
  • Why can't you add ``? You have to! `` is actually invalid HTML, and doesn't work in any browser. :-P – gen_Eric Aug 16 '12 at 21:55
  • What was the bug number of that bug? I'll close it ;) – hallvors Aug 17 '12 at 11:42
  • The original confusion was caused by Opera's Dragonfly tool, which displays script tags as self closing () in its HTML inspector. @Rocket oddly, the W3C validator says that the above HTML is 100% compliant. See: http://validator.w3.org – userx Aug 18 '12 at 20:32

2 Answers2

3

This "bug" exists in all browsers, not only in Opera (I've tested Chrome, FF and IE).

Self-closing script tags just don't work. See this question on SO: Why don't self-closing script tags work?

EDIT:

I have no idea why you can't close the script tag with </script>, but one possible workaround would be loading the scripts with JavaScript:

var newScript = document.createElement("script");
newScript.type = "text/javascript";
newScript.src = "https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js";
newScript.onload = function(){
    // jQuery code...
};
document.getElementsByTagName("head")[0].appendChild(newScript);

(OK, admittedly this actually forces you to use </script> somewhere, but at least the part between <script> and </script> now isn't empty... :P)

Community
  • 1
  • 1
Aletheios
  • 3,960
  • 2
  • 33
  • 46
  • I don't see how your edit is true. `` works as expected, as noted in the question. – gsnedders Aug 18 '12 at 00:11
  • I never said `` wouldn't work - of course it does. But I edited my answer to provide a possibility to avoid empty `` tags that you get when loading external JS files. I don't know if this helps @userx though, as I still don't understand why it would be necessary to self-close script tags. – Aletheios Aug 18 '12 at 01:21
2

What Opera does is correct per HTML5. To understand why the onload doesn't run, you need to remember that the contents of a SCRIPT tag with SRC set should be ignored. Since /> is not a correct way to close the first SCRIPT tag, the subsequent end-of-head and start-of-body tags end up inside the SCRIPT tag. It would be a bit like writing this:

<script src="foo.js">
</head><body><p>This doesn't appear anywhere, does it?</p></body>
</script>

So onload is never set in the first place because the BODY inside the SCRIPT tag will be ignored.

hallvors
  • 6,069
  • 1
  • 25
  • 43