javascript - 如何创建将数据从一个本地站点传输到另一个站点的 JS
问题描述
情况是这样的:您在网站 A 的一个字段中输入文本并按下按钮,在网站 B 中按下该按钮会显示输入的文本。我曾尝试使用 localStorage,但没有运气。有什么帮助吗?
解决方案
试试这个代码:在 testing.html
function testJS() {
var b = document.getElementById('name').value,
url = 'http://path_to_your_html_files/next.html?name=' + encodeURIComponent(b);
document.location.href = url;
}
在 next.html 中:
window.onload = function () {
var url = document.location.href,
params = url.split('?')[1].split('&'),
data = {}, tmp;
for (var i = 0, l = params.length; i < l; i++) {
tmp = params[i].split('=');
data[tmp[0]] = tmp[1];
}
document.getElementById('here').innerHTML = data.name;
}
说明:javascript不能在不同页面之间共享数据,我们必须使用一些解决方案,例如URL get params(在我的代码中我使用这种方式),cookies,localStorage等。将name参数存储在URL中(?name = ...) 并在 next.html 中解析 URL 并从上一页获取所有参数。
PS。我是非英语母语人士,如有必要,请您更正我的信息
推荐阅读
- r - 如何绘制多个分组条形图?
- python - XGBoost 分类/概率预测的折叠/聚合数据
- javascript - EXTJS AJAX调用需要同步
- node.js - e.preventDefault() 不是 NextJS/React 函数
- javascript - 获取选项值并在按钮中使用它们来加载特定的 url
- selenium - 如何在硒中使用爬虫代理
- ios - SwiftUI 列表和延迟加载
- python - 未找到带有参数 '('',)' 的 'detail_account' 的反向操作。尝试了 1 种模式:['account/account/(?P
[^/]+)$'] - jquery - 为什么 CORS 策略阻止我从同一文件夹加载 HTML 片段?
- quarkus - 从 quarkus graphQL 访问 RestClient 会出现 RESTEASY004655:无法调用请求:javax.net.ssl.SSLHandshakeException 错误