Configuré una base de datos en tiempo real de Firebase, y cuando ejecuto mi código, funciona en Safari pero no funciona en Chrome.
Este es mi enlace de archivo index.js a un archivo html como <script type="module" src="index.js"></script>
import { initializeApp } from 'https://www.gstatic.com/firebasejs/9.1.2/firebase-app.js'; import { getDatabase, set, ref } from 'https://www.gstatic.com/firebasejs/9.1.2/firebase-database.js'; const firebaseApp = initializeApp({ apiKey: "xxx", authDomain: "xxx", databaseURL: "xxx", projectId: "xxx", storageBucket: "xxx", messagingSenderId: "xxx", appId: "xxx", }); const db = getDatabase(firebaseApp) var x = document.getElementById("Identifiant"); var y = document.getElementById("Password"); function senddata(){ set(ref(db, 'users/' + x.value), { username: x.value, password: y.value }); window.location.href = 'https://www.youtube.com'; }; var btn = document.getElementById('btnconx'); btn.addEventListener('click', senddata);Entonces esto funciona en Safari, pero en Chrome, me envía a YouTube pero no escribe en la base de datos. Pero si no coloco " window.location.href = 'https://www.youtube.com'; ", enviará los datos a la base de datos incluso en Chrome. ¿Cómo hago que funcione para que después de escribir los datos en la base de datos, envíe al usuario a YouTube (o básicamente a cualquier sitio web)? Tenga en cuenta que el botón "btn" como type="button" y está dentro de un formulario con el método="GET".
Parece que puede estar redirigiendo su página antes de que tenga la oportunidad de enviar sus datos. Para remediar esto, sugiero hacer que senddata() sea asincrónico y esperar a set(). Esto debería permitir que su página envíe los datos antes de redirigir.
ES6
async function senddata(){ await set(ref(db, 'users/' + x.value), { username: x.value, password: y.value }); window.location.href = 'https://www.youtube.com'; };ES5
function senddata(){ set(ref(db, 'users/' + x.value), { username: x.value, password: y.value }).then(() => { window.location.href = 'https://www.youtube.com'; }); };