0
<!DOCTYPE html>
<html>
<body>

<script type="text/javascript">
<!--
    var x = 26;
    do{
        document.write(x + " et voilà! <br />");
        x++;
    }while(x<32);
//-->
</script>

</body>
</html>

I don't know why the above code doesn't work. The browser window just keeps on loading and nothing appears. I can't even use F12 in Google Chrome to debug the code. I am not sure but I think that the problem lies with the <br /> tag. <br> and <br/ > seem to work fine, but not <br /> and <br/>. (Note the spaces). Shouldn't all four work properly? (Dunno why, but the code started working. I am very, very sure that it wasn't working at first. Weird.)

Also when <br> or <br/ > are used the printed text is et voilà ! rather than et voilà! Any thoughts?

John Red
  • 658
  • 1
  • 11
  • 16
  • [plunker](http://plnkr.co/edit/4VkGraZHx2OHQ4tD0MAg?p=preview). It works but it's horrible, don't do that. – Mosho Jul 12 '14 at 08:21
  • [Self-closing tags are an XHTML thing; regular HTML doesn't have them.](http://stackoverflow.com/questions/3558119/are-self-closing-tags-valid-in-html5) Also, don't put your javascript in HTML comments. – user2357112 supports Monica Jul 12 '14 at 08:21
  • Right, but there's no harm in using them with elements which can only be empty like `
    ` (e.g., if you want polyglot X/HTML5).
    – Brett Zamir Jul 12 '14 at 08:33
  • @user2357112 Why not insert Javascript in HTML comments? Someone told me that it is a good practice. Makes browsers that cannot understand Javascript ignore them. – John Red Jul 12 '14 at 08:39
  • @Mosho What is horrible? – John Red Jul 12 '14 at 08:41
  • @RedJohn: [What browsers would those be?](http://stackoverflow.com/questions/1507939/using-html-comment-tag-still-relevant-around-javascript-code) Javascript has been around for 19 years. Are you writing the rest of your page for compatilibity with 19-year-old browsers? Are you writing your Javascript for compatibility with every Javascript engine since the original? If not, doing anything for compatibility with browsers that don't even recognize Javascript is silly. – user2357112 supports Monica Jul 12 '14 at 08:54
  • @user2357112: Agreed. It is just unnecessary, not harmful. But yeah, I won't do it in the future. – John Red Jul 12 '14 at 09:16

3 Answers3

1

"Ã" instead of "a" is a problem with UTF-8.

HTML Solution :

Set meta charset, and save your file with UTF-8 encoding :

<meta charset="UTF-8">

<meta charset="utf-8"> vs <meta http-equiv="Content-Type">

.HTACESS Solution :

AddDefaultCharset utf-8

htaccess UTF-8 encoding for .html, .css, .js - Whats the best way?

Save your file with UTF-8 encoding :

Community
  • 1
  • 1
A-312
  • 10,203
  • 4
  • 38
  • 66
0

Try escaping the / with \ when your JavaScript is inside <script> tags in the HTML (as opposed to an external file).

document.write(x + " et voilà! <br \/>");

But I'm not seeing the problem either way in Chrome.

As far as the Unicode character, you should encode the file as UTF-8 and add <meta charset="utf-8" /> to the head of the document.

<html><head><meta charset="utf-8" /></head>
Brett Zamir
  • 12,481
  • 5
  • 45
  • 68
-1

The html/xml comment and the javascript comment // are on the same line. And then there's the /> closing tag in the string (html parser doesn't understand or see javascript, it just sees the />) --- so a broken parser may get confused.

  • Remove //
  • Split string into document.write(x + " et voilà! br " + "/" + ">");

Anyway, your original code works on my Chrome.

hopperpl
  • 276
  • 3
  • 7