简体   繁体   English

如何使用javascript将值从一个html页面传递到另一个html页面

[英]How can I pass values from one html page to another html page using javascript

In first page im getting value in textbox i need to pass it to another page which is divided into 2 frames. 在第一页中我在文本框中获取值我需要将其传递给另一个分为2帧的页面。 I need to display that value in first frame's html page. 我需要在第一帧的html页面中显示该值。 Please provide me a simple example. 请给我一个简单的例子。 I tried with window.document.getElementById("inputbox1").value but im unable to get the value. 我尝试使用window.document.getElementById(“inputbox1”)。value但我无法获取值。

Please provide me a simple example. 请给我一个简单的例子。

I would go with localStorage, as @MicrosoftGoogle propose, but is not well supported yet, you can use pure javascript to achieve this. 我会选择localStorage,就像@MicrosoftGoogle提议的那样,但是还没有得到很好的支持,你可以使用纯javascript来实现这一点。 You will have something like this on your form page: 您将在表单页面上看到类似的内容:

<form action="param-received.html" method="GET">
   <input type="text" id="foo" name="foo">
   <input type="submit" value="Send" name="submit" id="submit">
</form>

Once you click on Send button,you will be redirect to /param-received.html?foo=hola&submit=Send . 点击“发送”按钮后,您将重定向到/param-received.html?foo=hola&submit=Send

  • location.search attribute contains the chain of parameters. location.search属性包含参数链。
  • ? concatenates the URL and the string of parameters. 连接URL和参数字符串。
  • & separates multiple parameters. &分隔多个参数。
  • = assigns a value to the variable. =为变量赋值。

Here is the complete code to process data sent on param-received.html : 以下是处理param-received.html上发送的数据的完整代码:

<script language="JavaScript">
  function processForm()
  {
    var parameters = location.search.substring(1).split("&");
    var temp = parameters[0].split("=");
    l = unescape(temp[1]);
    alert(l); //Dialog with the text you put on the textbox
  }
  processForm();
</script>

Write the value in a cookie and read the cookie from the other page. 将值写入cookie并从其他页面读取cookie。

For writing and reading cookies check here 如需写作和阅读cookies,请点击此处

if url parameters are an option you could use this 如果url参数是一个选项,你可以使用它

function getParameter(param) {
                var val = document.URL;
                var url = val.substr(val.indexOf(param))  
                var n=parseInt(url.replace(param+"=",""));
                alert(n+1); 
}
getParameter("page");

ref http://bloggerplugnplay.blogspot.in/2012/08/how-to-get-url-parameter-in-javascript.html 参考http://bloggerplugnplay.blogspot.in/2012/08/how-to-get-url-parameter-in-javascript.html

another might be cookies 另一个可能是饼干


was beaten to the cookie part :p 被殴打到饼干部分:p

edit indeed not a good cookie reference this one is better http://www.w3schools.com/js/js_cookies.asp 编辑确实不是一个好的cookie参考这个更好http://www.w3schools.com/js/js_cookies.asp

您可以使用请求的GET部分cookie

function getValue(varname)
                {
                    var url = window.location.href;
                    var qparts = url.split("?");

                    if (qparts.length == 1)
                    {
                        return "";
                    }
                    else{
                        var query = qparts[1];
                        var vars = query.split("&");
                        var value = "";
                        for (i=0;i<vars.length;i++)
                        {
                            var parts = vars[i].split("=");
                            if (parts[0] == varname)
                            {
                                value = parts[1];
                                break;
                            }
                        }
                        value = unescape(value);

                        // Convert "+"s to " "s
                        value.replace(/\+/g," ");
                        return value;
                    }

                }
var VariableGot = getValue(YourPassingVariableName);

Just copy the function into your html file and pass your variable name to the function which is send through GET Method.Now You will get the value of the variable from url. 只需将函数复制到您的html文件中,并将您的变量名称传递给通过GET Method.Now发送的函数。您将从url获取变量的值。

暂无
暂无

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

相关问题 如何仅使用JavaScript将表单数据从一个HTML页面传递到另一HTML页面? - How do I pass form data from one HTML page to another HTML page using ONLY javascript? 如何使用JavaScript将值从一个HTML页面传递到另一个HTML页面? - How can I pass a value from one HTML page to another 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? 如何使用HTML表单将JavaScript值传递到另一个PHP页面? - How can I pass JavaScript values to another PHP page using an HTML form? 如何使用Phonegap中的JavaScript将数据从一个html传递到另一个html页面? - How to pass the data from one html to another html page using JavaScript in Phonegap? 如何在JavaScript中将值从一个html页面传递到另一个页面? - How to pass value from one html page to another in JavaScript? 如何将javascript / jquery函数从一个HTML页面传递到另一HTML页面 - How to pass a javascript/jquery function from one html page to another 如何将值从一个html页面传递到另一个html页面javascript? - How to Pass values form One html Page to Another html Page javascript? 如何将 javascript 变量值从一个 html 页面传递到另一个 html 页面? - How to pass the javascript variable value from one html page to the another html page?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM