简体   繁体   中英

JavaScript code does not execute in a window opened by a script

I'm trying to open a new window, with some code in it and a button that should alert when you click on it. My code is something like this:

 $('#PREVIEW').click(function() { n = window.open("preview.html", "_blank"); var result = DSLExGenerator.parse($('#original').val()); var res = result.replace(/>,</g, "><"); n.document.write(res + '<br/><button id=\\"results\\">Resultados</button><script type=\\"text/JavaScript\\" src=\\"https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js\\"></script><script type=\\"text/JavaScript\\">$(document).ready(function() { $(\\"#results\\").click(function() { var aux = 0; var nPreg = 0; $(\\'input[value=\\"1\\"]\\').each(function() { nPreg++; }); $(\\'input:checked\\').each(function() { if($(this).attr(\\"ver\\") == \\"V\\"){ aux++; } }); alert(\\"Ha acertado \\" + aux.toString() + \\" de \\" + nPreg + \\" preguntas.\\"); });});</script>'); }); 

 <html><head></head><body><h1>Titulo: Titulo Fecha: 01,01,2015 Profesor: Profesor Asignatura: Asignatura</h1><p class="question">1. Pregunta 1</p><ul class="answers"><input type="radio" name="q0" value="1" id="q01" ver="F"><label for="q01">Respuesta 1 1</label><br><input type="radio" name="q0" value="2" id="q02" ver="V"><label for="q02">Respuesta 1 2</label><br></ul><p class="question">2. Pregunta 2</p><ul class="answers"><input type="radio" name="q1" value="1" id="q11" ver="V"><label for="q11">Respuesta 1 2</label><br><input type="radio" name="q1" value="2" id="q12" ver="V"><label for="q12">Respuesta 2 2</label><br></ul><p class="question">3. Pregunta 3</p><ul class="answers"><input type="radio" name="q2" value="1" id="q21" ver="V"><label for="q21">Respuesta 1 3</label><br><input type="radio" name="q2" value="2" id="q22" ver="V"><label for="q22">Respuesta 3 2</label><br><input type="radio" name="q2" value="3" id="q23" ver="F"><label for="q23">Respuesta 3 3</label><br></ul><p class="question">4. Pregunta 4</p><ul class="answers"><input type="radio" name="q3" value="1" id="q31" ver="V"><label for="q31">Respuesta 1 4</label><br><input type="radio" name="q3" value="2" id="q32" ver="V"><label for="q32">Respuesta 4 2</label><br></ul><br><button id="results">Resultados</button><script type="text/JavaScript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script><script type="text/JavaScript">$(document).ready(function() { $("#results").click(function() { var aux = 0; var nPreg = 0; $('input[value="1"]').each(function() { nPreg++; }); $('input:checked').each(function() { if($(this).attr("ver") == "V"){ aux++; } }); alert("Ha acertado " + aux.toString() + " de " + nPreg + " preguntas."); });});</script></body></html> 

The problem is if I manually copy the HTML code and save it into a file it works, but it doesn't work on the generated window.

Try adding \\ before / at closing script tags. Also, document is already loaded , $(document).ready(fn) does not fire. Try substituting an IIFE for $(document).ready(fn)

(function() {
  $("#results").click(/* do stuff */)
}());

    n = window.open("", "_blank");
   // var result = DSLExGenerator.parse($('#original').val());
   // var res = result.replace(/>,</g, "><");
    n.document.write('<br/><button id=\"results\">Resultados</button><script type=\"text/javascript\" src=\"https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js\"><\/script><script type=\"text/javascript\">(function() {        $(\"#results\").click(function() {alert(\"preview\");});}());<\/script>');

jsfiddle http://jsfiddle.net/ac7ax95L/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM