Former Member
inherit
guest@proboards.com
155913
0
Nov 29, 2024 3:51:36 GMT -8
Former Member
0
January 1970
Former Member
|
Post by Former Member on Aug 16, 2011 0:26:26 GMT -8
Ok curious I know that <!-- --> is used to create a comment and // is used for a single line comment. I also know that <!-- --> is used to temporarily deactivate codes for bugging reasons and such, so with that being said, what I don't understand is why coders use it inside of their script tags like so <script type="text/javascript"> <!--/* protect accounts */ var protect = /^(user1|user2|user3)$/i; if(pb_username != 'admin') { if(document.modifyForm && document.modifyForm.user.value.match(protect) && pb_username != document.modifyForm.user.value) { var obj = document.modifyForm.parentNode; obj.parentNode.parentNode.parentNode.getElementsByTagName('b').item(0).innerHTML = 'An Error Has Occured'; obj.innerHTML = 'You are not allowed to modify this persons profile.'; } } // --></script> By the way, this is just a random code I pulled out of the databaseBut as I was saying, I understand ...... now that I have basically talked it out with myself that the closing part of the comment --> is ignored because of the // before it. ( //-->) But still, my question ... or questions should I say .. is what are their purpose in this situation, and how is the top <!-- being ignored rather than the browser reading it as text? You would just think that it would be read as text because the closing --> is being ignored due to the // placed before it.
|
|
inherit
109382
0
Jan 6, 2019 9:03:49 GMT -8
uzi
1,756
August 2007
uzi
|
Post by uzi on Aug 17, 2011 12:59:30 GMT -8
Old browsers will treat the script (in between the HTML comment tags) as a long HTML comment. Newer browsers recognize this syntax as just that and ignore it.
Putting those comment tags now is pretty much pointless and should be avoided though, because the browsers that can't interpret the JavaScript are so old that pretty much nobody should have them.
The two slashes that precede the end of the HTML comment are not required as far as I am aware (besides PERHAPS in older browsers)
TL;DR: HTML markup and JavaScript are not the same thing. The HTML markup still operates as HTML markup when inside of the <script> tags, because after all, those are HTML objects, too.
|
|
Former Member
inherit
guest@proboards.com
155913
0
Nov 29, 2024 3:51:36 GMT -8
Former Member
0
January 1970
Former Member
|
Post by Former Member on Aug 17, 2011 20:18:10 GMT -8
I think I understand now. So are you saying in short, that without the HTML comment tags, the older browsers that don't recognize javascript will display it as plain text? and that method is not really even necessary anymore because all of the major browsers recognize javascript?
|
|
inherit
109382
0
Jan 6, 2019 9:03:49 GMT -8
uzi
1,756
August 2007
uzi
|
Post by uzi on Aug 17, 2011 20:23:50 GMT -8
Yes, in a nutshell. I don't have exact statistics for when browsers started support JavaScript, but I know (fairly certain) that IE started recognize script tags in IE 3. And Netscape started recognizing these tags in Netscape 2.0. It's literally been 15 years since those browsers came about.
On a side note, if you are interested in this HTML markup inside of JavaScript, you might want to know about using CDATA tags in <script>s. Technically, in order for the XHTML to validate properly, you must use CDATA tags like so:
<script type="text/javascript"> //<![CDATA[ code here //]]> </script>
Neither of these two scenarios are necessary and you might be better off just ignoring them completely until you have a better grasp of the languages.
|
|
Former Member
inherit
guest@proboards.com
155913
0
Nov 29, 2024 3:51:36 GMT -8
Former Member
0
January 1970
Former Member
|
Post by Former Member on Aug 17, 2011 21:06:50 GMT -8
CDATA ? ;D it never stops does it............ Well thanks for your answers, they were very useful and helped answer a question that popped in my head just about every time that I seen them HTML comment markers. Much appreciated
|
|
inherit
130228
0
Jul 11, 2024 19:19:59 GMT -8
Charles Stover
1,731
August 2008
gamechief
|
Post by Charles Stover on Aug 18, 2011 9:55:34 GMT -8
You can just ignore <!-- and --> inside of <script>. It was used, as said, so that old browsers that didn't know what JavaScript was wouldn't just output the code (imagine replacing the <script> tag with <div>).
I'm pretty sure 100% of browsers viewing your website recognize the JavaScript tag these days, and anyone who doesn't will be aware of the issue with JavaScript and, I imagine, only using such an old browsers for dookies and giggles.
So don't bother even putting <!-- --> in scripts. A lot of people don't anyway, and it's already been replaced by the modern alternative <![CDATA[ ]]>.
|
|
inherit
77753
0
Jul 18, 2024 12:23:50 GMT -8
Bob
2,623
April 2006
bobbyhensley
|
Post by Bob on Aug 19, 2011 4:04:41 GMT -8
Character data. It means the enclosed should be interpreted as straight characters instead of being read into the document structure. It's used to make sure the script isn't interpreted by the document at all. Often times there is markup embedded in a script (example: a variable containing a markup string to be printed out at a later time). It's possible for a browser to actually read that string as valid markup when it shouldn't. CDATA sections have more uses than just defining script bodies though. Printing markup to the screen, for example. In the past people would make sure the markup wasn't interpreted by using character codes for the tag brackets (& lt; and & gt;): Code: <strong>This might be a tutorial demonstrating the strong element.</strong>Output (to screen): <strong>This might be a tutorial demonstrating the strong element.</strong>Alternatively you can use CDATA: Code: <![CDATA[<strong>This might be a tutorial demonstrating the strong element.</strong>]]>Output (to screen): <strong>This might be a tutorial demonstrating the strong element.</strong>Obviously the latter is preferable to write out and maintain.
|
|