I'm new to this, I do not understand why the pop
method does not work on a parent window, I see in the console that the elements are deleted but the page should be refreshed, should not it? I do not know how to do this.
I have several html
and js
so I can not add all, I have a general panel that opens this html
, which is a raffle, if I click on a button there may be a prize or an earthquake, which eliminates my last two buildings that are stored in an array as I add it ... I try to go to the parent window and its object, pop it but it does not go:
opener.objPartida.parque.pop()
Any suggestions? Thanks
let buttons = document.getElementsByTagName('button');
let price = Math.floor(Math.random() * buttons.length) + 1;
let earthquake = price;
let premioDinero = 10000;
if (opener === null) {
alert('Pasa primero por el panel generl')
location.assign("../index.html");
}
while(price == earthquake){
earthquake = Math.floor(Math.random() * buttons.length) + 1;
}
console.log('Price is in button ' + price);
console.log('Earthquake is in button ' + earthquake);
let premioBoton = document.getElementsByTagName('button')[price - 1];
let earthquakeBoton = document.getElementsByTagName('button')[earthquake - 1];
for (button of buttons) {
button.onclick = function(e){
if (e.target === premioBoton) {
alert('premio');
opener.contadorSaldoActual = opener.objPartida.saldo += premioDinero;
}
if (e.target === earthquakeBoton) {
alert('terremoto');
let celdas = opener.document.getElementsByClassName('celda');
let hija = opener.document;
opener.objPartida.parque.pop();
console.log(opener.objPartida.parque);
}
};
}
var objPartida = {
iniciada: false,
saldo: 3000,
recaudacion: 0,
visitantes: 0,
detalles: {},
parque: []
};
// Ejecución panel nueva partida
document.getElementById('nuevaPartida').onclick = function (){
if (!objPartida.iniciada) {
open("paneles/nuevapartida.html", 'Nueva partida', 'scrollbars=yes,width=700,height=1000,toolbar=yes');
} else {
msg('error', 'Ya has iniciado una partida previamente, no es posible crear una nueva partida');
}
}
// Ejecución panel recaudar entradas
document.getElementById('recaudarCaja').onclick = function() {
if (objPartida.iniciada) {
open("paneles/recaudarEntradas.html", 'Recaudar Caja', 'scrollbars=yes,width=500,height=400');
} else {
msg('error', 'Para acceder al panel de recaudación, inicia una partida.');
}
};
// Ejecución panel sorteo
document.getElementById('nuevoSorteo').onclick = function(){
if (objPartida.iniciada) {
if (objPartida.parque.length >= 2) {
open("paneles/nuevoSorteo.html", 'width=600, height=500, scrollbars=yes, toolbar=yes');
} else {
msg('error', 'Debe tener al menos dos edificios para participar');
}
} else {
msg('error', 'Para participar en el sorteo debe iniciar la partida');
}
};
// Ejecución panel atracciones
let celdas = document.getElementsByClassName('celda');
for (cadaCelda of celdas) {
cadaCelda.onclick = function() {
let elemento = this.dataset;
if (objPartida.iniciada) {
if (elemento.edificio === "vacia") {
let ventana = open("paneles/nuevoEdificio.html", 'Crear edificio', 'scrollbars=yes,width=500,height=800');
ventana.onload = function() {
ventana.document.getElementById('numeroCelda').textContent = elemento.celda;
}
} else {
msg('error', 'Ya hay un edificio en esta celda.');
}
} else {
msg('error', 'Para acceder al panel de construcción inicia una partida.');
}
}
}
// intervalo de actualización
let actualizador = setInterval( function(){
if (objPartida.iniciada) {
let parque = objPartida.parque;
// progresion parque
for (let edificio of parque) {
console.log(edificio);
if (edificio.tipo === "atraccion") {
objPartida.visitantes += parseInt(edificio.visitantes);
objPartida.recaudacion += parseInt(edificio.visitantes * 2);
}
if (edificio.tipo === "puesto") {
objPartida.saldo += parseInt(edificio.ingresos);
}
}
// actualizacion interfaz
document.getElementById('contadorRecaudacion').textContent = objPartida.recaudacion + "$ en caja";
document.getElementById('contadorSaldoActual').textContent = objPartida.saldo + "$";
document.getElementById('contadorEdificios').textContent = parque.length + " edificios";
document.getElementById('contadorVisitantes').textContent = objPartida.visitantes + " visitantes";
}
}, 1000);
<html lang="es">
<head>
<meta charset="UTF-8">
<title>Sorteo</title>
<link rel="stylesheet" type="text/css" href="../css/style.css">
</head>
<body class="panel sorteo">
<h1>¡Bienvenido!</h1>
<p>Realiza un sorteo cada hora y podrás ganar 10.000$... ¡o un terrible terremoto!</p>
<h2>¿A qué número apuestas?</h2>
<button>1</button>
<button>2</button>
<button>3</button>
<button>4</button>
<button>5</button>
<div id="alerta"></div>
<script src="../js/comun.js"></script>
<script src="../js/nuevoSorteo.js"></script>
</body>
</html>