0

I have the following code below in which I am using to attempt to dynamically write some javascript into a new html page at the click of a button.

I am however getting an error when attempting to do so I get "unterminated string constamnt" in theory, this should work:

var html     =  '<!DOCTYPE html>\n'
html    +=  '<html>\n'
html    +=  '<head>\n'
html    +=  '<script type="text/javascript">\n'
html    +=  'function testme() {\n'
html    +=  'alert("the test worked!")\n'
html    +=  '}\n'       
html    +=  '</script>\n'
html    +=  '</head>\n'
html    +=  '<body>\n'
html    +=  '</body>\n'
html    +=  '</html\n'

window.open('','').document.write(html)

2 Answers 2

1

You are missing a closing > on the last line

html    +=  '</html\n'  <-- Missing a greater than

should be

html    +=  '</html>\n'

If the code is inline and not in an external script, you will need to break up the closing script tag.

html    +=  '</scr' + + 'ipt>\n';

also, use semicolons.

Now the window.open, document.write line looks strange. Most developers would write it as

var winPop = window.open('','');
winPop.document.open();
winPop.document.write(html);
winPop.document.close();
Sign up to request clarification or add additional context in comments.

Comments

0

Just a tip you can use multiple lines instead of putting html += on every line just add a backslash at the end of a line so:

html = '<html>\n  \
        <head>\n \
        <script type="text/javascript">\n \
            function testme() {\n \
                alert("the test worked!")\n \
            }\n \
        </script>\n \
        </head>\n \
        <body>\n \
        </body>\n \
        </html>\n  ';

However you there were some flaws in your code you forgot the > for the </html> And just like epascarello said break up the script tag.

However you might want to take a look at if you want to open a different html document with your popup, or even a php file since then you could just pickup the $_GET values and fill your custom values in with that. what would be more reliable.

Check out the w3c about $_GET

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.