document.write backslashes like </b>?

Discussion in 'JavaScript' started by Kerosene, Dec 4, 2008.

  1. #1
    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?
     
    Kerosene, Dec 4, 2008 IP
  2. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #2
    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.
     
    dimitar christoff, Dec 5, 2008 IP
  3. Kerosene

    Kerosene Alpha & Omega™ Staff

    Messages:
    11,366
    Likes Received:
    575
    Best Answers:
    4
    Trophy Points:
    385
    #3
    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 :p
     
    Kerosene, Dec 5, 2008 IP
  4. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #4
    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...
     
    dimitar christoff, Dec 5, 2008 IP
  5. Logic Ali

    Logic Ali Well-Known Member

    Messages:
    170
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    108
    #5
    Put your script inside a CDATA island.
     
    Logic Ali, Dec 5, 2008 IP
  6. Kerosene

    Kerosene Alpha & Omega™ Staff

    Messages:
    11,366
    Likes Received:
    575
    Best Answers:
    4
    Trophy Points:
    385
    #6
    I tried it, but it didn't make any difference to the validation.

    Can one of you JS nerds [:p] 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.
     
    Kerosene, Dec 5, 2008 IP
  7. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #7
    <!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, Dec 6, 2008 IP
  8. lordofthelake

    lordofthelake Peon

    Messages:
    70
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    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, Dec 7, 2008 IP
    Kerosene likes this.
  9. Kerosene

    Kerosene Alpha & Omega™ Staff

    Messages:
    11,366
    Likes Received:
    575
    Best Answers:
    4
    Trophy Points:
    385
    #9
    @ 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.
     
    Kerosene, Dec 7, 2008 IP
  10. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #10
    Or just put it where it belongs, in an external file.
     
    deathshadow, Dec 7, 2008 IP