5

I'm not sure what is wrong with my code, but when I try and add actorWin.document.write('<script type=\"text/javascript\"><\/script>') everything gets screwed up. Without this line, the code works fine.

<!DOCTYPE html>
<meta charset="utf-8">
<title>create a window</title>

<script type='text/javascript'>
  function Movie(title, actor) {
    this.title = title;
    this.actor= actor;    
  }
</script>
</head>
<body>
<script type='text/javascript'>

  var documentary = new Movie('http://www.imdb.com/title/tt0358456/?ref_=fn_al_tt_2','http://en.wikipedia.org/wiki/Joaquin_Phoenix');
  var movieWin = new Object();
  var actorWin = new Object();  

  newWin=window.open('','Win','width=300,height=200,top=100,left=600');

   newWin.document.write(
   "<script type='text/javascript'>" +
        "function PopUpWindowMovie(url) {" +
             "movieWin=window.open(url,'','height=600,width=800,left=400,top=100,scrollbars,status,resizable');" +
             "movieWin.focus();}" + 
        "function PopUpWindowActor(){" +
             "actorWin=window.open('','','height=600,width=800,left=400,top=100,scrollbars,status,resizable');" +
             "actorWin.focus(); " +
             "actorWin.document.write('Joaquin Phoenix is a great actor and a long time vegan.<br />');" +
             "actorWin.document.write('<script type=\"text/javascript\">" +
             "function test() {" +
                    "alert(\"here\");" +
             "} <\/script>');" +
        "}" +
    "<\/script>"); 
   newWin.document.write("This is a MUST SEE movie: <h1>Earthlings (2005)</h1>");
   newWin.document.write("<a href=\"javascript:PopUpWindowMovie('"+documentary.title+"')\">Go to see the movie info</a><br />");
   newWin.document.write("<a href=\"javascript:PopUpWindowActor()\">Go to see the lead actor</a>");
</script>

</body>
</html>
1
  • Mulitple document.write statements aren't a good idea, create a single text string and do one write. Far better to put all the script in external files, then load HTML into the popups rather than writing it. Commented Oct 25, 2013 at 0:06

3 Answers 3

9

Just change closing script tag inside other script tag to fool browser.

change :

actorWin.document.write('<script type=\"text/javascript\"><\/script>')

to :

actorWin.document.write('<script type=\"text/javascript\"><\/scr'+'ipt>')

Edit :

Full code :

 newWin.document.write(
   "<script type='text/javascript'>" +
        "function PopUpWindowMovie(url) {" +
             "movieWin=window.open(url,'','height=600,width=800,left=400,top=100,scrollbars,status,resizable');" +
             "movieWin.focus();}" + 
        "function PopUpWindowActor(){" +
             "actorWin=window.open('','','height=600,width=800,left=400,top=100,scrollbars,status,resizable');" +
             "actorWin.focus(); " +
             "actorWin.document.write('Joaquin Phoenix is a great actor and a long time vegan.<br />');" +
             "actorWin.document.write('<script type=\"text/javascript\">" +
             "function test() {" +
                    "alert(\"here\");" +
             "} <\/scr'+'ipt>');" + // <-- I've edited this line
        "}" +
    "<\/script>"); 
   newWin.document.write("This is a MUST SEE movie: <h1>Earthlings (2005)</h1>");
   newWin.document.write("<a href=\"javascript:PopUpWindowMovie('"+documentary.title+"')\">Go to see the movie info</a><br />");
   newWin.document.write("<a href=\"javascript:PopUpWindowActor()\">Go to see the lead actor</a>");
Sign up to request clarification or add additional context in comments.

5 Comments

That does nothing at all, it's the </ that signifies the closing tag, so <\/ (per the OP) is the correct treatment to disguise it.
I've tested that and it works, the real problem here is using closing script tag inside another script tag.
You have a test case where '<\/script>' fails but '<\/scr' + 'ipt>' doesn't? I'd like to see that.
In my knowledge of javascript "<\/script>" has value of <\script>. That's the difference!
'<\/scr' + 'ipt>' does nothing not needed only '<\/script>' is needed
3

If your embedding javascript in an HTML page the HTML parser when it finds your first script tag will immediately try to find the closing tag. Since your closing script tag is in your document.write you'll find yourself in a pickle.

You can easily just escape the closing forward slash on the tag with a backslash:

document.write("<script>alert('foo')</script>') 

To:

document.write("<script>alert('foo')<\/script>')

Comments

1

but when I try and add actorWin.document.write('</script>') everything gets screwed up

Not sure what's the problem but it may help you

Writing to a document that has already loaded without calling document.open() will automatically perform a document.open call.

About document.open call

If a document exists in the target, this method clears it.

Read more on MDN.

Well, whatever your problem is, you may use this approach

var newWin = window.open('','Win','width=300,height=200,top=100,left=600');

// Then add scripts
var script1 = document.createElement('script');
script1.innerHTML = "function someFunc(){  alert('Hello'); }";
newWin.document.getElementsByTagName('head')[0].appendChild(script1);

var script2 = document.createElement('script');
script2.src = "http://code.jquery.com/jquery-git2.js";
newWin.document.getElementsByTagName('head')[0].appendChild(script2);

This should work. An Example here.

1 Comment

this will break a script you are adding if the added script tries to use document.write

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.