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';");
2
  • "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. Commented Oct 29, 2012 at 9: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. Commented Oct 29, 2012 at 9:42

5 Answers 5

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?.

Sign up to request clarification or add additional context in comments.

1 Comment

@jan267 sorry it was the first result that came up in google :P. Mozzila link ok? developer.mozilla.org/en-US/docs/JavaScript/Reference/…
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()?

1 Comment

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.
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>

Comments

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';");

1 Comment

Why does that make a difference?
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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.