Home  >  Article  >  Web Front-end  >  JavaScript method to transfer values ​​between pages_javascript skills

JavaScript method to transfer values ​​between pages_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:05:111076browse

The example in this article describes how JavaScript implements value transfer between pages. Share it with everyone for your reference. The details are as follows:

The question is as follows:

In the a.html page, the onsubmit event of ff9c23ada1bcecdd1a0fb5d5a0f18437 calls a method foo() to open the b.html page and pass parameters to b.html at the same time. In the method foo(), variable parameters need to be passed to the b.html page. The b.html page accepts parameter values, but server-side technology cannot be used.

The solution code is as follows:

a.html page is as follows:

<html>
<head>
  <title> demo </title>
  <meta name="Author" content="xugang" />
  <script type="text/javascript">
  function foo(){
   var a ="abc"; // a为变量值
   var str = "b.html&#63;id="+a+";";
   //alert(document.frm.action);
   //方案一(无效)
   // document.frm.action = str;
   //方案二(无效)
   // window.location.href = str;
   //方案三(有效)
   window.location.replace(str);
   return false;
  }
 </script>
</head>
<body>
   <FORM name="frm" method="get" 
   onsubmit = "return foo()" >
      <INPUT TYPE="SUBMIT" />
   </FORM>
</body>
</html>

Note: The b.html page must exist in advance.

b.html The code to obtain the parameter value is as follows:

b.html part of the code

var getQueryString = function(name) {
  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
  var r = window.location.search.substr(1).match(reg);
  if (r != null) return r[2]; return "";
}

Supplement:

myjs.js code:

function foo(){ 
    var str = "abc"; 
    //document.forms[0].hid.value = str; 
    var frm = window.event.srcElement; 
    frm.hid.value = str; 
    return true; 
}

a.html code:

<html> 
<head> 
 <title> demo </title> 
 <meta name="Author" content="xugang" /> 
 <script src="myjs.js"></script> 
</head> 
<body> 
 <FORM name="frm" METHOD="get" ACTION="b.html" 
 onsubmit="return foo()"> 
  <INPUT TYPE="hidden" id="hid" name="hid"> 
  <INPUT TYPE="submit" value="提交"> 
 </FORM> 
</body> 
</html>

Note: When passing a value to the b.html page, the b.html page must already exist!

b.html code:

<HTML> 
 <HEAD> 
  <TITLE> New Document </TITLE> 
 </HEAD> 
 <BODY> 
  <SCRIPT LANGUAGE="JavaScript"> 
   document.write(decodeURIComponent(location.search.substr(3)));
  </SCRIPT> 
 </BODY> 
</HTML>

I hope this article will be helpful to everyone’s JavaScript programming design.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn