0

For example, I have this script

<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
var myWindow = window.open("", "myWindow", "width=200,height=100");
myWindow.document.write("<p>This is 'myWindow'</p>");
myWindow.opener.document.write("<p>This is the source window!</p>");
}
</script>

Instead of

 myWindow.document.write("<p>This is 'myWindow'</p>");

I would want to put something like

 myWindow.document.write("<script> alert("h");</script>");

Due to circumstances, I cannot link to another page with the code in it. Putting the script makes the window not even open. Thanks

asked Nov 8, 2018 at 15:41
6
  • Just do "<script> alert('h');</script>"? Your problem is that you need to escape h or use a different set of quotation marks. Commented Nov 8, 2018 at 15:43
  • Possible duplicate of How to put "</script>" in a javascript string? Commented Nov 8, 2018 at 15:44
  • The problem is neither work. Commented Nov 8, 2018 at 15:48
  • 1
    This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. Commented Nov 8, 2018 at 15:50
  • You need myWindow.opener.document.close() at the end AND escape the / <\/script> Commented Nov 8, 2018 at 15:50

2 Answers 2

1
  1. quotes
  2. close the document (myWindow.document.close())
  3. escape the </script> you cannot have </script> inside script tags.
  4. NEVER document.write into an already rendered page

Something like this

function myFunction() {
 var myWindow = window.open("", "myWindow", "width=200,height=100");
 myWindow.document.write("<p>This is 'myWindow'</p>");
 myWindow.document.close();
 var p = document.createElement("p")
 p.innerHTML = "This is the source window!";
 myWindow.opener.document.querySelector("body").appendChild(p); // same as document.querySelector("body") 
}

If you want to use script, you need to

var myWindow = window.open("", "myWindow", "width=200,height=100");
myWindow.document.write("<script>alert('h')<\/script>"); // MANDATORY ESCAPE \/SCRIPT
myWindow.document.close();
answered Nov 8, 2018 at 15:54
Sign up to request clarification or add additional context in comments.

6 Comments

Sorry but this doesn't really answer my question. The code works fine with text but once i put a script into it, it breaks. How would you put a script in the opened window?
Your use case is unclear. What do you need to have in the child window and what in the parent window?
Look at my question. I want to put a script in the popup window.
Look at my answer. You need to escape the end /script tag
OK I see it now. If i put a full webpage in there do I have to end all tags like that? Right now instead of the button it just says [Object Object]
|
0

Instead of double quotes wrap your content into single quote

myWindow.document.write('<script> alert("h");</script>');
answered Nov 8, 2018 at 15:45

6 Comments

Doesn't work, just writes it in plaintext next to the button.
Try to add this method reload script Working example - (function (){ var w = window.open(); w.document.write('<script> alert("h");</script>'); })();
Sorry, I don't really understand your code. What does it mean/do/ how do I use it?
You cannot have </script> inside script tags.
@mplungjan Where did you find that in myWindow.document.write('<script> alert("h");</script>');
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.