在textarea onkeypress中创建一个cookie并发送它

时间:2015-01-22 10:15:25

标签: javascript jquery html cookies

您好我想知道是否可以在textarea,onkeypress中创建cookie,然后将cookie发送到结果页面?好吧,如果用户在textarea中键入B或R,它会创建一个cookie或类似的东西并将其发送到结果页面。我不知道它是否有意义,但我会尽我所能,这是每页的总代码:

Exercise1.html:

<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Exercise1</title>
<style>
#first-child {
       width: 200px;
       height: 200px;
       background: white;
       margin-top: 150px;
       margin-bottom: 50px;
       margin-right: 0px;
       margin-left: 550px;
       -webkit-animation: myfirst 1s;
       animation: myfirst 1s;
}
@-webkit-keyframes myfirst {
       0% {background: white;}
      20% {background: white;}
      40% {background: white;}
      60% {background: white;}
      80% {background: white;}
     100% {background: red;}
}
.first-parent {
       color: blue;
       margin-top: 5px;
       margin-bottom: 50px;
       margin-left: 600px;
       margin-right: 0px;
}
.second-parent {
        color: red;
        margin-top: 0px;
        margin-bottom: 50px;
        margin-left: 40px;
        margin-right: 0px;
}
p {
margin-left: 640px;
}
textarea {
    width: 30px;
    height: 30px;
    visibility: ;
    margin-top: -50px;
    margin-bottom: 0px;
    margin-left: 0px;
    margin-right: 0px;
}
</style>
</head>
<body>

<div id='first-child'></div>

<button class="first-parent" onclick="window.location.href='Exercise2.html'">B</button>

<button class="second-parent" onclick="window.location.href='Exercise2.html'">R</button>
<br />
<p>1/2</p>

<form id="form1" action="result.html" method="get">
   <textarea id="aboutme" name="Key Pressed: " autofocus></textarea>
   <input type="submit" class="bottom" name="submit" id="submit" value="Test" >
</form>

<script>
document.onkeypress = function(b) {
    b = b || window.event;
    var charCode = b.charCode || b.keyCode,
    character = String.fromCharCode(charCode);

console.log(charCode);
window.location.href="Exercise2.html";
};

document.onkeypress = function(r) {
    r = r || window.event;
    var charCode = r.charCode || r.keyCode,
    character = String.fromCharCode(charCode);

console.log(charCode);
window.location.href="Exercise2.html";
};
</script>
</body>
</html>

Result.html:

<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8>
<title>Result</title>
</head>
<body>
<h1>This is the Results</h1>
<script>
var queryString = decodeURIComponent(window.location.search);
queryString = queryString.substring(1);
var queries = queryString.split("&");
for (var i = 0; i < queries.length; i++) {
     document.write(queries[i] + "<br>");
}
</script>
</body>
</html>

如果您想知道我为什么发布整个代码,我会在记事本中写这个。正如你所看到的,我知道jQuery和Javascript,如果有任何其他解决方案我需要详细的描述或代码。

-Nikki

1 个答案:

答案 0 :(得分:0)

document.body.addEventListener('keydown',function(e){
 
  if(e.keyCode=="66" ||e.keyCode=="82"){
     if(e.keyCode=="66"){var key= "B";}
  if(e.keyCode=="82"){var key="R";}
  var text ="key pressed = "+key+"\n\n and the code for that key is "+e.keyCode+" \n\n  And the text in the textarea is \n\n"+
  document.getElementById('theText').value;
    alert(text);
  
 document.cookie="text="+text; //or to send url do
    window.location.href="http://myURL.com?text="+text;
  
  }
  
  
  });
<textarea id="theText"> </textarea>