General discussion

Need assistabce with code

I'm trying to do a little personal experiment with changing the text in a document through JavaScript and I'm kind of lost :s. I'm not too good with JavaScript yet, only just started trying to learn a couple days ago. Here's my code:

<script type="text/javascript">

var xtc = new Array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D ","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U", "V","W","X","Y","Z","1","2","3","4","5","6","7","8","9","0","!","@","# ","$","%","^","&","*","(",")","-","_","+","=","[","{","]","}","|","\\" ,";",":","\"","'","/","?",">","<",",",".","~","`");

function fd() {

//using this function I'm able to write a random character from Array xtc to the document loaded


var za = xtc[Math.floor(Math.random()*90)];
return za;

}

function writeToDoc() {

//opens the document, writes the value returned in fd(), and then closes the file

var fso = new ActiveXObject("Scripting.FileSystemObject");
var fh = fso.CreateTextFile(document.getElementById('txtfile').value, true);

fh.WriteLine(fd());
fh.Close();

}


</script>
...
<!--finds the file, and then once selected, writeToDoc() is performed, writing to the document-->
<input type="file" onChange="writeToDoc(this)" id="txtfile" value="#" />


What I want to do is make it so I can replace a character in the document, such as the letter "a",for as many times as it occurs, for a random character from the array, and do that for each character found (like replacing "a" for "t", "b" for "4", so on and so forth). Is this possible without changing my script completely or making it too long?

Discussion is locked

Follow
Reply to: Need assistabce with code
PLEASE NOTE: Do not post advertisements, offensive materials, profanity, or personal attacks. Please remember to be considerate of other members. If you are new to the CNET Forums, please read our CNET Forums FAQ. All submitted content is subject to our Terms of Use.
Reporting: Need assistabce with code
This post has been flagged and will be reviewed by our staff. Thank you for helping us maintain CNET's great community.
Sorry, there was a problem flagging this post. Please try again now or at a later time.
If you believe this post is offensive or violates the CNET Forums' Usage policies, you can report it below (this will not automatically remove the post). Once reported, our moderators will be notified and the post will be reviewed.
Comments
- Collapse -
Three notes...

1.) ActiveX is only supported by IE and, given the security risks, even Microsoft has largely locked down the functionality in IE8+. This is not something that can be attempted cross-browser.

2.) If you replace every 'A' with a 'W' for instance, then replace every 'W' with a 'S', you will inadvertently have turned every 'A' and every 'W' into an 'S'. Depending on your goal This may be a critical flaw.

3.) If the letter transformation is completely random, there will be no clear way of reverting the process to restore the original content. Again, this may or may not be a deal-breaker.

It can be done (see JavaScript's regex functionality), but keep in mind the above before investing time in a problematic implementation.

John

- Collapse -
Thanks for your input :)

I'm a beginner at using JavaScript so I'm not very familiar with it just yet, so thanks for the notes I'll definitely keep those in mind.

The Random() was just there so I could quickly replace without writing too much code at the time, but later on I can easily figure out how to replace piece by piece. My only problem is I can't seem to use the replace() function on the text file, I can only write to it. Is there any other ways to replace it without writing over the document with predefined text?

- Collapse -
The method doesn't modify the object's contents...

For instance:

var string = "CNET Forums";
string.replace("Forums", "Forums rule!");
// string = "CNET Forums"

var string = "CNET Forums";
string = string.replace("Forums", "Forums rule!");
// string = "CNET Forums rule!"

It's just the nature of the method you're using; it returns the result but doesn't modify the object. Just compute the result and then write it to the file.

John

CNET Forums

Forum Info