What is the correct way to document.write forwards slashes? document.write("<b>Hello</b>"); Works fine, but gives me an HTML validation error document.write("<b>Hello<\/b>"); Works fine, but writes out server side invalid HTML Suggest me plz?
document.write("<b>Hello</b>"); Works fine, but gives me an HTML validation error perhaps because <b> is deprecated and you should use <strong> instead? you don't need to escape / just " in this instance anyway.
I get the validation error because of the /, not the <b>. Same thing happens when I use <strong> Any ideas? It's not really a big deal, I'm just a bit obsessive about validation
the case about strong is true anyway you can't validate document.write content as it's not been written yet and it is not a part of the dom. you can do so on-the-fly after it writes which is what i assumed you'd be doing. hence your validator is inherently wrong as it is trying to parse your script (presumably within the correct script tags???) as html... check to see how you've defined / encapsulated your js code here...
I tried it, but it didn't make any difference to the validation. Can one of you JS nerds [] give me a complete example of how to document.write bold/strong/whatever tags? As I said before, it's not really a big deal - my site works fine either way. I'd just like to make it as clean as possible.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>Title</title> <meta http-equiv="content-type" content="text/html;charset=utf-8" /> <meta http-equiv="Content-Style-Type" content="text/css" /> <link rel='stylesheet' type='text/css' href='style.css' /> </head> <body> <div> <script type="text/javascript">//<![CDATA[ document.write('<b>Bold text!</b>'); //]]> </script> </div> </body> </html> Code (markup):
joebert's code should work, but if it does not, you may try with document.write('<b>Bold text!<' + '/b>'); Code (markup): BTW nowadays using document.write isn't a very good call anyway, if you are seeking clean code (it is so '90s)... if you expose us the problem, with every probability there will be another cleaner solution.
@ lordofthelake - that's fixed it. I'm using copy and pasted JS from eBay Partner Network, it's a real mess. Thanks, at least it appears to be valid now.