繁体   English   中英

javascript用变量打开新的html

[英]javascript open new html with variable

我想按此按钮转到“ stats.html”,我想在第一个站点的“ stats.html”代码上写var名​​称:

<html>
    <head>
    </head>
    <body>
        <script type="text/javascript">  
            function getcube(){  
                var number=document.getElementById("field").value;  
            }  
            function window() {
                var number=document.getElementById("field").value;
            }
        </script>
        <form action="stats.html">
            <input id="field" type="text" name="name" placeholder="Player Name...">
            <input id="button" type="submit" value="SEARCH" onclick="window()">
        </form>
    </body>
</html>

我不知道我应该在“ stats.html”中添加什么。

感谢帮助。

一种方法是使用查询参数。

// index.html
<script type="text/javascript">  
  function redirect() {
    const name = document.getElementById("name").value;
    location.href = `stats.html?name=${name}`;
  }
</script>

<input id="name" type="text" name="name" placeholder="Player Name...">
<input id="button" value="Go!" onclick="redirect()">

// stats.html
<script type="text/javascript"> 
  const urlParams = new URLSearchParams(window.location.search);
  const name = urlParams.get('name');
  document.getElementById("name").value = name;
</script>

<input id="name" type="text" name="name" placeholder="Player Name...">

详细信息: 如何获取JavaScript中的查询字符串值

要重定向到state.html,可以使用window.location.replace(url) 例如:

window.location.replace(http://example.com/state.html)

如果要模拟某人单击链接,请使用location.href

如果要模拟HTTP重定向,请使用location.replace

有一些不同的方法可以将变量的值传递到下一页。 在此之前,您应该知道HTML / HTTP是无状态的。 这意味着,您在前一页上所做的/所看到的与当前页面上所执行的/所看到的是断开的。

这些附加信息也可能会有所帮助:

如何将javascript对象从一页传递到另一页

有关“客户端存储解决方案”的其他信息

localStorage,sessionStorage,session和cookie之间有什么区别?

祝好运!

暂无
暂无

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

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