Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New Coder
    Join Date
    May 2007
    Location
    NW USA
    Posts
    63
    Thanks
    0
    Thanked 2 Times in 1 Post

    How to validate a page with javascript variables in the <head> section?

    CF--

    Please excuse me if this is not the forum in which to post this. I have a page that validates as XHTML 1.1 before I use a script, but shows an error if I use it because the javascript has a variable that assigns an arbitrary name to an image that is to be used further down on the page:

    Code:
    var myPic = '<img src="myImage.jpg" width="100" height="200" alt="" />'
    The validator tells me that document type does not allow element "img" here.

    The purpose of the script is to show an image as a 'pop-up' over the main page (unhide a <div> then hide it again--kind of like a giant tooltip). I would like it to do this rather than send me to a new blank page with the image on it which then requires hitting the BACK button to get back to my page.

    Any suggestions would be appreciated as I have several scripts that have this sort of variable to pass an image to the page and of course, all of them cause errors on validation attempt.

    My pages are on a local intranet so I cannot provide a link, but if necessary, I will create a dummy page or provide the entire script to show what I mean.

    Thank you for any help/suggestion you can provide.

    lorie

  • #2
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,624
    Thanks
    6
    Thanked 1,001 Times in 974 Posts
    Arbitrator might intervene here and try to arbitrate… …but I suppose you have internal JavaScript which is enclosed in the usual comments?
    Code:
    <script>
    // <!--
    (stuff here)
    // -->
    </script>
    This is actually wrong and hiding the content (the script) completely in XHTML. You should denote your script content as CDATA to allow for characters that would usually lead to validation errors (as you can see). This is the correct way:
    Code:
    <script type="text/javascript">
    // <![CDATA[
    (stuff here)
    // ]]>
    </script>
    Search the web for “CDATA” or browse the W3C website for more info on that… or ask the Arbitrator. I always keep forgetting the correct definition/explanation.

    However, you should get used to put your CSS and JavaScript in external files and only put a reference to those files in the head section. This will keep your pages/code clean and prevents such issues.

  • #3
    Senior Coder
    Join Date
    Jul 2005
    Location
    UK
    Posts
    1,051
    Thanks
    6
    Thanked 13 Times in 13 Posts
    I went and looked up CDATA and it seems to me like this should be rolled out so that it works within the HTML body as well.

    That would stop us from having to use character encoding for say, when you want to show some HTML markup on a page without the HTML being parsed. You could just enclose the markup in CDATA tags instead.

    Tried it though and evidently it doesn't work.

    Does anyone know if there are any long term plans for this by the W3C?

  • #4
    New Coder
    Join Date
    May 2007
    Location
    NW USA
    Posts
    63
    Thanks
    0
    Thanked 2 Times in 1 Post
    Thanks so much for the prompt reply. That works. (And yes, the snippet of code I posted would be inside of the proper <script></script> block...)

    As far as using an external .js page: would I need to make a new page to reference for every HTML page that uses this script? Several of the scripts I use have arrays that might hold 10 or 15 references to images. Could I make just one, having the array go into the hundreds? Wouldn't that slow loading as the HTML page would have to wade through all of that to get to the instance [becoming something like: "showPic(115, 'div1')" where 115 is the 116th item in a huge array]?

    Or if I use a separate .js for each page, that doubles the number pages in the site...

    I wonder what the W3C suggests... will try to find some info when I have time to do some extensive surfing. For now, this will solve the problem.

    Thanks, again!
    lorie

  • #5
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,616
    Thanks
    0
    Thanked 645 Times in 635 Posts
    Separating the JavaScript out from the HTML and doubling the number of files on your site will reduce the irrelavant content that the search engines see and probably improve your ranking.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #6
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,624
    Thanks
    6
    Thanked 1,001 Times in 974 Posts
    If you are calling the same function(s) on several pages it definitely makes sense to put your scripts into one external file and refer to that one file in the HTML documents that this script relates to - much as you would do with a stylesheet or server side includes.

    Quote Originally Posted by lorie View Post
    Could I make just one, having the array go into the hundreds? Wouldn't that slow loading as the HTML page would have to wade through all of that to get to the instance [becoming something like: "showPic(115, 'div1')" where 115 is the 116th item in a huge array]?
    Hm, you would need to test that (there are scripts to check how long it takes to execute). Now, I’m not the baddest JavaScript developer but I think if you are getting such huge arrays there must be a different solution to reduce the process (maybe sort out dynamic things on the server even before the HTML gets parsed). In any way, one reasonably large external JavaScript file will reduce the loading time as the JS file will be cached and loaded from your local memory then.

  • #7
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,300
    Thanks
    28
    Thanked 275 Times in 269 Posts
    Quote Originally Posted by lorie View Post
    Code:
    var myPic = '<img src="myImage.jpg" width="100" height="200" alt="" />'
    The validator tells me that document type does not allow element "img" here.
    See this post of mine on the subject. Basically, the greater‐than sign (<) character inside a script element is parsed literally in an XHTML (XML) document because the script element’s content is not predefined to be CDATA. You must manually make it a CDATA section.

    In this case, you could also avoid use of the greater‐than sign (<) character by using DOM methods instead of what I presume you are using: the non‐standard innerHTML, document.write, or document.writeln. As mentioned in the resource that I just linked to, it is improper to use the latter two in any XHTML document since they stop functioning when the document is served as XML.

    Quote Originally Posted by lorie View Post
    I have a page that validates as XHTML 1.1 before I use a script […]
    If you’re sending the XHTML document as HTML, you shouldn’t be; you’ll be able to tell since a correct XHTML document won’t work in Internet Explorer. Either use the real thing or use HTML. Using HTML, you will neither have to deal with this issue nor others that a validator will not be able to catch.

    Quote Originally Posted by Pennimus View Post
    I went and looked up CDATA and it seems to me like this should be rolled out so that it works within the HTML body as well.

    That would stop us from having to use character encoding for say, when you want to show some HTML markup on a page without the HTML being parsed. You could just enclose the markup in CDATA tags instead.

    Tried it though and evidently it doesn't work.

    Does anyone know if there are any long term plans for this by the W3C?
    HTML 4.01 is derived from SGML. As such, CDATA sections already exist in HTML and can be implemented at any browser vendor’s pleasure. The developers of the Opera browser have already done this. Here’s a test page if you want to see what browsers do with it.

    So far as I can tell, the current draft of HTML5 doesn’t define CDATA sections. It’s not based on SGML either, so their existence can’t be implied.
    For every complex problem, there is an answer that is clear, simple, and wrong.

  • #8
    New Coder
    Join Date
    May 2007
    Location
    NW USA
    Posts
    63
    Thanks
    0
    Thanked 2 Times in 1 Post
    So, then, Arbitrator, you are of the opinion that as I'm doing a massive redesign I should give my pages the HTML 4.01 strict DOCTYPE rather than the XHTML because they really are only .html, is that correct?

    I have read numerous articles on 'which DOCTYPE' and am more confused than ever. Since I don't want to have to do this yet again, I was trying to move the pages to the strictest standard. But you are right, they are not being sent as XML.

    Confused and bemused,
    lorie

  • #9
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,300
    Thanks
    28
    Thanked 275 Times in 269 Posts
    Quote Originally Posted by lorie View Post
    So, then, Arbitrator, you are of the opinion that as I'm doing a massive redesign I should give my pages the HTML 4.01 strict DOCTYPE rather than the XHTML because they really are only .html, is that correct?

    I have read numerous articles on 'which DOCTYPE' and am more confused than ever.
    Yes, I would recommend HTML 4.01. HTML 4.01 Strict is the preferable flavor. Think about why you’re using XHTML and what you’re doing that HTML can’t do. If the answer is “nothing” or “I don’t know”, then you should avoid XHTML, at least, until Internet Explorer supports it.

    Quote Originally Posted by lorie View Post
    Since I don't want to have to do this yet again, I was trying to move the pages to the strictest standard.
    By not serving it as XML, you have not actually adhered to the strictness of the standard, so the effort is moot.
    For every complex problem, there is an answer that is clear, simple, and wrong.

  • #10
    New Coder
    Join Date
    May 2007
    Location
    NW USA
    Posts
    63
    Thanks
    0
    Thanked 2 Times in 1 Post
    Thanks for all of your input.

    Now I will go and 'unclose' all of those .css and img and meta tags I so diligently 'closed'...

    Why can't everyone play nice and agree on then stick to standards? Greed, arrogance, selfishness and pride sure do make designing web pages a lot more difficult than it would have to be...

    Thanks, again!
    lorie

  • #11
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,624
    Thanks
    6
    Thanked 1,001 Times in 974 Posts
    Quote Originally Posted by Arbitrator View Post
    […] you should avoid XHTML, at least, until Internet Explorer supports it.
    Well, I was gonna wait for your opinion before I chime in again. This is a lot of stuff for discussion as there are people bringing good reasons why you should use XHTML rather than HTML and there are also people defending the “old way”.
    I would just say: Once all major browsers actually do support real XHTML it’s easier to just change the MIME type instead of having to change entire documents (e.g. “self-closing” elements and stuff). So you could serve XHTML as HTML for now and when time is right just make a small switch. And there’s no real disadvantage of using XHTML at the moment.

    Anyway, a strict doctype is definitely the only “alternative”.


  •  

    Posting Permissions

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