How to pass values from one HTML page to other using javascript?

后端 未结 2 1847
既然无缘
既然无缘 2021-01-16 06:47

I want to pass my value of one variable to another html page using query string.

first.html:


   
相关标签:
2条回答
  • 2021-01-16 07:14

    EDIT: I've edited slightly and this seems to work for me, what exactly is the problem?

    Please explain what "required result" is..

    Page 1

       <form id="form1" name="form1" method="get" action="2.html">
        <input type="text" name="txtFileName" id="txtFileName"/>
    
       <input type="hidden" name="hid1" value="">
       <br><input type="submit"  value="Send me your name!"  onClick="submitform();">
       <br>
       </form>
       <script type="text/javascript">
       function submitform()
       {
         document.form1.hid1.value="hidden value";
    
        document.form1.submit();
       }
    </script>
    

    Page 2

    <SCRIPT LANGUAGE="javascript">
    function getQueryVariable2(variable) { 
      var query = window.location.search.substring(1); 
      document.write(query);
      var vars = query.split("&"); 
      document.write("<br />");
      document.write(vars);
    
      for (var i=0;i<vars.length;i++) { 
        var pair = vars[i].split("="); 
        if (pair[0] == variable) { 
          return pair[1]; 
        }
      } 
    } 
    
    
    document.write("<br />txtFileName = " + getQueryVariable2("txtFileName"));
    document.write("<br />hid1 = " + getQueryVariable2("hid1"));
    </SCRIPT>
    

    Have a look at this link http://www.htmlgoodies.com/beyond/javascript/article.php/3471111/A-Quick-Tutorial-on-JavaScript-Variable-Passing.htm

    0 讨论(0)
  • 2021-01-16 07:25

    Add method="get" to the form element. Otherwise, POST (the default) might be used which passes parameters in a different way.

    See: FORM method attribute

    0 讨论(0)
提交回复
热议问题