繁体   English   中英

如何将 index.html 文件中的变量加载到 results.html 文件中?

[英]How can I load a variable from a index.html file to a results.html file?

我正在尝试制作某种搜索引擎,我已经设置了输入,但是我需要将搜索到的内容加载到我的结果页面中,我为此使用 javascript,1 页用于输入,一个文件用于存储数据,和 1 个文件到 output 数据。

输入:

var inputBox = document.getElementById("input-box");
var searchButton = document.getElementById("search-button");
var input = "";

searchButton.onclick = function() {
    input = inputBox.value;
    input = input.toLowerCase();
    console.log("Input: " + input);
    getData(input);
}

数据存储:

var storeSearchedTearm = "";

getData = function(whatToStore) {
    console.log("Database collection: " + whatToStore);
    storeSearchedTearm = whatToStore;
    console.log("Database added to store: " + storeSearchedTearm);
}

OUTPUT:

console.log("Data: " + storeSearchedTearm);

var searchedTearm = "";

searchedTearm = storeSearchedTearm;

console.log("Output: " + searchedTearm);

在 index.html 中按下 Search 按钮后,您可以将搜索到的查询存储在 Localstorage 中,并在 results.html 中获取该值。 试试我的解决方案。

索引.html

var inputBox = document.getElementById("input-box");
var searchButton = document.getElementById("search-button");
var input = "";

searchButton.onclick = function() {
    input = inputBox.value;
    input = input.toLowerCase();
    console.log("Input: " + input);
    localStorage.setItem("searchedQuery",input );
    getData(input);
}

Results.HTML 或任何您想查看已保存数据的地方:

var SearchedQuery = localStorage.getItem("searchedQuery");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM