最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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.

Please provide me a simple example.

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.

Please provide me a simple example.

Share Improve this question asked Nov 20, 2012 at 9:38 user1825788user1825788 3592 gold badges4 silver badges5 bronze badges
Add a comment  | 

5 Answers 5

Reset to default 9

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:

<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.

  • location.search attribute contains the chain of parameters.
  • ? concatenates the URL and the string of parameters.
  • & separates multiple parameters.
  • = assigns a value to the variable.

Here is the complete code to process data sent on param-received.html:

<script language="JavaScript">
  function processForm()
  {
    var parameters = location.search.substring(1).split("&");
    var temp = parameters[0].split("=");
    l =  decodeURIComponent(temp[1]);
    alert(l);
  }
  processForm();
</script>

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

For writing and reading cookies check here

You could use the GET part of the request or cookies

if url parameters are an option you could use this

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

another might be cookies


was beaten to the cookie part :p

edit indeed not a good cookie reference this one is better http://www.w3schools.com/js/js_cookies.asp

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.

发布评论

评论列表(0)

  1. 暂无评论