Jump to content

Reproducing HTML within Javascript


AndrewAK

Recommended Posts

The folowing is a snippet of an array that reproduces an HTML <a> element. I ran my entire page through the W3C validateor and I get the errors listed at the bottom.

var pausecontent=new Array()pausecontent[0]=("<a href='http://www.travel.state.gov/passport/passport_1738.html'>U.S. Department of State</a><p>If you have plans to ride with us to Milwaukee in the Fall, it would be a good time to get your passport application started.</p>")pausecontent[1]=("<a href=''></a><br />Be sure to get your bike checked by a competant mechanic before the new riding season begins. Also be sure to continuously check the tread on your tires. You may need new rubber. ")pausecontent[2]=("<a href=''>Open Link</a> Put some other new information here to fill up the empty space.")

The errors the W3C validateor listed. Line 15, Column 83: document type does not allow element "a" here.…tate.gov/passport/passport_1738.html'>U.S. Department of State</a><p>If you h Line 15, Column 114: document type does not allow element "p" here.….html'>U.S. Department of State</a><p>If you have plans to ride with us to MiThere were 19 errors total. The two listed above are examples of most, if not all of the errors.When I refreshed the page in my browser everything worked fine. The code just didn't validate. So, my question is...What is the correct way to reproduce HTML/XHTML within Javascript?

Link to comment
Share on other sites

You can either cut the string so the validator doesn't read an element there, or you can put the javascript within a <![CDATA[ ]]> section. The validator is validating it as markup because your script is probably inline.Here's an example using the CDATA tag:

<script type="text/javascript">//<![CDATA[var pausecontent=new Array()pausecontent[0]=("<a href='http://www.travel.state.gov/passport/passport_1738.html'>U.S. Department of State</a><p>If you have plans to ride with us to Milwaukee in the Fall, it would be a good time to get your passport application started.</p>")pausecontent[1]=("<a href=''></a><br />Be sure to get your bike checked by a competant mechanic before the new riding season begins. Also be sure to continuously check the tread on your tires. You may need new rubber. ")pausecontent[2]=("<a href=''>Open Link</a> Put some other new information here to fill up the empty space.")//]]></script>

Link to comment
Share on other sites

  • 2 weeks later...

Isn't this one of the reasons why comments added when a <script> is inserted into the <body> section?Ex.

<script type="text/javascript"><!--var pausecontent=new Array()pausecontent[0]=("<a href='http://www.travel.state.gov/passport/passport_1738.html'>U.S. Department of State</a><p>If you have plans to ride with us to Milwaukee in the Fall, it would be a good time to get your passport application started.</p>")pausecontent[1]=("<a href=''></a><br />Be sure to get your bike checked by a competant mechanic before the new riding season begins. Also be sure to continuously check the tread on your tires. You may need new rubber. ")pausecontent[2]=("<a href=''>Open Link</a> Put some other new information here to fill up the empty space.")//--></script>

Link to comment
Share on other sites

No, people started adding comments to Javascript code when there were still browsers around that didn't understand a script tag and would just print the code out on the page. Those browsers no longer exist so there's not much of a reason to stick an HTML comment inside Javascript or CSS code.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...