如何将 js 变量传递到另一个页面


How to pass the js variable to another page

我有2页。第一页有一个Javascript函数,该函数获取h1的值,当用户单击该按钮时,警报将显示为h1的值,并将重定向到另一个页面。第 2 页有一个 HTML 表单。

现在我想从第一页调用 js 函数并在输入文本(第 2 页)中显示值。我尝试了这个<?php echo "<script>document.writeln(h1function);</script>"; ?>,但它无法正常工作。

这是 js 脚本:

function h1function(){
    var h1 = document.getElementsByClassName("entry-title");
        for(var i = 0; i < h1.length; i++){
            alert(h1[i].innerHTML);
    }
}

第一页:

<a href="http://example.com/list/summary/" target="_blank" onclick="h1function()">

网页(第2页):

<input type="text" name="h1title" value="<?php echo "<script>document.writeln(getJob);</script>"; ?>" size="40" id="h1title" required/>

您可以使用GET发送数据

function h1function(){
    window.location.href = "http://example.com/page2?&h1="+h1[i].innerHTML;
  }

网页第二页

<input type="text" name="h1title" value="<?php echo $_GET['h1']" size="40" id="h1title" required/>
您可以使用

  1. Cookie in Javascript

  2. 此处所述的 HTML5 存储