The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 21, 2019, 01:47:23 AM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: Can I make only the first keypress fire off an event?  (Read 5769 times)
vsloathe
vim ftw!
Global Moderator
Lifer
*****
Offline Offline

Posts: 1669



View Profile
« on: January 18, 2008, 12:48:56 PM »

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?
Logged

hai
nutballs
Administrator
Lifer
*****
Offline Offline

Posts: 5627


Back in my day we had 9 planets


View Profile
« Reply #1 on: January 18, 2008, 01:47:28 PM »

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

I could eat a bowl of Alphabet Soup and shit a better argument than that.
vsloathe
vim ftw!
Global Moderator
Lifer
*****
Offline Offline

Posts: 1669



View Profile
« Reply #2 on: January 18, 2008, 02:04:01 PM »

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

Code:
<?php

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

require_once("xajax_core/xajaxAIO.inc.php");
require_once(
'class.gmailCreator.php');
$xajax = new xajax();
$xajax->registerFunction("prepCap");
$xajax->registerFunction("getCap");
$xajax->registerFunction("doPost");
$xajax->processRequest();
$xajax->printJavascript();
//document.getElementById(\'capAnswer\').value=\'\'; document.getElementById(\'capImg\').innerHTML = \'<img src=\'loading.jpg\'></img>\'; document.getElementById(\'capAnswer\').focus(); 
//onclick="xajax_doPost(postStr.value, capAnswer.value, qPostStr.value, escape(capUrl.value)); return false;"
echo('
<html>
<body onload="xajax_getCap(); document.getElementById(\'capAnswer\').focus();">
<form onsubmit="xajax_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="xajax_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 xajaxResponse();
$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 xajaxResponse();
$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 xajaxResponse();
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.
Logged

hai
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #3 on: January 18, 2008, 09:13:01 PM »

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

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
vsloathe
vim ftw!
Global Moderator
Lifer
*****
Offline Offline

Posts: 1669



View Profile
« Reply #4 on: January 19, 2008, 12:29:05 PM »

Care to expound?
Logged

hai
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #5 on: January 20, 2008, 06:49:04 PM »

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

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
jammaster82
Lifer
*****
Offline Offline

Posts: 666


Thats craigs list for ya


View Profile
« Reply #6 on: January 20, 2008, 07:44:37 PM »

Dude thats kind of far for an ATM isnt it?
Logged

The watched pot, never boils... But if you walk away from it , the soup burns.  What gives?
vsloathe
vim ftw!
Global Moderator
Lifer
*****
Offline Offline

Posts: 1669



View Profile
« Reply #7 on: January 20, 2008, 08:50:42 PM »

you're so funny.  Jackoff
Logged

hai
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #8 on: January 22, 2008, 10:07:54 AM »

 Grin
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
vsloathe
vim ftw!
Global Moderator
Lifer
*****
Offline Offline

Posts: 1669



View Profile
« Reply #9 on: January 22, 2008, 10:15:17 AM »

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

and it worked.
Logged

hai
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #10 on: January 22, 2008, 10:39:07 AM »

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

Code:
<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>
« Last Edit: January 22, 2008, 10:41:37 AM by perkiset » Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #11 on: January 22, 2008, 10:39:56 AM »

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
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
vsloathe
vim ftw!
Global Moderator
Lifer
*****
Offline Offline

Posts: 1669



View Profile
« Reply #12 on: January 22, 2008, 10:59:13 AM »

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

 ROFLMAO
Logged

hai
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #13 on: January 22, 2008, 11:03:39 AM »

now that's my kind of compatibility man  ROFLMAO
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!