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

时间:2016-02-11 02:58:12

标签: javascript php html

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

现在我想从第1页调用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);
    }
}

第1页:

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

HTML(第2页):

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

2 个答案:

答案 0 :(得分:0)

您可以使用:

  1. Cookies in Javascript

  2. HTML5存储,如here

  3. 所述

答案 1 :(得分:0)

您可以使用GET

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

HTML第2页

<input type="text" name="h1title" value="<?php echo $_GET['h1']" size="40" id="h1title" required/>
相关问题