【发布时间】:2019-12-01 06:08:35
【问题描述】:
我有一个从 URL 读取值的模板标签。例如 搜索词是癌症。搜索后,出现的下一页将有一个搜索词:癌症。而且我希望值 cancer 出现在我的所有网页中,直到用户进行新的搜索。
我以这种方式工作的页面: Search.html > display.html > explore.html
Search.html 是用户输入他们想要搜索的内容的地方。
我有一个 searchedterms.html,它包含在所有 3 个模板中,并包含要从 URL 读取的 Javascript 代码。
通过使用 JavaScript 代码,我设法在 display.html 中显示搜索词:cancer ,但在 explore.html 中该值为空。我希望能够将“癌症”保存到标签中并在其他模板中使用它。
?conditions=cancer
在 search.html 中:
<input required="" type="text" name="conditions">
在 display.html 中:
{% with searched_terms='searchedterms.html' %}
{% include searched_terms %}
{% endwith %}
在 explore.html 中:
{% with searched_terms='searchedterms.html' %}
{% include searched_terms %}
{% endwith %}
在 searchedterms.html 中:
<div id="searched" class="container"></div>
<script type="text/javascript">
var urlParams = new URLSearchParams(window.location.search);
function getUrlParameter(name) {
name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
var results = regex.exec(location.search);
return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
};
var condition = getUrlParameter('conditions');
document.getElementById("searched").innerHTML = "Searched Terms: " + condition;
</script>
实际结果:Cancer 出现在 display.html 中,但没有出现在 explore.html 中。当 display.html 刷新时,“癌症”也消失了。
期望的结果:癌症出现在 display.html 和 explore.html 中,直到用户开始新的搜索。
【问题讨论】: