Reputation: 911
i want to delete php session variable using a key press. so i used these codes,
<script type="text/javascript">
function textsizer(e){
var evtobj=window.event? event : e
var unicode=evtobj.charCode? evtobj.charCode : evtobj.keyCode
var actualkey=String.fromCharCode(unicode)
if(actualkey=="x"){
location.href="biling.php?cmd="+actualkey;}
}
document.onkeypress=textsizer
</script>
<?php if(isset($_GET['cmd'])){
unset($_SESSION["bill_array"]);
header('location:biling.php');
}
?> }
but the problem is, this code also clearing the session when i'm typing "x" in a text box. so i just want to stop that and clear the session only when i press "x" out side of a text box
Upvotes: 1
Views: 1149
Reputation: 5217
You can check the e.target
property to identify which element trigger the keypress
event.
//Does not work on IE
window.addEventListener('keypress', function(e){ console.log(e.target) }, false)
Taking action when e.target.tagName == 'BODY'
is a good choice.
For more info about Event
, check:
Upvotes: 1
Reputation: 7356
I think you can edit your existing function as such:
function textsizer(e) {
var evtobj=window.event? event : e;
var element = evtobj.target ? evtobj.target : evtobj.srcElement;
if (element.tagName.toLowerCase() == "body") {
var unicode=evtobj.charCode? evtobj.charCode : evtobj.keyCode;
var actualkey=String.fromCharCode(unicode)
if(actualkey=="x"){
location.href="biling.php?cmd="+actualkey;
}
}
}
Upvotes: 1