tags:

views:

38

answers:

2

helloworld.js

function helloworld() {
    alert('hello world');
}

static html:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
    <title></title>
    <script src="helloworld.js" type="text/javascript"></script>
</head>
<body>
    <form action="#">
     <input type="button" onclick="helloworld()" />
    </form>
</body>
</html>

1st conversion using http://accessify.com/tools-and-wizards/developer-tools/html-javascript-convertor/ works:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
    <title></title>
    <script src="helloworld.js" type="text/javascript"></script>
</head>
<body>
    <form action="#">

     <script type="text/javascript">
         document.write("<input type=\"button\" onclick=\"helloworld()\" \/>");
     </script>

    </form>
</body>
</html>

second conversion doesn't work:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
    <title></title>
    <script src="helloworld.js" type="text/javascript"></script>
</head>
<body>
    <form action="#">

     <script type="text/javascript">
document.write("     <script type=\"text\/javascript\">");
document.write("         document.write(\"<input type=\\"button\\" onclick=\\"helloworld()\\" \\/>\");");
document.write("     <\/script>");

     </script>

    </form>
</body>
</html>

Is it possible to correct the second code so that it can work ?

+2  A: 
<script type="text/javascript">
            document.write("     <script type=\"text\/javascript\">");
            document.write("document.write('<input type=\"button\" onclick=\"helloworld()\" />');");
            document.write("     <\/script>");
</script>

Will output

EDIT

After checking fire bug, the code below is what is produced.

<script type="text/javascript">
    document.write('<input type="button" onclick="helloworld()" />');     
</script>
<input onclick="helloworld()" type="button">
Q_the_dreadlocked_ninja
That is already my first code so I want to know if I can iterate the same process anyhow (it may seem stupid but I like to push the enveloppe to see how the language behaves at extreme).
"second conversion doesn't work:... Is it possible to correct the second code so that it can work ?" - If you run my code you will see that it works. Your code had problems with nesting and and double escapes(\\").
Q_the_dreadlocked_ninja
+2  A: 

How about:

      ..
      <form action='#'>
      </form>

      <script type="text/javascript">
        window.addEventListener('load', function() {
          var inp = document.createElement('input');
          inp.setAttribute('type', 'button');
          input.addEventListener('click', function() {
            ..
          }, false);

          document.forms[0].appendChild(inp);
        }, false);
      </script>

Sure, it's longer, but it's efficient and it's the "proper" way to do it (outside of using jQuery).

    </body>
  </html>
andrewmu
Thank you very much for this very usefull source code I will study later on. I'm not saying that document.write is the way to go, I just want to experiment by myself all the ways I could do and do something stupid somehow but as I said below I like to test extreme cases to see how languages will cope with.