JavaScript code does not execute in window opened by script
I am trying to open a new window with some code in it and a button that should be warned when you click on it. My code looks 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 I manually copy the HTML code and save it to a file it works but it doesn't work in the generated window.
+3
source to share
1 answer
Try adding \
before /
when closing tags script
. Also, document
already loaded, $(document).ready(fn)
doesn't fire. Try replacing 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/
0
source to share