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
2 Answers 2
- quotes
- close the document (
myWindow.document.close()) - escape the
</script>you cannot have</script>inside script tags. - 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
mplungjan
180k29 gold badges183 silver badges246 bronze badges
Sign up to request clarification or add additional context in comments.
6 Comments
user3125845
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?
mplungjan
Your use case is unclear. What do you need to have in the child window and what in the parent window?
user3125845
Look at my question. I want to put a script in the popup window.
mplungjan
Look at my answer. You need to escape the end /script tag
user3125845
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]
|
Instead of double quotes wrap your content into single quote
myWindow.document.write('<script> alert("h");</script>');
answered Nov 8, 2018 at 15:45
front_end_dev
2,0661 gold badge13 silver badges15 bronze badges
6 Comments
user3125845
Doesn't work, just writes it in plaintext next to the button.
front_end_dev
Try to add this method reload script Working example -
(function (){ var w = window.open(); w.document.write('<script> alert("h");</script>'); })();user3125845
Sorry, I don't really understand your code. What does it mean/do/ how do I use it?
mplungjan
You cannot have
</script> inside script tags.front_end_dev
@mplungjan Where did you find that in
myWindow.document.write('<script> alert("h");</script>'); |
default
"<script> alert('h');</script>"? Your problem is that you need to escape h or use a different set of quotation marks.myWindow.opener.document.close()at the end AND escape the /<\/script>