How do I click a button after the window.open() event?

Asked

Viewed 25 times

0

I have a code where after an event onClick() is triggered opens an event window.open() where some data is sent via url to the new page, but the data is only typed in the fields, I need that after the data is entered in the field the send button is triggered too, I will send the code to exemplify. (on the other site there is no one id on the send button, so I’m using the class)

let site
let valor1 = document.getElementById('v1')
let valor2 = document.getElementById('v2')

function abrirPopUp() {
  site = window.open(`https://exemplo.com/send? 
valor1=${valor1.value}&valor2=${valor2.value}`)
  document.getElementByClassName("teste").click();
}

function fecharPopUp() {
  site.close()
}

function enviarMensagem() {
  abrirPopUp()
  setTimeout(fecharPopUp, 13000)
}
<!DOCTYPE html>
<html>
<head>
  <title>Teste automação</title>
</head>
<body>
  <span>Valor 1: </span><input type="text" id="v1"><br><br>
  <span>valor 2: </span><textarea id="v2"></textarea><br><br>
  <button onclick="enviarMensagem()">Enviar</button>
  <script type="text/javascript" src="funcoes.js"></script>
</body>

  • Are you trying to send from one domain to another, or to your own domain? because there is a cross-browser lock that would conflict your action when opening a new page!

  • To another domain, but as such lock?

  • Modern browsers have some restrictions on not allowing certain actions to happen. This is one of them, it doesn’t work if the domain of the new window is different from the parent.

1 answer

0

You may be using the event load for this!

Just put under the code:

site = window.open(`https://exemplo.com/send?valor1=${valor1.value}&valor2=${valor2.value}`);

A addEventListener of the element site as follows:

site = window.open(`https://exemplo.com/send?valor1=${v1.value}&valor2=${v2.value}`);
// ao ser carregado o método window.open()
site.addEventListener('load', function() {
  // é invocado um clique no botão!
  document.querySelector('button').click();
});

But if it’s not the same domain as the father, it doesn’t work!

Because modern browsers have new rules and restrictions on not allowing certain actions to happen! You may be trying this way:

let site;
let v1 = document.getElementById('v1');
let v2 = document.getElementById('v2');

function abrirPopup(url) {
  site = window.open('');
  if (site.location.href === 'about:blank') 
    site.location.href = url;
  
  // ações aqui..
  console.log('fui executado antes de abrir!');
  
  // é feita as devidas alterações (neste caso o endereço)
  return site;
}

function enviarMensagem() {
  // executa a função definindo o endereço pelos parâmetros..
  abrirPopup(`http://exemplo.com/send?valor1=${v1.value}&valor2=${v2.value}`);
  // por tratar-se de uma simples ação, basta incluir na própria função gerada pelo setTimeout..
  setTimeout(function() { site.close() }, 13000)
}
<span>Valor 1:</span>
<input type="text" id="v1" /><br/><br/>
<span>Valor 2:</span>
<input type="text" id="v2" /><br/><br/>
<button onclick="enviarMensagem()">Enviar</button>

May be testing the code here

Browser other questions tagged

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