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.
Page 1 of 3 123 LastLast
Results 1 to 15 of 31
  1. #1
    Regular Coder
    Join Date
    Sep 2002
    Location
    Scotland
    Posts
    407
    Thanks
    0
    Thanked 0 Times in 0 Posts

    correct way to include a javascript in XHTML document

    I have this javascript to spam-protect my email address:

    Code:
    <script language="javascript">
    	var name = "brian";
    	var domain = "sweenster.co.uk";
    	document.write('<a href=\"mailto:' + name + '@' + domain + '\">');
    	document.write(name + '@' + domain + '</a>');
    </script>
    this works fine, but whenever I try to validate my code it fails - because of the language attribute and the fact that i'm printing HTML tags.

    How would I fix this??

    I have tried removing, changing & manipultaing both but with no success.
    My body's a temple... and like those ancient Greek ones it's a ruin
    sweenster.co.uk

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeň, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    First of all, language attribute is deprecated. Use type="text/javascript" instead. Likewise, don't wrap your script in a comment as xml compliant browsers shouldn't parse comments even though they doesn't have to discard them.

    As for the real problem, you can take multiple approaches to this:
    - Use an external file instead.
    - Escape &<>" with &amp;&lt;&gt;&quot; in the script (and suffer the browser incompatibilities this leads to).
    - Use <![CDATA[ ...script contents... ]]> to allow any characters inside the script element
    Last edited by liorean; 11-07-2005 at 11:19 PM.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    Regular Coder
    Join Date
    Sep 2002
    Location
    Scotland
    Posts
    407
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by liorean

    As for the real problem, you can take multiple approaches to this:
    - Use <[CDATA[ ...script contents... ]]> to allow any characters inside the script element
    i'm intregued by this CDATA command. can you explain it??

    as for using &lt; and &gt; etc - they wont work, it has to be < or > or it wont recognise it as a HTML tag.
    My body's a temple... and like those ancient Greek ones it's a ruin
    sweenster.co.uk

  • #4
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeň, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    The CDATA command tells the xml browser that the contents are of the form Character DATA, not of the default form Parsed Character DATA. This means that you can use the &<>" characters safely within the CDATA block.

    As for the escaping of the characters &<>", I said the browser support was lousy (or rather that you had to suffer the browser incompatibilities, which essentially means thesame thing). By that I meant browser support is pretty much non-existant for any HTML browser. An XML only browser might be able to manage, however..
    Last edited by liorean; 09-16-2003 at 07:27 PM.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #5
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeň, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Hmm, considering the reason for the script, why don't you simply place the tag in the html again and replace the @ with &#0064; to protect the email addy from getting caught by spam targets collectors?
    Last edited by liorean; 11-07-2005 at 11:20 PM.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #6
    Regular Coder
    Join Date
    Sep 2002
    Location
    Scotland
    Posts
    407
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by liorean
    Hmm, replace the @ with &amp;#0064; to protect the email addy from getting caught by spam targets collectors?
    good thinking, batman!
    My body's a temple... and like those ancient Greek ones it's a ruin
    sweenster.co.uk

  • #7
    Moderator
    Join Date
    May 2002
    Location
    Hayward, CA
    Posts
    1,459
    Thanks
    1
    Thanked 23 Times in 21 Posts
    Originally posted by liorean
    First of all, language attribute is deprecated. Use type="text/javascript" instead.
    Netscape also supports an application/x-javascript content-type for JavaScripts. It's technically more correct, as text/javascript has not been registered as an official content-type.

    As for the deprecated language attribute -- if you're using XHTML 1.0 Transitional, why not use both? Particularly if you're serving as text/html (though XHTML as text/html isn't generally a recommended practice -- don't ask me why...)

    As for the real problem, you can take multiple approaches to this:
    - Use an external file instead.
    This is the most cross-browser solution there is. Because of irregularities in Internet Explorer, I recommend you use the open and close tags, instead of a self-closing tag:

    Code:
    <script language="JavaScript" type="application/x-javascript" src="myscript.js"></script>
    Great question, automatic sticky.
    "The first step to confirming there is a bug in someone else's work is confirming there are no bugs in your own."
    June 30, 2001
    author, Verbosio prototype XML Editor
    author, JavaScript Developer's Dictionary
    https://alexvincent.us/blog

  • #8
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeň, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Just correcting an error above <[CDATA[ ... ]]> should have been <![CDATA[ ... ]]>.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #9
    WA
    WA is offline
    Administrator
    Join Date
    Mar 2002
    Posts
    2,596
    Thanks
    2
    Thanked 19 Times in 18 Posts
    To the question of how to include a CSS file in a XML file, please see this thread: CSS style sheets in XML - Is it possible
    - George
    - JavaScript Kit- JavaScript tutorials and 400+ scripts!
    - JavaScript Reference- JavaScript reference you can relate to.

  • #10
    Regular Coder
    Join Date
    May 2002
    Location
    Helsinki, Finland
    Posts
    231
    Thanks
    0
    Thanked 1 Time in 1 Post

    Thumbs up Re: correct way to include a javascript in XHTML document

    I've managed with "comment" marks:

    Code:
    <script type="text/javascript">
    <!--
    	var name = "brian";
    	var domain = "sweenster.co.uk";
    	document.write('<a href=\"mailto:' + name + '@' + domain + '\">');
    	document.write(name + '@' + domain + '</a>');
    // -->
    </script>
    
    <style type="text/css">
    <!--
    	mySelector {property:value;...}
    -->
    Zvona
    First Aid for
    Web Design

  • #11
    Moderator
    Join Date
    May 2002
    Location
    Hayward, CA
    Posts
    1,459
    Thanks
    1
    Thanked 23 Times in 21 Posts
    The main problem with that, Zvona, is that in "standards" mode (as opposed to quirks mode) a browser may decide to completely omit the contents of the comment. Mozilla is particularly good at that.

    Not only that, but the -- characters will trigger an XML well-formedness error.
    "The first step to confirming there is a bug in someone else's work is confirming there are no bugs in your own."
    June 30, 2001
    author, Verbosio prototype XML Editor
    author, JavaScript Developer's Dictionary
    https://alexvincent.us/blog

  • #12
    Regular Coder
    Join Date
    Nov 2002
    Posts
    672
    Thanks
    1
    Thanked 1 Time in 1 Post
    When was the <![CDATA[ ]]> introduced? I'm seeing it used more and more today, but I don't know how many browsers will recognize it. Is it safe to say that this can substitute the <!-- --> comment tags? Or should we put the <![CDATA[ ]]> inside them?

  • #13
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeň, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    The CDATA blocks were introduced in the XML spec.

    They are safe to use today, if you make sure browsers that don't handle them ignore them. This can be donoe through the following:
    Code:
    <script type="text/javascript">
    // <![CDATA[
    ...
    // ]]>
    </script>
    And for CSS:
    Code:
    <style type="text/css">
    /* <![CDATA[ */
    ...
    /* ]]> */
    </style>


    The reason for using the CDATA block in XHTML pages:
    XHTML is XML, and in XML elements cannot default to CDATA content. Content of elements is ALWAYS parsed, and if it is not well formed, it will cause an error (well, not when sent as 'text/html', but you really shouldn't do that unless the client lacks support for 'application/xhtml+xml'). Since the contents of script and style elements in HTML4.01 was of CDATA type instead of PCDATA type, you could use the characters < and & in it freely. That is not the case in XHTML where the contents model is PCDATA. The CDATA block allows you to write any character sequence except ]]> in the contents of the block, without it being parsed as XML. This allows you to use the same characters as you could in the HTML4.01 contents model.

    Why you should not use SGML comments:
    - XHTML is XML. In XML the content of comments shouldn't be parsed, thus, the script shouldn't run.
    - You may not use the -- character sequence inside a comment in XML.


    Browser support:
    - If you use the CSS or JavaScript comments like I provided above, the CDATA block can safely be inserted into the document even for browsers that don't support them.
    - The browsers that don't handle the CDATA block will choke on the </ sequence inside a script like they did in HTML4.01, which browsers that support the CDATA block will not.
    - Browsers only support the CDATA block in XML ('text/xml' or 'application/xml') or XHTML ('application/xhtml+xml') modes, not in HTML ('text/html') mode.
    - All browsers that support XML and/or XHTML mode support the CDATA block.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #14
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    All of which implies then that using CDATA to replace the old comment-hack is not generally a good idea - it's only okay for pages in XHTML mode. Yes?
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

  • #15
    Moderator
    Join Date
    May 2002
    Location
    Hayward, CA
    Posts
    1,459
    Thanks
    1
    Thanked 23 Times in 21 Posts
    It's only okay if the user-agent knows how to recognize it. Unless you can guarantee that only user-agents you know about will look at the page, it's better for you to assume it won't work.

    This thread, being a sticky, will have some comments removed for brevity. (Liorean, if you want to do this first, be my guest -- and delete this comment while you're at it.)
    "The first step to confirming there is a bug in someone else's work is confirming there are no bugs in your own."
    June 30, 2001
    author, Verbosio prototype XML Editor
    author, JavaScript Developer's Dictionary
    https://alexvincent.us/blog


  •  
    Page 1 of 3 123 LastLast

    Posting Permissions

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