Client side scripting is perceived by a few like an approach that
illegitimately tampers with a world that should have been all style sheets (
css) layout driven, with
server side scripting allowed by necessity.
Not that the latter is better tolerated than client side scripting is by those who believe in a css approach as the
big resolver (we carry our bias and illusions
truly everywhere, even on browsers): only, they just can't do anything about it because when we are on the server side a dedicated language tailored for the specific platform of the server Operative System is a requirement that cannot be eluded no matter how despised it would be because belonging to the same
scripting paradigm.
Yet, it is barely comprehensible this apparently widespread distrust of the
client side: all web logs show that 97% of the visits have javascript enabled (and that 3% is likely mostly composed by search engines visiting bots), important enterprises like
Hotmail rely on it too and
prosper, and I can't see why we should portray neglecting client side as an improvement rather than as an
impoverishment: all the power that client side scripting undoubtedly provides is a fact, and the pretence we would have been gaining something with the stance just adds conceit to a wrong thesis: we are
losing something, and not gaining, when we give up something.
I agree
100% that a server side engine should
never, absolutely
never, rely or trust on the client side not even for mere incoming data validation.
A Marc Chagall painting
|
Yet, what I am here about to propose to your attention is a
javascript class that does something on the client side that not only can prove its patent utility for the server side end, but that could
not be achieved in any other fashion - unless resubmitting to the server side over and over again just to produce html changes on the client side page that are
not mission critical
but which none the less
still are
interesting additional features and that can be attained
staying on the client we
already are on.
So that when in order to do this we predicate the solution would have consisted in
repeatedly requesting again from the server side non mission critical and yet interesting features, what we predicate is a
plain misconception, which a few
even argue would tantamount to "spreading" good programming practice and
performance enhancement!
Moreover, this javascript class adds a
significant value to a page and not a
perfunctory one (this is not
scripting frippery for decorative purposes and frills), while at the same time providing a sheer enrichment in the layout and, last but not least, while also allowing for a default setting which won't send things awry if the client side is not supported (
notorious javascript disabled, a phoenix we all hear about but we
never actually meet): in fact, if javascript would not be enabled, as long as your form holds a default set of form fields, this (alleged and anyway remotely) possible inability to
add more fields (this is what this class does) to a form on the fly via javascript, would
not compromise one thing on the server side, whereas if javascript is enabled (as it is 97% of the times) the gain is
remarkable: more fields to be processed on the server side
in case the client surfing the page requires them - and don't you wish to provide your end users with
more functionalities rather than less, if this addition does not compromise a
basic default setting.
And so, being things like this, no one can
disprove any longer the utility of this javascript approach to
support server side scripting too. This is a
fact.
I have developed this class working on the field, I mean dealing with an
actual server side database interface that had to print a set of
html form fields, populated with data taken from the database, whereas the need to add fields via client side script arose as an
inherently interesting option.
This was basically to serve a
many to one SQL table which could allow, for one type of element, a manifold set of possible answers - from 1 till
x.
Thus what was needed was
just one default html form field
and the possibility
to add more on demand in case the surfer wished so.
Moreover, this javascript can produce not only form fields, or remove them on demand, but also all the wrapping html you may want for each of them, in case these form fields are conceived as more complex html units and therefore should also have around them a set of commands or of other form elements or of Css statements that provide them all with one same feeling.
Not only, but this class proved its utility also for those cases of SQL fields that would allow only for a limited amount of chars (say, typically, the 255
varchar mySQL data type limit) in order to avoid filling a SQL table with
blob data type, and that yet were also fit, in some cases, to extend their functionality to twice or thrice that amount: in this case,
multiple additional form fields with a limit of 255 chars could be
produced on demand via client side scripting and this only
if and when needed by the client.
The thus produced fields could then either be
recombined (once elaborated onto a server side interpreter) or assigned to
multiple SQL table fields in a
many to one relation so to build texts (aren't we in the
hypertext era?) that are well above the 255 chars limit and
yet without forcing the SQL tables to host too obtrusive data types (like the
blob data type).
This is
client side truly helping the
server side, while compromising
nothing.
Working Example formAdder Set Up Guidelines Summary:
- var x=new formAdder('foo');
- Optional step: change class properties?
x.wrapperTag="DIV";
-
x.set('');
Use this in the TEST:
x.set('
');
Use this in the TEST:
- x.addLayer();
- You could now modify the properties of the newly added layer on the fly if you want, for the class only assigns to it an id, a name, and a class name.
To change the properties, use the class variable named last; instance:
x.last.style.color='#ff0000';
x.last.value='I am layer n.' + x.amount;
WARNING: for this feature to work (namely for calling in the class property named "last" and affecting through it the designed form tag), if you pass replacements in the shape @#Number, it is imperative that inside your main FORM FIELD nested within the bigger definition you assign this very same line:
id="@#2"
Anyway, the class property named "last" will refer to an object with an id corresponding to the replacement for @#2, so if you use such property make sure such an id (the replacement for @#2) exists in some element at least. The class is designed to avoid errors if you don't comply with this requirement; but yet, then, if this compliance lacks and none the less you also use last do not wonder why you see affected another layer: the _wrapper_.
Example of usage of the class variable named last: what is in this textarea will be executed by the script on the run, so be careful if you decide to edit it.
- x.removeLayer(
, remains?
);
TEST IT (click the buttons
after the image)
A beautiful John Sargent Singer painting
|
This would be the stealth foo layer used to append, here shown just for clarity
Of course, it is possible that you are now wondering whether this class which works so good with form fields, could be used to add and remove
other type of tags too than just form fields, or anyway set of tags that do not involve form fields somewhere.
The answer is:
definitely yes, it can. Just run it in the
@# definitions fashion, defining there what these html units must be.
This is how flexible these scripts can be, though I have no personal illusion or any peculiar narcissism that would have barred me from knowing what I know all to well, namely that you can also find questionable solutions on this website, now and then, or that a misconception is possible; and yet it is still true that
Unitedscripters is a place where you can
also find truly
interesting things
at times - which nothing prevents you from making better, possibly dropping an acknowledgement as I myself various times do here when I used a
tidbit of another programmer -
pardon: scripter.
One day I'll tell ya what's the difference. But that day is not today.