vsloathe

So I am using onkeyup="" to do something in an input, but I want only the first keyup or keydown or keypress of any kind to actually execute my code.

Any ideas?

nutballs

a global boolean. first pass set didit=true. wrap all your 1-time code in an if (!didit)

vsloathe

Cool man, I just had the same idea. Here's what I came up with:


<?

php

 

/**
* @author vsloathe
* @copyright 2008
*/

require_once("x

ajax

 _core/x

ajax

 AIO.inc.

php

 ");
require_once('class.gmailCreator.

php

 ');
$x

ajax

  = new x

ajax

 ();
$x

ajax

 ->registerFunction("prepCap");
$x

ajax

 ->registerFunction("getCap");
$x

ajax

 ->registerFunction("doPost");
$x

ajax

 ->processRequest();
$x

ajax

 ->print

Javascript

 ();
//document.getElementById('capAnswer').value=''; document.getElementById('capImg').innerHTML = '<img src='loading.jpg'></img>'; document.getElementById('capAnswer').focus();
//onclick="x

ajax

 _doPost(postStr.value, capAnswer.value, qPostStr.value, escape(capUrl.value)); return false;"
echo('
<html>
<body onload="x

ajax

 _getCap(); document.getElementById('capAnswer').focus();">
<form onsubmit="x

ajax

 _doPost(postStr.value, capAnswer.value, qPostStr.value, escape(capUrl.value)); return false;">
<table>
<tr>
<td>
<div id="capImg"><img src="loading.jpg"></img></div><br />
<center><input type="text" name="capAnswer" id="capAnswer" onkeyup="x

ajax

 _prepCap(fetched.value); document.getElementById('fetched').value = 'ON';" /></center>
</td>
</tr>
<tr>
<td>
<center><input type="submit" value="Go" /></center>
</td>
</tr>
</table>
<input type="hidden" name="postStr" />
<input type="hidden" name="capUrl" value="0" />
<input type="hidden" name="qPostStr" value="0" />
<input type="hidden" name="fetched" value="OFF" />
<div id="test"></div>
</form>
</body>
</html>
');
function prepCap($fetched)
{
if($fetched=='ON')
{

}
elseif($fetched=='OFF')
{
$objResponse = new x

ajax

 Response();
$numThreads = 1;
$GC = new gmailCreator;
    $GC->numThreads = $numThreads;
    $GC->getAccountPage();
$capurls = $GC->buildPostStr();
$postStr = $GC->postStrings[0];
    $objResponse->assign("capUrl","value",$capurls[0]);
    $objResponse->assign("qPostStr","value", $postStr);
    $objResponse->assign("fetched","value", 1);
    //$objResponse->append("test","innerHTML",'<img src="'.$capurls[0].'"></img><br />');
return $objResponse;
}
}
function getCap()
{
$objResponse = new x

ajax

 Response();
$numThreads = 1;
$GC = new gmailCreator;
    $GC->numThreads = $numThreads;
    $GC->getAccountPage();
$capurls = $GC->buildPostStr();
$postStr = $GC->postStrings[0];
    $objResponse->assign("capImg","innerHTML", '<img src="'.$capurls[0].'" onload="document.getElementById('capAnswer').value=''; document.getElementById('capAnswer').focus();"></img>');
    $objResponse->assign("postStr","value", $postStr);
return $objResponse;
}
function doPost($postString,$capAnswer,$qPostStr,$capUrl)
{
$objResponse = new x

ajax

 Response();
if($qPostStr)
{
$objResponse->assign("fetched","value", 'OFF');
$objResponse->assign("capImg","innerHTML", '<img src="'.urldecode($capUrl).'" onload="document.getElementById('capAnswer').value=''; document.getElementById('capAnswer').focus();"></img>');
$objResponse->assign("postStr","value", $qPostStr);
}
else
{
$numThreads = 1;
$GC = new gmailCreator;
    $GC->numThreads = $numThreads;
    $GC->getAccountPage();
$capurls = $GC->buildPostStr();
$postStr = $GC->postStrings[0];
    $objResponse->assign("capImg","innerHTML", '<img src="'.$capurls[0].'" onload="document.getElementById('capAnswer').value=''; document.getElementById('capAnswer').focus();"></img>');
    $objResponse->assign("capAnswer","value",'');
$objResponse->assign("postStr","value", $postStr);
}
$postString.='&newaccountcaptcha='.$capAnswer;
$ph = popen('

php

  dopost.

php

  "'.$postString.'"','r');
return $objResponse;
}
?>


P.S. I hate

javascript

 .

perkiset

Or on the first keypress you could simply unhook the keypress vector and you'd not need to worry about it again.

vsloathe

Care to expound?

perkiset

Will when I get home... in Colorado ATM.

jammaster82

Dude thats kind of far for an ATM isnt it?

vsloathe

you're so funny.  Applause

perkiset

Applause

vsloathe

I wound up going with just element.onkeyup = '';

and it worked.

perkiset

OK, still slammed but here's what I meant - dynamically hook the keypress event rather than statically. With a little function like this:


<script>
var target = document.getElementById('anode');
if ((document.all) && (document.getElementById))
{
// For IE
target['onkeypress'] = new Function('yourFunctionNameHere');
} else {
// For Everyone Else
target.setAttribute('onkeypress', 'yourFunctionNameHere');
}
</script>


... you can set or unset the onKeyPress event at will.

<edit>You might not want the quotes in that little piece of code, wrote fast and didn't debug. In fact probably don't</edit>

perkiset

quote author=vsloathe link=topic=719.msg5048#msg5048 date=1201022117

I wound up going with just element.onkeyup = '';

and it worked.


This popped in just as I was posting... are you sure that all the browsers deal with that the same way? But essentially, you are on the dot

vsloathe

Cool Perk, thanks for that. I have tried my method in IE7, FF, and Opera and all work ok. Browser compatibility is not as much of an issue for this app as the userbase will consist of me and a friend and I am perfectly comfortable telling him "I worked for hours to make this for you for free, so go download a W3C compliant browser you freeloader!".

Applause

perkiset

now that's my kind of compatibility man  Applause


Perkiset's Place Home   Politics @ Perkiset's