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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13470285/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 12:10:04  来源:igfitidea点击:

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

javascripthtml

提问by user1825788

In first page im getting value in textbox i need to pass it to another page which is divided into 2 frames. I need to display that value in first frame's html page. Please provide me a simple example. I tried with window.document.getElementById("inputbox1").value but im unable to get the value.

在第一页中,我在文本框中获取值,我需要将其传递到另一个页面,该页面分为 2 个框架。我需要在第一帧的 html 页面中显示该值。请给我一个简单的例子。我尝试使用 window.document.getElementById("inputbox1").value 但我无法获取该值。

Please provide me a simple example.

请给我一个简单的例子。

回答by Tom Sarduy

I would go with localStorage, as @MicrosoftGoogle propose, but is not well supported yet, you can use pure javascript to achieve this. You will have something like this on your form page:

我会使用 localStorage,正如@MicrosoftGoogle 建议的那样,但还没有得到很好的支持,您可以使用纯 javascript 来实现这一点。您的表单页面上将有类似的内容:

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

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>

回答by kiranvj

Write the value in a cookie and read the cookie from the other page.

将值写入 cookie 并从其他页面读取 cookie。

For writing and reading cookies check here

有关写入和读取 cookie 的信息,请在此处查看

回答by ufosnowcat

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

回答by banzsh

You could use the GET partof the request or cookies

您可以使用请求或cookieGET 部分

回答by tamil

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 方法发送的函数。现在您将从 url 获取变量的值。