PDA

View Full Version : General Javascript questiom


mkascher
12-03-2009, 04:32 PM
What does the below do and what are the differences of using this vs not using them? I'm only referring to the following tags ("<!--" and "//-->")

Thanks Michael

<script type="text/JavaScript">
<!--

function userMessage()
{
alert('example message to user');
}

//-->
</script>

abduraooft
12-04-2009, 08:32 AM
It's an outdated way of commenting out CDATA in the javascript to avoid issues with xhtml validator. Read http://javascript.about.com/library/blxhtml.htm

Iceheart
12-04-2009, 08:43 AM
It's use is very simple, to hide the JavaScript code

It can be used to hide broken JavaScript codes or to hide them from browsers that can't support JavaScript codes (which is very unusual).

But in my own opinion: If your JavaScript code is located in the <head>/</head> section, it's just a waste of time to add this codes because placing it in the <head>/</head> automatically hides it.

So no problem about it ^_^

abduraooft
12-04-2009, 09:16 AM
But in my own opinion: If your JavaScript code is located in the <head>/</head> section, it's just a waste of time to add this codes because placing it in the <head>/</head> automatically hides it.

So no problem about it ^_^No, that's not true, try validating the following code<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<title>Test</title>
<script type="text/javascript">
window.onload=function(){
var links=document.links;
for(var i=0;i<links.length;i++)
//something
}
</script>
</head>
<body>
<div></div>

</body>
</html>

Iceheart
12-04-2009, 09:48 AM
I'm very sorry sir, but I think the <!-- //--> code is used only for the things I mentioned (and also for many things )

But for assuring my statement, I've searched my favorite search engine for the keyword "how to hide a javascript code" and I've found some
websites to prove it ^_^

-http://www.chami.com/tips/internet/013097I.html
-http://www.java2s.com/Code/JavaScript/Development/UsingHTMLCommentstoHideJavaScriptCode.htm

Validation Output: 6 Errors

1. Error Line 9, Column 27: character ";" not allowed in attribute specification list

for(var i=0;i<links.length;i++)

2. Error Line 9, Column 27: element "links.length" undefined

for(var i=0;i<links.length;i++)

You have used the element named above in your document, but the document type you are using does not define an element of that name. This error is often caused by:
* incorrect use of the "Strict" document type with a document that uses frames (e.g. you must use the "Frameset" document type to get the "<frameset>" element),
* by using vendor proprietary extensions such as "<spacer>" or "<marquee>" (this is usually fixed by using CSS to achieve the desired effect instead).
* by using upper-case tags in XHTML (in XHTML attributes and elements must be all lower-case).
3. Error Line 12, Column 9: end tag for "links.length" omitted, but OMITTAG NO was specified

</script>

You may have neglected to close an element, or perhaps you meant to "self-close" an element, that is, ending it with "/>" instead of ">".
4. Info Line 9, Column 13: start tag was here

for(var i=0;i<links.length;i++)

5. Error Line 9, Column 26: XML Parsing Error: error parsing attribute name

for(var i=0;i<links.length;i++)

6. Error Line 9, Column 26: XML Parsing Error: attributes construct error

for(var i=0;i<links.length;i++)

7. Error Line 9, Column 26: XML Parsing Error: Couldn't find end of Start Tag links.length line 9

for(var i=0;i<links.length;i++)

--- It doesn't speak about not putting JavaScript comment tags ------

abduraooft
12-04-2009, 09:52 AM
--- It doesn't speak about not putting JavaScript comment tags ------ May be, but you can avoid those errors by adding the CDATA comments like
<script type="text/javascript">
/* <![CDATA[ */
window.onload=function(){
var links=document.links;
for(var i=0;i<links.length;i++)
//something
}
/* ]]> */
</script>
Anyway, the best solution is to put all javascript code in an external js file.

Iceheart
12-04-2009, 09:54 AM
Sir I am respecting your comment, but I think you've done a mistake:


I opened the page you've mentioned : http://javascript.about.com/library/blxhtml.htm

What your saying (the <![CDATA[ ]]> ]]> code) is not the same of what mkascher is mentioning (the <!-- // --> code that is for hiding the JavaScript code).

^_^ but thank you for mentioning that code, my knowledge grew ^_^

Iceheart
12-04-2009, 10:17 AM
Ye. I think its the best way ^_^

For eyesore removal and for organizing files ^_^