This is a discussion on "Urgent Javascript Help" within the JavaScript Forum section. This forum, and the thread "Urgent Javascript Help are both part of the Program Your Website category.
|
|
|
|
|
![]() |
||
Urgent Javascript Help
|
||
| Notices |
![]() |
|
|
LinkBack | Thread Tools |
|
#1
|
|||
|
|||
|
Urgent Javascript Help
Hi All
I have a problem with Javascript, I need to do something fairly complicated and I'm not sure how. first let me explain what I am trying to do. I need a user to be able to enter a single word in a text form on a page. i then need to seperate every character in that string to create new variables (example: var charater1 = string.charAt(0)) there are ten characters in total. I then need to compare the character to find the appropriate integer number for it, for example A would equal the integer number 1. once I have converted all ten characters I then need to write them to a file, something like a text file or the like. does anyone know how to do this? really need help |
|
|
|
#2
|
||||
|
||||
|
Re: Urgent Javascript Help
Writing to a text file is possible, but it makes use of ActiveX. Hence, only Internet Explorer users will be able to do it. Unless I am misinformed, Firefox and Safari won't work.
PHP is a language that you can do what you want in. In case this is what you meant: There is no way to write a file to a users computer. Only files on your server that you have permission to read and write can be edited. |
|
#3
|
|||
|
|||
|
Re: Urgent Javascript Help
i am using a web server so I can in theory read and write to a text file contained on the web server. Unfortunatley I am confined to Javascript to do this (it's one of the limitations to our software). using an ActiveX shouldn't be a problem, I just can't get my head around the code to do what i need to be done.
|
|
#4
|
||||
|
||||
|
Re: Urgent Javascript Help
I'm afraid I'm not much help in that field. Do some Google searches to try and locate a nice tutorial on it. Other than a simple Ajax function, I haven't delved into ActiveX objects because I've been working on a Mac for 4 years.
|
|
#5
|
|||
|
|||
|
Re: Urgent Javascript Help
Thanks ryanfait, it is a tricky thing that I'm trying to do and would be easier if I wasn't limited to using Javascript.
I figured that if I use something like a two dimension array i could get half way there but the problem there is how to I split the characters of the string and then search through the array to find the corresponding letter and then display the second column of that array and store it into a new variable. it's bloddy difficult. |
|
#6
|
|||
|
|||
|
Re: Urgent Javascript Help
Set up a string of the allowable characters in the order you want them enumerated, eg;
var allow = 'ABCDE...'; var inpstr = document.getElementById('inputID'); var output; for (i=0; i < inpstr.length; i++){ output = output + (allow.indexOf(inpstr.charAt(i)) + 1) + ':'; } <!-- The + 1 above allows for strings being indexed from 0. The ':' is used as a seperator. Up to you if you need something like this --> Can't tell you how to use ActiveX to right the output variable to a local file. |
|
#7
|
|||
|
|||
|
Re: Urgent Javascript Help
I have tried to implement that code, using a function from a form in the body of the page but it doesn't work. I think I might have done it wrong? the code is:
<script language="JavaScript"> var allow = " ABCDEFGHIJKLMNOPQRSTUVWXYZ,./*[];:1234567890"; var inpstr = document.getElementById("test"); var output=0 function runscript(form){ for (i=0; i < inpstr.length; i++){ output = output + (allow.indexOf(inpstr.charAt(i)) + 1) + ':'; } } <!-- The + 1 above allows for strings being indexed from 0. The ':' is used as a seperator. Up to you if you need something like this --> function display(){ document.write(output) document.write(inpstr) } </script> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <form> <input id="test" type="text" onKeyUp="runscript(this.form)"> </form> <table border="1"><Tr><td><script>display()</script></td></Tr></table> </body> |
|
#8
|
|||
|
|||
|
Re: Urgent Javascript Help
For starters, the only time your display() function gets called is when the page first loads. It needs to be called once your runscript() function finishes. At the moment, your runscript() function is called after each character is entered. Is this your intention? Originally you refered to the user entering a single word, so I had assumed they would enter the completed word before the function was run. Otherwise you don't need the for loop. Also, the way you have the code, the var 'inpstr' is set when the page is loaded, not when the input text is evaluated.
Now see how you get on. Last edited by ukgeoff; Sep 19th, 2006 at 09:11. |
|
#9
|
|||
|
|||
|
Re: Urgent Javascript Help
Thanks ukgeoff, I tried the mods to that script but when I try and run it, It doesn't work.
I can't see why so I put an alert instead of trying to write it to a table but the alert box just came up blank. any suggestions? |
|
#10
|
||||
|
||||
|
Re: Urgent Javascript Help
You just had to search...
http://www.irt.org/script/279.htm The charCodeAt function gives you the ASCII code for any given character... The easiest way would be to do this:
Last Blog Entry: Random String in Javascript (Apr 21st, 2008)
Last edited by spinal007; Sep 20th, 2006 at 10:33. |
|
#11
|
|||
|
|||
|
Re: Urgent Javascript Help
Let me see the actual code you now have. |
|
#12
|
|||
|
|||
|
Re: Urgent Javascript Help
Spinal:
If you read the initial post, the person wants 'A' to be 1 and so on, so the use of the charCodeAt function is not relevant. It will just confuse the issue for them. |
|
#13
|
|||
|
|||
|
Re: Urgent Javascript Help
Hi Geoff the code I am using is : <script language="JavaScript"> var allow = "ABCDEFGHIJKLMNOPQRSTUVWXYZ,./*[];:1234567890"; var output=''; var inpstr=''; function runscript(){ var inpstr = document.getElementById("test"); for (i=0; i < inpstr.length; i++){ output = output + (allow.indexOf(inpstr.charAt(i)) + 1) + ':'; } display(); } <!-- The + 1 above allows for strings being indexed from 0. The ':' is used as a seperator. Up to you if you need something like this --> function display(){ alert(output) } </script> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <form> <input id="test" type="text"> <input name="Submit" type='button' value='Submit' onClick="runscript()"> </form> <table border="1"><Tr><td> </td></Tr></table> </body> </html> I added the var inpstr=''; because when I ran it the first time it said that inpstr was not defined. thanks again for your help Adam |
|
#14
|
|||
|
|||
|
Re: Urgent Javascript Help
Where is the top part of this file?
|
|
#15
|
||||
|
||||
|
Re: Urgent Javascript Help
Right, I don't mean to confuse anyone BUT...
"a".charCodeAt(0) returns 97 "a".charCodeAt(0)-96 returns 1 that's the encryption problem solved... charCodeAt is the quickest way to retrieve the character code AND can also be used for validation: if(char.charCodeAt(0)>=65 && char.charCodeAt(0)<=122){ // do the thing } there's no need for long string constants OR regular expressions. then all you need is to build a decent interface, which you guys seems to be doing. This might be usefull: www.lookuptables.com - ASCII Table
Last Blog Entry: Random String in Javascript (Apr 21st, 2008)
|
|
#16
|
|||
|
|||
|
Re: Urgent Javascript Help
Yes I take your point but it assumes that the allowable characters are going to be contiguous in the ascii table and there was no indication that this was going to be the case. The version I gave him allows the user to set-up whatever sequence of characters they want. |
|
#17
|
||||
|
||||
|
Re: Urgent Javascript Help
fair enough...
Last Blog Entry: Random String in Javascript (Apr 21st, 2008)
|
|
#18
|
|||
|
|||
|
Re: Urgent Javascript Help
Hi Geoff
I just checked that but no change i still get an empty alert box. Adam |
|
#19
|
|||
|
|||
|
Re: Urgent Javascript Help
Hi geoff
I have fixed that but it didn';t change the resul;, I still get an empty alert box, Adam |