使用两个不同的 js 从一个 HTML 页面发送到另一个页面

Send from one HTML page to another with two different js

我正在主页上使用 HTML 和 JS 将变量发送到另一个 js 页面。

我做到了 在页面索引中,我使用 index.js

创建按钮
<a href="index.html" id= "button1">Easy</a>

在索引 .js 中我这样做了

window.addEventListener('load', function(){
document.getElementById("button1").onclick = function(){
    sessionStorage.setItem("mood", '1');
    window.location.replace("second.html");
};

在 second.js 我在 js 中做了这个来获取情绪变量。

data = sessionStorage.getItem('mood');

在 Firefox 上一切正常(在本地),但是当使用 Chrome o 在线上传时按钮不起作用,因为它不会重定向到 second.html

谁能帮帮我? p.s 我不想使用脚本(Ajax、Jquery 等..)

这不是常见的做法,但您可以使用 browserify https://www.npmjs.com/package/browserify 或要求 https://www.npmjs.com/package/requirejs 打造你的 "Nodish" 风格。

编辑: 如果您直接引用其他文件,我认为它会按照您的方式工作。

window.addEventListener('load', function(){
document.getElementById("button1").onclick = function(){
sessionStorage.setItem("mood", '1');
 window.location = "http://yourURL.yourURL.com/ourPath/second.html"; 
};

如我的评论所述,将 href 设置为“#”,或将其从 a 标签切换为按钮标签将防止直接出现双页,而是 运行 onclick 函数,设置你的存储然后从 index.html.

重定向到 second.html

示例:

<a href="#" id="button1">Easy</a>

<button id="button1" onclick="loadB1()" >Easy</button>

并以这种方式调用 index.js 函数:

function loadB1(){
sessionStorage.setItem("mood", '1');
window.location.replace("second.html");
};