|
|
 |
|
September 17th, 2006, 10:04 PM
|
#1
|
|
New Member
Join Date: Sep 2006
Location: Sydney
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0
|
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
|
|
|
September 18th, 2006, 12:29 AM
|
#2
|
|
Elite Veteran
Join Date: May 2006
Location: Las Vegas
Posts: 3,784
Thanks: 0
Thanked 0 Times in 0 Posts
|
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.
|
|
|
September 18th, 2006, 12:32 AM
|
#3
|
|
New Member
Join Date: Sep 2006
Location: Sydney
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0
|
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.
|
|
|
September 18th, 2006, 01:15 AM
|
#4
|
|
Elite Veteran
Join Date: May 2006
Location: Las Vegas
Posts: 3,784
Thanks: 0
Thanked 0 Times in 0 Posts
|
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.
|
|
|
September 18th, 2006, 04:01 AM
|
#5
|
|
New Member
Join Date: Sep 2006
Location: Sydney
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0
|
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.
|
|
|
September 18th, 2006, 09:09 AM
|
#6
|
|
Most Reputable Member
Join Date: Apr 2006
Location: Cornwall, UK
Posts: 1,307
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0
|
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.
|
|
|
September 18th, 2006, 08:13 PM
|
#7
|
|
New Member
Join Date: Sep 2006
Location: Sydney
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0
|
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>
|
|
|
September 19th, 2006, 06:08 AM
|
#8
|
|
Most Reputable Member
Join Date: Apr 2006
Location: Cornwall, UK
Posts: 1,307
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0
|
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.
Code:
<script language="JavaScript">
var allow = " ABCDEFGHIJKLMNOPQRSTUVWXYZ,./*[];:1234567890";
var output='';
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(){
document.write(output)
document.write(inpstr)
}
</script>
The bits in bold have either been changed, added or moved. For the purposes of testing, I've added a submit button which as you can see from the code will trigger the 'runscript()' function.
Code:
<form onsubmit='runscript();'>
<input id="test" type="text">
<input type=button' value='Submit'>
</form>
Get rid of the 'display()' function from inside your table element.
Now see how you get on.
Last edited by ukgeoff; September 19th, 2006 at 06:11 AM..
|
|
|
September 19th, 2006, 09:41 PM
|
#9
|
|
New Member
Join Date: Sep 2006
Location: Sydney
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0
|
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?
|
|
|
September 20th, 2006, 07:29 AM
|
#10
|
|
Most Reputable Member
Join Date: Mar 2004
Location: Good Ol'London
Age: 24
Posts: 1,683
Thanks: 1
Thanked 4 Times in 4 Posts
Rep Altering Power: 0
|
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:
Code:
<script>
function Scramble(f){
var output = '';
var s = f.elements['string'].value;
for(var i=0;i<=s.length; i++){
output += ''+s.charCodeAt(i);
}
f.elements['output'].value = output;
}
</script>
<form onsubmit="Scramble(this); return false;">
<input type="text" name="string"/>
<input type="submit" value="GO"/>
<br/>
Output: <input type="text" name="output"/>
</form>
But to write a file you will need some sort of server-side language or use Java instead of Javascript...
Last edited by spinal007; September 20th, 2006 at 07:33 AM..
|
|
|
September 20th, 2006, 08:11 AM
|
#11
|
|
Most Reputable Member
Join Date: Apr 2006
Location: Cornwall, UK
Posts: 1,307
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0
|
Re: Urgent Javascript Help
Let me see the actual code you now have.
|
|
|
September 20th, 2006, 08:20 AM
|
#12
|
|
Most Reputable Member
Join Date: Apr 2006
Location: Cornwall, UK
Posts: 1,307
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0
|
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.
|
|
|
September 21st, 2006, 03:54 AM
|
#13
|
|
New Member
Join Date: Sep 2006
Location: Sydney
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0
|
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
|
|
|
September 21st, 2006, 05:53 AM
|
#14
|
|
Most Reputable Member
Join Date: Apr 2006
Location: Cornwall, UK
Posts: 1,307
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0
|
Re: Urgent Javascript Help
Where is the top part of this file?
Code:
<table border="1"><Tr><td> </td></Tr></table>
Not likely to be the cause of your problem but the highlighted bits above should be al lowercase.
|
|
|
September 21st, 2006, 08:15 AM
|
#15
|
|
Most Reputable Member
Join Date: Mar 2004
Location: Good Ol'London
Age: 24
Posts: 1,683
Thanks: 1
Thanked 4 Times in 4 Posts
Rep Altering Power: 0
|
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
|
|
|
September 21st, 2006, 09:39 AM
|
#16
|
|
Most Reputable Member
Join Date: Apr 2006
Location: Cornwall, UK
Posts: 1,307
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0
|
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.
|
|
|
September 21st, 2006, 10:26 AM
|
#17
|
|
Most Reputable Member
Join Date: Mar 2004
Location: Good Ol'London
Age: 24
Posts: 1,683
Thanks: 1
Thanked 4 Times in 4 Posts
Rep Altering Power: 0
|
Re: Urgent Javascript Help
fair enough...
|
|
|
September 21st, 2006, 07:47 PM
|
#18
|
|
New Member
Join Date: Sep 2006
Location: Sydney
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0
|
Re: Urgent Javascript Help
Hi Geoff
I just checked that but no change i still get an empty alert box.
Adam
|
|
|
September 21st, 2006, 07:52 PM
|
#19
|
|
New Member
Join Date: Sep 2006
Location: Sydney
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0
|
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
|
|
|
September 22nd, 2006, 06:13 AM
|
#20
|
|
Most Reputable Member
Join Date: Apr 2006
Location: Cornwall, UK
Posts: 1,307
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0
|
Re: Urgent Javascript Help
I'd like to see the complete file.
There should be some code above what you have shown us so far.
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|