views:

71

answers:

4

I am opening a blank window and writing a javascript on the new window to submit a form.

when I execute a line "newWindow.document.write(newwdtxt2);\n\"(3rd line from last) I get an exception and last two lines do not execute. Below mention is my code

function openWindow(url,name,options) {
       var aToken = ""; 
       aToken ="2121225434349231132674638921:SUPER.SUPER"; 
       if(aToken=="") { 
       aToken=document.formEMS.AUTHTOKEN.value; }
       var newWindow = window.open("", name); 
       if (!newWindow) return false; 
       var newwdtxt = ""; 
       newwdtxt += "<html><head></head>\n"; 
       newwdtxt += "<body>\n"; 
       newwdtxt += "<form name=\"eventForm\" method=\"post\" action="+url+ ">\n"; 
       newwdtxt += "<input type=\"hidden\" name=\"AUTHTOKEN\"";
       newwdtxt += "value= '";newwdtxt += aToken+"'/>\n"; 
       newwdtxt += "</form>\n"; 
       newwdtxt += "<scr"; 
       var newwdtxt1 = ""; 
       newwdtxt1 += "ipt type=\"text/javascript\" language=\"javascript\">\n"; 
       newwdtxt1 += "window.onLoad=document.eventForm.submit();\n"; 
       newwdtxt1 += "</scr"; 
       var newwdtxt2 = ""; 
       newwdtxt2 += "ipt>\n"; 
       newwdtxt2 += "</body></html>\n"; 
       newWindow.document.write(newwdtxt);
       alert(newwdtxt); 
       newWindow.document.write(newwdtxt1);
       alert(newwdtxt1); 
       alert(newwdtxt2); 
       newWindow.document.write(newwdtxt2);
       alert('wrote newwdtxt2'); 
       return newWindow; }

Please help me to figure out what is the problem?

+2  A: 

You should look into this: http://getfirebug.com/

somacore
A: 

I believe when you do a document.write the browser parses the html you wrote into DOM nodes. In your code you're writing incomplete HTML so when it gets parsed it is giving an error. Try putting the "ipt>" onto the end of 'newwdtxt1' instead of where it is now.

kbosak
And as somacore pointed out, Firebug will help you 'see' javascript errors better. Also check your javascript console in your browser and there may be errors there as well.
kbosak
+1  A: 
  1. Javascript supports multiline strings:
    var doc = '<html>\
    <head>\
    </head>\
    <body>\
    </body>\
    </html>';
  1. Write full tags (it was a root of your problem): document.write('</scr' + 'ipt>'); works fine
  2. Use firebug console for easier javascript debugging
Anatoliy
I didn't know you could write multi-line strings in JS!
Gabe Moothart
+1  A: 

That will make a syntax exception. Try this instead:

   newWindow.document.write(newwdtxt+newwdtxt1+newwdtxt2);
Rodrigo