SitePoint Sponsor

User Tag List

Results 1 to 17 of 17
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2004
    Location
    seattle.usa
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    two javascript 'open book' test questions

    hello,
    i'm taking a javascript class and we have an 'open book' test. there are two questions i'm having a hard time answering, and i'm hoping somebody could tell me the answers to these last two True or False questions.

    12.) By default, most browsers elect to display JavaScript errors to the user.
    True or False.

    17.) Inside JavaScript's <script> tags, we can place common HTML.
    True or False.

    thanks for any help!

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,161
    Mentioned
    190 Post(s)
    Tagged
    2 Thread(s)

    True and False

    Those are more like True and False.
    The questions need to be qualified.
    By default, most browsers elect to display JavaScript errors to the user
    If by "most" you mean most used, then that would be IE. IE shows an error alert in the status bar that can be clicked on to show a (slighty) more detailed message. If you mean most individual browsers of all possible browser types, then I'd have to guess no, but I don't have experience using all possible browsers, so it's only a guess.
    Inside JavaScript's <script> tags, we can place common HTML
    "common" HTML inside of script tags? That depends if you consider tags inside of document.write to be common or not.

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i'm taking a javascript class and we have an 'open book' test.
    An open book test is a test where you can look at any of your materials in order to try and figure out the answers yourself. A teacher does not give an open book test in order to grade your ability to persuade other people to answer the problems for you.

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,813
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    The only HTML tag I am aware of that can be placed inside a <script></script> element is a <!-- --> comment which in version 1 and very modern browsers comments out the Javascript but is ignored in old browsers other than the really early one. It was used when Version 3 browsers were the most modern available to hide the Javascript from the browses that didn't understand it. That form of code ceased to be useful about 10 years ago and now actually conflicts with some browsers and server side scripts (eg. CMS) where it has the effect of commenting out the entire script.

    The answer to the question on error messages depends on when the question was written as the answer changes over time with new browser releases. With the thousands of different browsers that exist today it would be impossible to say which way most default to handling Javascript errors. They can even default differently depending on the operating system you install onto.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    SitePoint Enthusiast
    Join Date
    Sep 2004
    Location
    seattle.usa
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mittineague View Post
    Those are more like True and False.
    The questions need to be qualified.
    exactly..

    @ 7stud
    calm down.
    the test is open book/open computer, and like the 'real world' we're encouraged to find answers wherever possible.

    @ felgall and Mittineague
    Thanks for both your input!!! helpful and informative!

    happy new year!

  6. #6
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The only HTML tag I am aware of that can be placed inside a <script></script> element is a <!-- --> comment
    Code:
    <script type="text/javascript">
    
    document.write("<div>Hello world!</div>");
    
    </script>

  7. #7
    Caveat surfer Buddy Bradley's Avatar
    Join Date
    May 2003
    Location
    Cambridge, UK
    Posts
    2,366
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    The only HTML tag I am aware of that can be placed inside a <script></script> element is a <!-- --> comment
    That's not an HTML tag, it's an SGML comment. </pedant>

  8. #8
    &#083;itePoint Aficionado JVLB's Avatar
    Join Date
    Jan 2002
    Location
    N 44 56.537' W 123 3.683'
    Posts
    1,127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But, then, since all HTML is a subset of SGML, couldn't the argument be made that all HTML tags are SGML as well?

  9. #9
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,813
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by 7stud View Post
    Code:
    <script type="text/javascript">
    
    document.write("<div>Hello world!</div>");
    
    </script>
    That's not an HTML tag, it is a Javascript document.write statement - what is being written is then interpreted by the browser as HTML. Anything interpreted directly as HTML would break the script (eg if the browser treats an unescaped closing script tag as HTML then it would end the script early which is why all </ for HTML tags should be coded as <\/ within Javascript).

    That's not an HTML tag, it's an SGML comment. </pedant>
    <!-- --> is the HTML tag for comments as well as the SGML one (HTML is based on SGML after all). Why anyone would want to put their Javascript inside a comment and risk having it treated as a comment rather than as javascript I don't know but it is the only non-Javascript that is permitted inside the script tags (regardless of what language you consider it to be).
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  10. #10
    bronze trophy
    Join Date
    Dec 2004
    Location
    Sweden
    Posts
    2,670
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If we are to nitpick on SGML terminology then you're all wrong...

    <!-- --> is not a tag, it's a comment declaration (containing one comment).

    <script> in HTML is a CDATA element (that's declared in the DTD), and hence, it can only contain text. It cannot contain any markup. Things that look like markup inside CDATA elements are interpreted as text, except end tags (which should, by SGML rules, terminate the element).

    So <script><!-- --></script> is not a script element containing a comment declaration, it is a script element containing the text "<!-- -->".

    HTML, according to HTML4, is not a subset of SGML. It is an application of SGML.
    Last edited by zcorpan; Jan 8, 2007 at 21:39. Reason: typo
    Simon Pieters

  11. #11
    &#083;itePoint Aficionado JVLB's Avatar
    Join Date
    Jan 2002
    Location
    N 44 56.537' W 123 3.683'
    Posts
    1,127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the clarification. For my further edification, I have often seen XML described as a subset of SGML. Can I assume from your description, it also is an application and not a subset? And, if an application uses a reduced set of SGML's definitions, is that not a subset?

  12. #12
    bronze trophy
    Join Date
    Dec 2004
    Location
    Sweden
    Posts
    2,670
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by JVLB View Post
    Thanks for the clarification. For my further edification, I have often seen XML described as a subset of SGML. Can I assume from your description, it also is an application and not a subset?
    XML is a meta markup language which is a subset of SGML. Other languages can in turn be applications of XML (e.g. XHTML).
    Quote Originally Posted by JVLB View Post
    And, if an application uses a reduced set of SGML's definitions, is that not a subset?
    Perhaps, but HTML4 does not restrict itself to a subset of SGML's features, although real-world implementations of HTML4 do (which typically don't use SGML parsers at all).
    Simon Pieters

  13. #13
    &#083;itePoint Aficionado JVLB's Avatar
    Join Date
    Jan 2002
    Location
    N 44 56.537' W 123 3.683'
    Posts
    1,127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Got it! Thanks again.

  14. #14
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,813
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by zcorpan View Post
    So <script><!-- --></script> is not a script element containing a comment declaration, it is a script element containing the text "<!-- -->".
    Most CMS don't parse the script element as CDATA though and will treat the <!-- --> as commenting out the script. One of the differences between the standards and the real world.

    In any case <!-- immediately following the script tag is the only thing that can be included within a script element that contains Javascript which is not Javascript and which will not produce an error when processing the Javascript.

    One of the differences between HTML and XHTML is that XHTML considers the content of a script element to be PCDATA and not CDATA.

    So what we end up with is that a Javascript contained within HTML cannot contain any HTML tags within the Javascript unless they are within text to be processed by the Javascript in which case </ needs to be escaped in order to avoid premature termination of the script. Surrounding the Javascript with <!-- --> in this case serves no purpose whatsoever except to make the file slightly bigger. The only version of HTML where this code did make a difference was HTML 1.0 which didn't have a script element but did allow comments.

    Javascript contained within a script element in XHTML can be surrounded by <!-- --> if you want to comment out the entire Javascript but no other XHTML is permitted unless it is within the text being processed by the Javascript where again </ needs to be escaped.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  15. #15
    bronze trophy
    Join Date
    Dec 2004
    Location
    Sweden
    Posts
    2,670
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    Most CMS don't parse the script element as CDATA though and will treat the <!-- --> as commenting out the script.
    Then that's a bug in the CMSs.
    Quote Originally Posted by felgall View Post
    In any case <!-- immediately following the script tag is the only thing that can be included within a script element that contains Javascript which is not Javascript and which will not produce an error when processing the Javascript.
    That's a hack in the javascript engine, not in HTML. The string <!-- is still passed to the JS engine, but the JS engine knows to ignore it (and the rest of that line). Other scripting languages which you might be able to place in <script> might not have this hack, so then <!-- after <script> wouldn't work. In conclusion, <!-- is actually javascript. (CSS has a similar hack, and also for --> at the end.)
    Quote Originally Posted by felgall View Post
    One of the differences between HTML and XHTML is that XHTML considers the content of a script element to be PCDATA and not CDATA.
    Correct.
    Quote Originally Posted by felgall View Post
    So what we end up with is that a Javascript contained within HTML cannot contain any HTML tags within the Javascript unless they are within text to be processed by the Javascript in which case </ needs to be escaped in order to avoid premature termination of the script. Surrounding the Javascript with <!-- --> in this case serves no purpose whatsoever except to make the file slightly bigger. The only version of HTML where this code did make a difference was HTML 1.0 which didn't have a script element but did allow comments.
    HTML 2.0 didn't have <script> either, FWIW. But the hack wasn't to be backwards compatible with older HTML specs, it was to be backwards compatible with at the time existing implementations, in particular Mosaic 1.0 and Netscape 2.0, which didn't use SGML parsers. (If they did, then this hack wouldn't exist. A real SGML parser would read the DTD and know which elements were CDATA elements, and hence <!-- would just be text to them.)
    Quote Originally Posted by felgall View Post
    Javascript contained within a script element in XHTML can be surrounded by <!-- --> if you want to comment out the entire Javascript but no other XHTML is permitted unless it is within the text being processed by the Javascript where again </ needs to be escaped.
    </ doesn't need to be escaped in XHTML, or at least not as <\/. < and &, however, needs to be escaped just like anywhere else in XML.
    Simon Pieters

  16. #16
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,813
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Now you are getting your Javascript and your XHTML mixed up. I didn't say </ needed to be escaped within XHTML I said it needs to be ecsaped when it is in the text within your Javascript so as to ensure that it isn't confused for something that it is not by browsers and other code that don't follow the standards properly. If a CMS is going to incorrectly parse a </script> in the middle of your text inside of the Javascript incorrectly as the end of the Javascript then specifying it as <\/script> corrects the problem. That escape is always valid in Javascript and even though XHTML is supposed to be able to recognise that the </script> is in fact not the end of the script because the CDATA tag hasn't been ended yet you can't expect everything updating the content of the page to follow the proper rules. After all which web browsers actually read the DOCTYPE so as to see how they are supposed to parse the web page rather than ignoring its content and parsing the page the way that they have hard coded into them even where it runs contrary to the doctype.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  17. #17
    bronze trophy
    Join Date
    Dec 2004
    Location
    Sweden
    Posts
    2,670
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    Now you are getting your Javascript and your XHTML mixed up.
    Not as far as I can tell.
    Quote Originally Posted by felgall View Post
    I didn't say </ needed to be escaped within XHTML I said it needs to be ecsaped when it is in the text within your Javascript
    What's the difference? The XML parser doesn't know or care what is "XHTML" or what is "javascript". The same parsing rules apply regardless.
    Quote Originally Posted by felgall View Post
    so as to ensure that it isn't confused for something that it is not by browsers and other code that don't follow the standards properly.
    All XHTML browsers I know of use a conforming XML parser for XML content so I don't see what would confuse.
    Quote Originally Posted by felgall View Post
    If a CMS is going to incorrectly parse a </script> in the middle of your text inside of the Javascript incorrectly as the end of the Javascript then specifying it as <\/script> corrects the problem. That escape is always valid in Javascript and even though XHTML is supposed to be able to recognise that the </script> is in fact not the end of the script because the CDATA tag hasn't been ended yet you can't expect everything updating the content of the page to follow the proper rules.
    If your tools are broken then fix them...
    Quote Originally Posted by felgall View Post
    After all which web browsers actually read the DOCTYPE so as to see how they are supposed to parse the web page rather than ignoring its content and parsing the page the way that they have hard coded into them even where it runs contrary to the doctype.
    The doctype doesn't determinate how the document should be parsed. The MIME type fulfills that purpose.
    Simon Pieters


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •