optikalefx Posted March 12, 2008 Share Posted March 12, 2008 So i found a way using keycodes to detect when the user types .com but is there a way that when it detects .com to search backwards till it gets to a space and forwards till it gets to a space? for example if you type www.helloworld.com/boots.htm it would store into a string "www.helloworld.com/boots.htm" So is there way to index back till it hits a space and then forward till it hits a space? Quote Link to comment Share on other sites More sharing options...
nogray Posted March 12, 2008 Share Posted March 12, 2008 you can loop through a string using it's length property and charAt() method e.g. var str = "hello world"; for (i=0; i<str.length; i++){ if (str.charAt(i) == " ") alert("space"); } Not tested Quote Link to comment Share on other sites More sharing options...
optikalefx Posted March 12, 2008 Author Share Posted March 12, 2008 but i dont have the string... I need to get the string Quote Link to comment Share on other sites More sharing options...
haku Posted March 12, 2008 Share Posted March 12, 2008 You can get the string with: onkeypress="functionName(this.value)" Quote Link to comment Share on other sites More sharing options...
optikalefx Posted March 12, 2008 Author Share Posted March 12, 2008 document.addEventListener('keypress',keyPressed,true); } function keyPressed(e) { alert(e.value); } and document.addEventListener('keypress',keyPressed,true); } function keyPressed(e) { alert(this.value); } they both alert undefined Quote Link to comment Share on other sites More sharing options...
nogray Posted March 12, 2008 Share Posted March 12, 2008 addEventListener = Firefox attachEvent = IE Quote Link to comment Share on other sites More sharing options...
optikalefx Posted March 12, 2008 Author Share Posted March 12, 2008 i appreciate the advice, i was looking for something more advance, i guess i should have said that. I got it working. I add the event Listener to the document for keyUp. (ff only). And i take in event e as always. I check to see if you hit space or enter, if so, reset the sentence back to blank. elsewise check to see if you hit period because the charcode returns 2/4 instead of . elsewise use String.fromCharCode to convert the unicode to the letter. Now that returns all caps. so we send it to lowercase letters to be better readable. We then dump to the js console to see what we get. the only problem is the uppercase lowecase part. if anyone can find a solution, id love it. I cant seem to find a way to figure out what case the letter was when the user hit it. document.addEventListener('keyup',detectWord,true); function detectWord(e) { if(e.keyCode == 32 || e.keyCode == 13) { sentence = ""; } else if(e.keyCode == 190) { sentence += "." } else { sentence += String.fromCharCode(e.keyCode); } sentence = sentence.toLowerCase(); //dump(sentence+"\n"); } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.