简体   繁体   English

如何使用JavaScript将值从一个HTML页面传递到另一个HTML页面?

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

This is my first HTML page: 这是我的第一个HTML页面:

<!--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: 这是我的第二个HTML页面:

<!-- 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. 在这里,我想从first.html显示hidden1的内容。 Please suggest to me what code I should use for this. 请告诉我我应该使用哪些代码。

in HTML5 you can use session to pass object from page to another: 在HTML5中,您可以使用会话将对象从页面传递到另一个:

1- create a sesison 1-创建一个sesison

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

2- read session: 2-阅读会议:

sessionStorage.getItem('key')

check this example 检查这个例子

Probably the best way in your case to use GET params like: 可能是您使用GET参数的最佳方式,例如:

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

or if it's important or big data - use POST 或者如果它是重要的或大数据 - 使用POST

Found a solution for you to parse GET variables: 找到解析GET变量的解决方案:

// Read a page's GET URL variables and return them as an associative array.
function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

Get URL parameters & values with jQuery 使用jQuery获取URL参数和值

If you are not using html5, you have these ways to pass the value from one html to another - QueryString/GET/Cookies. 如果你没有使用html5,你可以通过这些方法将值从一个html传递给另一个html - QueryString / GET / Cookies。

HTML5 provides two objects localStorage and sessionStorage to save the client data. HTML5提供了两个对象localStorage和sessionStorage来保存客户端数据。 Both allow user to store the data on local machine. 两者都允许用户将数据存储在本地计算机上。 Provides two methods - getItem('Key') and setItem('Key','Value') or we can just store the data in array of localStorage or sesionStorage; 提供两种方法 - getItem('Key')和setItem('Key','Value')或者我们可以将数据存储在localStorage或sesionStorage的数组中;

// Store
localStorage.setItem("lastname", "abc");
// Retrieve
document.getElementById("result").innerHTML = localStorage.getItem("lastname");

The sessionStorage object is similar to the localStorage object except it stores the data for only one session. sessionStorage对象类似于localStorage对象,除了它仅存储一个会话的数据。 The data is deleted when user closes the window. 用户关闭窗口时删除数据。

to remove any item from session: 从会话中删除任何项目:

localStorage.removeItem("lastname");

to store as an array: 存储为数组:

for (item in items) {
   localStorage[item] = AnyArray[item];
}

暂无
暂无

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

相关问题 如何使用javascript将值从一个html页面传递到另一个html页面 - How can I pass values from one html page to another html page using javascript 如何使用 JavaScript 将数据从一个 HTML 页面传递到另一个页面 - How can i pass data from one HTML page to another using JavaScript 如何使用JavaScript将列表组之类的数据从一个HTML页面传递到另一HTML页面? - How can I pass data like list group from one html page to another using JavaScript? 如何仅使用JavaScript将表单数据从一个HTML页面传递到另一HTML页面? - How do I pass form data from one HTML page to another HTML page using ONLY javascript? 如何在JavaScript中将值从一个html页面传递到另一个页面? - How to pass value from one html page to another in JavaScript? 如何将 javascript 变量值从一个 html 页面传递到另一个 html 页面? - How to pass the javascript variable value from one html page to the another html page? 如何使用PHP和Javascript将数据从一页传递到另一页? - How can I pass data from one page to another using PHP and Javascript? 如何使用html将值传递到另一个页面 - how to pass value one page to another page using html 如何使用Phonegap中的JavaScript将数据从一个html传递到另一个html页面? - How to pass the data from one html to another html page using JavaScript in Phonegap? 如何将javascript / jquery函数从一个HTML页面传递到另一HTML页面 - How to pass a javascript/jquery function from one html page to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM