Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I pass a value from one HTML page to another using JavaScript?

This is my first HTML page:

<!--first.html-->   
<html>
    <body>
    <div data-role="page" data-theme="a" data-url="first" id="first"> 
        <form id="form1" name="form2" action="checking.html">
        <input type="text" name="txtFileName" id="txtFileName"/>
       <!-- <button onClick="uploadFile();">Upload</button> -->
       <input type="hidden" name="hidden1" value="">
       <br><input type="submit"  value="Send me your name!"  onClick="submitform();"><br>
       </form>
       <script type="text/javascript">
       function submitform()
       {
         document.forms.form1.hidden1.value=1;
         alert("i am working");

        document.form1.submit();
       }
    </script>

        </div>
    </body>
</html>

This is my second HTML page:

<!-- second.html -->
<html>
    <head>
    </head>
    <body> 
<h1>Javascript call after page loaded</h1>

<script>
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>
hellllo
</body>

Here I want to display the contents of hidden1 from first.html. Please suggest to me what code I should use for this.

like image 495
Pushpendra Kuntal Avatar asked Sep 01 '11 08:09

Pushpendra Kuntal


People also ask

How do you pass input value from one HTML page to another?

For sending data to two servelets make one button as a submit and the other as button. On first button send action in your form tag as normal, but on the other button call a JavaScript function here you have to submit a form with same field but to different servelets then write another form tag after first close.


2 Answers

in HTML5 you can use session to pass object from page to another:

1- create a sesison

sessionStorage.setItem('key', 'value');

2- read session:

sessionStorage.getItem('key')

check this example

like image 123
Alex Avatar answered Oct 28 '22 01:10

Alex


Probably the best way in your case to use GET params like:

http://mysite//second.html?myparams=value

or if it's important or big data - use POST

like image 29
Samich Avatar answered Oct 28 '22 03:10

Samich