473,668 Members | 2,261 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

implementing undo and redo for textarea's - does Javascript support static variables?

I'm offering users the ability to type weblog posts into a form and
post them. They type the text into a TEXTAREA which is on a form. The
form, when submitted, hits a PHP script. Before it is submitted, while
they are typing, I'm trying to offer them some common word processing
functions.

I want to implement unlimited undo and redo for the textarea. I've set
the textarea to onChange="addTo ArrayOfPastWork ()";

My undo button gives me "undefined" , but I suppose I can fix that with
a check on the index. My redo button seems to work.

Here is my question:
Currently I'm implementing currentFormInde xValue as a global variable,
but really I want it to be a static variable in a function that
handles both the undo and redo. Does Javascript allow static variables?
I checked here: http://developer.irt.org/script/vfaq.htm
but it didn't mention static variables.


// 12-04-04 - our form functions, especially McFormsPrintNow ,
will output text boxes and textareas
// that call addToArrayOfPas tWork() whenever anything changes.
Then elementsAdminSh owFormattingBut tons
// contains an undo and a redo button that lets users step
through the array and bring back
// past iterations of work.
arrayOfPastForm Work = new Array();
currentFormInde xValue = 0;

function undoFormWork(my Field) {
currentFormInde xValue = currentFormInde xValue - 1;
myField.value =
arrayOfPastForm Work[currentFormInde xValue];
}

function redoFormWork(my Field) {
currentFormInde xValue = currentFormInde xValue + 1;
myField.value =
arrayOfPastForm Work[currentFormInde xValue];
}
function addToArrayOfPas tWork(myField) {
var newValue = myField.value;
arrayOfPastForm Work.push(newVa lue);
}

Jul 23 '05 #1
6 8075
few comments:

1. you need to catch the onpropertychang e and not the onchange event
(at least in IE). in addToArrayOfPas tWork you'll need to check if the
value property was changed (using event.propertyV alue).
2. in redoFormWork and undoFormWork you'll need to detach the event
before assiging the value and attach back at the end of the functions
(or use some kind of a flag)
3. currently you do not clean the array from the current point to the
end once the user make a change (to prevent the redo action after a
change)
4. you cannot create a real static variable in javascript (you can use
prototype but it will not prevent you from changing the variable in a
specific instance of an object)

lk******@geocit ies.com wrote:
I'm offering users the ability to type weblog posts into a form and
post them. They type the text into a TEXTAREA which is on a form. The
form, when submitted, hits a PHP script. Before it is submitted, while they are typing, I'm trying to offer them some common word processing
functions.

I want to implement unlimited undo and redo for the textarea. I've set the textarea to onChange="addTo ArrayOfPastWork ()";

My undo button gives me "undefined" , but I suppose I can fix that with a check on the index. My redo button seems to work.

Here is my question:
Currently I'm implementing currentFormInde xValue as a global variable, but really I want it to be a static variable in a function that
handles both the undo and redo. Does Javascript allow static variables? I checked here: http://developer.irt.org/script/vfaq.htm
but it didn't mention static variables.


// 12-04-04 - our form functions, especially McFormsPrintNow ,
will output text boxes and textareas
// that call addToArrayOfPas tWork() whenever anything changes.
Then elementsAdminSh owFormattingBut tons
// contains an undo and a redo button that lets users step
through the array and bring back
// past iterations of work.
arrayOfPastForm Work = new Array();
currentFormInde xValue = 0;

function undoFormWork(my Field) {
currentFormInde xValue = currentFormInde xValue - 1;
myField.value =
arrayOfPastForm Work[currentFormInde xValue];
}

function redoFormWork(my Field) {
currentFormInde xValue = currentFormInde xValue + 1;
myField.value =
arrayOfPastForm Work[currentFormInde xValue];
}
function addToArrayOfPas tWork(myField) {
var newValue = myField.value;
arrayOfPastForm Work.push(newVa lue);
}


Jul 23 '05 #2
Thank you, that is some very valuable feedback.

To clean the array from the current point to the end once a user makes
a change, to prevent redo, do I simply use array.pop() on all the
elements above the current index? Is there simpler way to do it than a
for loop?

As to static variables, I noticed this article by Richard Conford on
the subject:
http://www.litotes.demon.co.uk/js_in...te_static.html

Jul 23 '05 #3
On 9 Dec 2004 11:52:49 -0800, <lk******@geoci ties.com> wrote:

[snip]
To clean the array from the current point to the end once a user makes a
change, to prevent redo, do I simply use array.pop() on all the elements
above the current index? Is there simpler way to do it than a for loop?


Yes. Assign a value to the length property of the array. If the number is
smaller than the current length, the array will be truncated.

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #4
Hot tip. So if I have this:

var myArray = new Array();
myArray[0] = 'Napoleon';
myArray[1] = 'Hannibal';
myArray[2] = 'Rommel';
myArray[3] = 'Caesar';
myArray[4] = 'Patton';

then if I go like this:

myArray.length = 3;

then the last two items, holding the names of Caesar and Patton, are
gone? If I then use push(), I'm placing something into the 4th
position, which will have an index of 3?

Jul 23 '05 #5
On 9 Dec 2004 14:40:54 -0800, <lk******@geoci ties.com> wrote:

[array, length 5]
myArray.length = 3;

then the last two items, holding the names of Caesar and Patton, are
gone?
You could have just tried it, you know. :P But yes, only the first three
will remain in the array.
If I then use push(), I'm placing something into the 4th position, which
will have an index of 3?


Yes.

As an aside, you might want to be careful using push and pop; versions of
IE earlier than IE5.5 don't implement it (unless the user updated their
JScript implementation separately).

If this is a concern, you can test for them and write your own versions if
they're not available:

if(Array.protot ype) {
if(!Array.proto type.push) {
Array.prototype .push = function() {
for(var i = 0, n = arguments.lengt h; i < n; ++i) {
this[this.length] = arguments[i];
}
return this.length;
};
}
if(!Array.proto type.pop) {
Array.prototype .pop = function() {var e, n;
if((n = this.length)) {
e = this[--n]; this.length = n; return e;
}
};
}
}

or, as far as pushing is concerned, it may be easier to write

arr[arr.length] = ...;

if only one value is concerned.

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #6
Can I call both the onChange and the onPropertyChang e events, like
this? (Please ignore the escapes, this is PHP code):

<textarea id=\"$inputId\" name=\"$inputNa me\" style=\"height: 200px;
width:100%;\" class=\"$class\ " onChange=\"addT oArrayOfPastWor k(this)\"
onPropertyChang e=\"addToArrayO fPastWork(this) \">

Also, when you write this:
"in redoFormWork and undoFormWork you'll need to detach the event
before assiging the value and attach back at the end of the functions
(or use some kind of a flag)"

Do you mean I need to protect against multiple events happening at
once? Do you mean I should set a flag to lock in the array to just one
function till a value has been assigned? If you meant something else,
could you elaborate?

Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3773
by: Mickel Grönroos | last post by:
Hello everybody I'm developing a tool in Tkinter and would like to add Undo and Redo commands to my Edit menu. Does somebody know if anybody has implemented standard Undo/Redo as a Python module? I could not find any info with Google on the matter. Cheers, Mickel Grönroos
1
2549
by: TEK | last post by:
Hello I'm wondering if anyone out there might give some input/suggestions/viewpoints around the Command pattern. In my case, the number one priority for using the pattern is undo support. Some now, a lot more in the future. I think I have a pretty good understanding about how the command pattern is tought to work, however during implementation I'm htting a couple of quite large design considerations that I'm not sure I have the answare...
3
11627
by: babylon | last post by:
any facilities in csharp that can help me implmenting undo/redo in my application? thx
4
4713
by: thanigai | last post by:
How to implement undo/redo technique in C#? The undo/redo are not only for text changes, it also support previous action like creation of an object.
11
15162
by: Mad Joe | last post by:
I'm using a richTextBox for editing a source code (with Syntax Highlighting) of my own programming language... How come that myRichTextBox doesn't respond to Undo/Redo functions by using default shortcut keys, or even programaticaly (by using Button_onClick event)? if(myRichTextBox.CanUndo == true) { myRichTextBox.Undo(); }
3
3156
by: GoogleEyeJoe | last post by:
Dear ladies and gents, I'm trying to determine whether the .NET Framework implements a means of transactional processing without the need for a database. Essentially, I'd like to enlist light-weight portable business objects within transactions so that that have the ability to roll-back if the client of the object wishes to undo any changes made. For example, if I had a, let's say for sake of simplicity, a person class that had two...
0
8459
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8791
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8653
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7398
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5677
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4202
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4373
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2784
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1783
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.