Directing to two pages

Asked

Viewed 81 times

0

I am working on some updates and I need my Javascript function to open a new tab with a boleto and back to another after I click generate boleto. It opens the new tab, but does not go back to the page choose_estab.php, does not issue any error either. In Snippet it shows the page after 5 seconds, but here in my project it runs window.open. Follows Code:

function myFunction() {
			var valorBoleto = document.getElementById('valor').value; // joga o valor na variavel valorBoleto
			var dataBoletoString = document.getElementById('data_venc').value; // joga a data na variavel dataBoletoString
			var dataBoleto = new Date(dataBoletoString);
			dataBoleto.setDate(dataBoleto.getDate() + 1);
			var dataBoletoSegundos = dataBoleto.getTime();
			var dataHoje = new Date();
			var dataHojeSegundos = dataHoje.getTime();
			if (valorBoleto <= 0){
				alert("Valor inválido! Insira o valor do boleto.");
			}else{
				var res = window.confirm("O valor do boleto é: R$ "+ valorBoleto + ". Está correto?");
				if (res == true){
					if(dataBoletoSegundos >= dataHojeSegundos){
						var dataCorreta = confirm("O vencimento deste boleto é dia: "+ dataBoleto.toLocaleDateString("pt-BR") + " , está correto?")
						if(dataCorreta == true){
							setTimeout("document.location = 'http://www.guj.com.br/java/207585-comparando-datas-javascript'",5000);
							window.open("http://g1.globo.com/index.html", "_blank", "toolbar=yes, scrollbar=yes, resizable=yes, top=500, left=500, width=500, height=400");
						}
					}else{
						alert("O sistema não aceita datas anteriores ao dia de hoje.");
					}
				}
			}
		}
<body oncontextmenu='return false' onselectstart='return false' ondragstart='return false'>  <!-- Não deixa o usuário clicar com o botão direito na página -->
		<div class="container clearfix">
			<?php
				include "header.php";
			?>
			<div class="recuperar_usuario_senha">
				<form method="post" action="?acao=confirmar">
					<h1>Enviar boleto <em>SICLOP</em></h1>
					<h2> para <?php echo $nome_estab?></h2>
					<input type="password" name="estab" id="estab" value="<?php echo $nome_estab ?>" style="display:none"/> <!-- Nome do estabelecimento invisível para o usuário, para podermos pegar depois na ação GET -->
					<h2>Digite o valor do boleto:</h2>
					<input type="text" name="valor" id="valor"/>
					<h2> Digite a data de vencimento: </h2>
					<input type="date" name="data_venc" id="data_venc"/>
					<h2>Informações Adicionais</h2>
					<textarea cols="45" rows="8" name="textarea" id="textarea" maxlength="225"></textarea>
					</br><input onClick="myFunction()" type="image" src="img/gera_boleto.png" value="Confirmar" id="gera_boleto">
				</form>
			</div>
			<?php
				include "footer.php";
			?>
		</div>
 	</body>

1 answer

1

Your code worked here, I tested it on both Firefox and Chrome.

For awareness raising (and debug facility), I put the separate redirect function:

function redirBoleto() {
    window.setTimeout(function() { 
        document.location = 'http://www.guj.com.br/java/207585-comparando-datas-javascript';
    }, 5000);
    window.open("http://g1.globo.com/index.html", "_blank", "toolbar=yes, scrollbar=yes, resizable=yes, top=500, left=500, width=500, height=400");
}

The window opens instantly and after 05 seconds the page is redirected to the other URL.

Try using the console’s "Network" tab (Ctrl+Shift+I) and see if it records the "Location" request if it still doesn’t work.

  • Cool. Thanks for the help, but I ended up using another strategy. I called the form action and it redirects me to a page that has the information to send the boleto, ai, after it executes this sending code, it redirects again. It worked like this and was even better because it informs if the email was sent or not.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.