如何将数据从javascript函数传递到重定向页面?

时间:2020-04-08 19:20:17

标签: javascript html google-cloud-firestore

const db = firebase.firestore();
function getDataOfBus() {
var dates = document.getElementById('departure-date').value.toString();
var fromd = document.getElementById('from-dest').value.toString()
var tod = document.getElementById('to-dest').value.toString()
console.log(dates + " -" + fromd + " -" + tod)

db.collection('Buses').where("DepartureDate", "==", dates).where("To", "==", tod).
where("From", "==", fromd).get().then((snapshot) => {
    let html = '';
    snapshot.forEach((busDatas) => {
        busData = busDatas.data()
          // console.log(busData.id)
          busData.docId = busDatas.id
        html += `
<div class="single-room-area d-flex align-items-center 
mb-50 wow fadeInUp" data-wow-delay="100ms" id="prince">

<div class="room-thumbnail">
  <img src="${busData.ImageLink}" alt="">
 </div>

<div class="room-content">

<h2><a href="javascript:getID('${busData.docId}');">${busData.TourName}</h2>
  <h6>${busData.From} to ${busData.To}</h6>
  <h4>₹ ${busData.SeatPrice} </h4>

  <div class="room-feature">
    <h6>Boarding Point  <span>${busData.BoardingTime}</span></h6>
    <h6>Dropping Point <span>${busData.DroppingTime}</span></h6>
    <h6>Seats Left <span>${busData.SeatsLeft}</span></h6>
    <h6>Total Time <span>${busData.TotalTime}</span></h6>
    <h6>Departure Date <span>${busData.DepartureDate}</span></h6>
  </div>


  </div>

</div>  `

        document.getElementById('bus-container-dynamic').innerHTML = html;

    })    // End foreach
})      // End then

}


function getID(id) {
  db.collection('Buses').doc(id).get().then((doc) => {
let html = '';
var data =  doc.data();
html += `<h1>${data.TourName} </h1>`
document.getElementById('div-1').innerHTML = html;
window.location = "single-bus.html"
}).catch((e) => {
console.log(e);
 })

}

我已经执行了此代码,以在html页面中显示过滤的文档,在该页面上,我获得了用户单击的文档的ID,但我无法执行此操作,我想将其重定向到“单总线”。单击“ html”,我要将其ID发送到下一个页面“ single-bus.html”,在此我要使用该ID在使用该ID的html文件中显示数据。

<!-- single-bus.html-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Homepage</title>
</head>
<body>
<div class="classer" id="div-1">

</div>
<script src="./loader.js"></script>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

对不起,我的英语。

据我了解,您想使用window.location将文档ID从A页发送到B页。

好吧,下一个示例是我找到的解决方案。

页面A看起来像这样

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>

</body>

<script>
    // redirect user to some site with a parameter called 'documentParam' which value = 123
    window.location = 'some-site.html?documentParam=123'
</script>
</html>

请注意,参数使用问号?放置在URL中。如果您需要更多参数,可以将它们分开使用&在此处了解有关查询参数的更多信息:What is the difference between URL parameters and query strings?

页面B看起来像这样

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>

</body>

<script>

    // this page expects one parameter called 'documentParam'
    var urlParams = new URLSearchParams(window.location.search);

    console.log(urlParams.has('post')); // false --- no parameter called 'post'
    console.log(urlParams.get('documentParam')); // "123"

    alert(urlParams.get('documentParam'));
</script>
</html>

此解决方案使用查询参数并使用JavaScript从URL读取它们。我认为您可以根据您的需求调整此想法。

您可以在此处了解更多信息:How can I get query string values in JavaScript?