0

I searched for this, but I keep finding issues regarding

"<scr" + "ipt>"

My issue is that I want to generate some JavaScript code from my JavaScript code.

I have copied a general idea below. How can what I want be achieved? Is it necessary to use an external .js file?

document.write("var testprompt = prompt('What zindex page to change?');
var getpage = getElementById('mobileimage'+testprompt);
getpage.style.zIndex = '1000';");
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Damien Golding
  • 1,003
  • 4
  • 15
  • 28
  • *"My issue is that I want to generate some JavaScript from my JavaScript code."* — You should never need to do this, and the example you give certainly doesn't need you to do so. – Quentin Oct 29 '12 at 09:09
  • @Quentin It's useful if you are using `document.write` to generate a completely new page (document.open/write/close). New pages do not preserve the Javascript from the old page, so you need to write new ones. – Kernel James Oct 29 '12 at 09:42

5 Answers5

3

You are looking for eval(). I would, however, advise against using it as there are many security problems with running JavaScript code using eval. See Why is using the JavaScript eval function a bad idea?.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Zappa
  • 453
  • 1
  • 5
  • 14
  • @jan267 sorry it was the first result that came up in google :P. Mozzila link ok? https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/eval – Zappa Oct 29 '12 at 08:59
1

The problem with using JavaScript to write JavaScript is that the browser will only execute the first pass over the code.

The JavaScript written with document.write() will not be executed, thus rendering it quite useless. Why can't you simply write the JavaScript you want to execute as JavaScript instead of abstracting it through document.write()?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
L0j1k
  • 12,255
  • 7
  • 53
  • 65
  • Hi, I am using JavaScript to generate a HTML page with JavaScipt inside it. The HTML page is then placed somewhere and when the user executes the generated JavaScript. – Damien Golding Oct 30 '12 at 03:30
1

This works for me:

<html>

<body>
    <script>
        var i = 1;
        document.write(i);
        document.write("<scr" + "ipt> i = 2; document.write(i); </scr" + "ipt>");
        i = 3;
        document.write(i);
    </script>
    123
</body>

</html>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kernel James
  • 3,752
  • 25
  • 32
0

getElementById() works only after the DOM is loaded, while the code written via document.write() is executed immediately as the HTML document is parsed.

You have to encapsulate your code in a callback to be executed after the DOM is loaded (I suggest to use a library like jQuery).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Raffaele
  • 20,627
  • 6
  • 47
  • 86
0

Run it in a single line without '\n' between the string. It works.

document.write("var testprompt = prompt('What zindex page to change?'); var getpage = getElementById('mobileimage' + testprompt); getpage.style.zIndex = '1000';");
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Satish Bellapu
  • 740
  • 1
  • 5
  • 15