473,398 Members | 2,188 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

My First (Relatively) large Javascript Project

While I've done javascript work for as long as I can remember (since
Netscape first released it), I've only ever used it for trivial
things, change a color here, validate a text element there, blah blah
blah. With Ajax (actually, the uncovering of the XmlHTTPRequest
object) I absolutely see the benefit of moving more of the UI work to
the client, rather than doing page refreshes.

I know there are a bunch of libraries out there (prototype, DOJO,
etc.) but I wanted to write one that specifically dealt just with Ajax
requests, and handling the responses (i.e. parsing the DOM tree of an
XML response or the instantiation of an object for a JSON response,
etc.). As I progressed I added logging capabilities and a few touches
here and there.

The biggest part of the challenge was trying to wrap myself around the
Object vs Prototype style in javascript. I still only know 1/100th of
what I need to, but I seem to have been able to write a relatively
self contained little package that only exposes what I wanted to and
not expose what was 100% internal. Writing classes (or objects or
whatever the JS community calls them) is completely foreign to me. I
am only use to writing some inline functions that are called by
various elements on the page.

So...I put up a dumb little test page at http://www.lamatek.com/Ajax/
and was looking for constructive criticism. Not of the test page, but
of the javascript code. Is my encapsulation okay...that sort of thing.
It all appears to work as I intended, but because of my lack of
knowedge & experience it would be nice to get your opinions.

Thanks in advance.

Feb 27 '07 #1
10 2642
"Tom Cole" <tc****@gmail.comwrote in message
news:11**********************@t69g2000cwt.googlegr oups.com...
While I've done javascript work for as long as I can remember (since
Netscape first released it), I've only ever used it for trivial
things, change a color here, validate a text element there, blah blah
blah. With Ajax (actually, the uncovering of the XmlHTTPRequest
object) I absolutely see the benefit of moving more of the UI work to
the client, rather than doing page refreshes.

I know there are a bunch of libraries out there (prototype, DOJO,
etc.) but I wanted to write one that specifically dealt just with Ajax
requests, and handling the responses (i.e. parsing the DOM tree of an
XML response or the instantiation of an object for a JSON response,
etc.). As I progressed I added logging capabilities and a few touches
here and there.

The biggest part of the challenge was trying to wrap myself around the
Object vs Prototype style in javascript. I still only know 1/100th of
what I need to, but I seem to have been able to write a relatively
self contained little package that only exposes what I wanted to and
not expose what was 100% internal. Writing classes (or objects or
whatever the JS community calls them) is completely foreign to me. I
am only use to writing some inline functions that are called by
various elements on the page.

So...I put up a dumb little test page at http://www.lamatek.com/Ajax/
and was looking for constructive criticism. Not of the test page, but
of the javascript code. Is my encapsulation okay...that sort of thing.
It all appears to work as I intended, but because of my lack of
knowedge & experience it would be nice to get your opinions.
Well, at least you are not using $() as a wrapper for getElementById().

All in all though, it looks fairly clean. (I am no expert though.) At first glance,
aside from your strange markup-in-comments style, I thought it looked similar to Kruse's
AjaxRequest (AjaxToolbox).

Also, just out of plain nosiness, are you able to publicize the contents of "test.do?"

-Lost
Feb 27 '07 #2
On Feb 27, 7:40 am, "Tom Cole" <tco...@gmail.comwrote:
While I've done javascript work for as long as I can remember (since
Netscape first released it), I've only ever used it for trivial
things, change a color here, validate a text element there, blah blah
blah. With Ajax (actually, the uncovering of the XmlHTTPRequest
object) I absolutely see the benefit of moving more of the UI work to
the client, rather than doing page refreshes.

I know there are a bunch of libraries out there (prototype, DOJO,
etc.) but I wanted to write one that specifically dealt just with Ajax
requests, and handling the responses (i.e. parsing the DOM tree of an
XML response or the instantiation of an object for a JSON response,
etc.). As I progressed I added logging capabilities and a few touches
here and there.

The biggest part of the challenge was trying to wrap myself around the
Object vs Prototype style in javascript. I still only know 1/100th of
what I need to, but I seem to have been able to write a relatively
self contained little package that only exposes what I wanted to and
not expose what was 100% internal. Writing classes (or objects or
whatever the JS community calls them) is completely foreign to me. I
am only use to writing some inline functions that are called by
various elements on the page.

So...I put up a dumb little test page athttp://www.lamatek.com/Ajax/
and was looking for constructive criticism. Not of the test page, but
of the javascript code. Is my encapsulation okay...that sort of thing.
It all appears to work as I intended, but because of my lack of
knowedge & experience it would be nice to get your opinions.

Thanks in advance.
Nice work for a first project.

Couple of points:

Line 105:
/**Attempts to hold browser type. Only works after an AjaxRequest has
been created.*/
var browser = (navigator.appName.indexOf('Microsoft') >= 0);

This puzzles me; you're apparently using a variable named "browser" to
indicate whether you're in an IE environment. This is cryptic; I
can't tell from a given context what the variable "browser" means
without looking up its definition here. Also, this variable, as
you're defining it here, will only work internally within the context
of your AjaxRequest function, so saying it will only work after and
AjaxRequest has been creating is moot - it still isn't in scope
outside your AjaxRequest function.

Line 119ff:
Your use of both style.top and style.bottom is a little strange. For
Drag & Drop you should only need to use top and left; bottom and right
are implicit. Set the height and width using style.height and
style.width; retrieve it using offsetHeight.

Lines 142-144:
No point in assigning dummy functions; leave these blank or assign
them to null.

Lines 241ff:
No need for a try/catch block here; nothing you're doing will throw an
error. Also, try/catch all over the place can be disastrous for
debugging.

265ff:
Use "var" keyword to declare names for the following functions.

365ff:
Ah, the "getRequest" function. First off, you shouldn't be using the
IE-specific conditional comments here or anywhere, for that matter.
All you need is:

var xml;
var _factories = [function() { return new XMLHttprequest(); },
function() { return new ActiveXObject("Msxml2.XMLHTTP"); },
function() { return new ActiveXObject("Microsoft.XMLHTTP"); }];

for (var i=0; i<factories.length; i++) {
try {
xml = _factories[i]();
break;
} catch(e) {
continue;
}
}

370ff:
Your "parseResponse" functions are all so similar, you should try to
reuse functionality from them. Something along the lines of:

function makeResponseHandler(fn) {
return function() {
if (xmlhttp.readyState != 4) return;
if (xmlhttp.status == 200) {
try {
value = fn(xmlhttp);
} catch (ex) { // log errors
}
}
}
}

var parseJSONResponse = makeResponseHandler(function(response)
{ return eval(response.responseText); });
var parseTextResponse = makeResponseHandler(function(response)
{ return response.responseText; });
var parseXMLResponse = makeResponseHandler(function(response) { return
response.responseXML; });

etc.

Also, as a general rule, I don't like to set any element.style
properties using JS except for top, left, height, and width; all
others should be specified in a stylesheet and toggled using CSS
classnames. Helps keep your style separate from your behavior.

Anyway, just my 2c. Others may have different preferences. Good job
though!

-David

Feb 27 '07 #3
On Feb 27, 12:15 pm, "-Lost" <missed-s...@comcast.netwrote:
"Tom Cole" <tco...@gmail.comwrote in message

news:11**********************@t69g2000cwt.googlegr oups.com...


While I've done javascript work for as long as I can remember (since
Netscape first released it), I've only ever used it for trivial
things, change a color here, validate a text element there, blah blah
blah. With Ajax (actually, the uncovering of the XmlHTTPRequest
object) I absolutely see the benefit of moving more of the UI work to
the client, rather than doing page refreshes.
I know there are a bunch of libraries out there (prototype, DOJO,
etc.) but I wanted to write one that specifically dealt just with Ajax
requests, and handling the responses (i.e. parsing the DOM tree of an
XML response or the instantiation of an object for a JSON response,
etc.). As I progressed I added logging capabilities and a few touches
here and there.
The biggest part of the challenge was trying to wrap myself around the
Object vs Prototype style in javascript. I still only know 1/100th of
what I need to, but I seem to have been able to write a relatively
self contained little package that only exposes what I wanted to and
not expose what was 100% internal. Writing classes (or objects or
whatever the JS community calls them) is completely foreign to me. I
am only use to writing some inline functions that are called by
various elements on the page.
So...I put up a dumb little test page athttp://www.lamatek.com/Ajax/
and was looking for constructive criticism. Not of the test page, but
of the javascript code. Is my encapsulation okay...that sort of thing.
It all appears to work as I intended, but because of my lack of
knowedge & experience it would be nice to get your opinions.

Well, at least you are not using $() as a wrapper for getElementById().

All in all though, it looks fairly clean. (I am no expert though.) At first glance,
aside from your strange markup-in-comments style, I thought it looked similar to Kruse's
AjaxRequest (AjaxToolbox).

Also, just out of plain nosiness, are you able to publicize the contents of "test.do?"

-Lost- Hide quoted text -

- Show quoted text -
Where is Kruse's AjaxRequest? Can you give me a link to it? Maybe I've
just beat an Old Horse to death (I'm sure I have, but at least I
learned something while I was shipping him :)

Sure, the test.do is just a simple servlet that dishes out test data.
Here it is (sorry if the format is messed up, I use Google Groups so
who knows...):

<pre>
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Date;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Test extends HttpServlet {

public void doPost(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
String xml = request.getParameter("type");
if (xml == null) {
BufferedWriter output = new BufferedWriter(new
OutputStreamWriter(response.getOutputStream()));
output.write("This is a null test.");
output.flush();
output.close();
}
else if (xml.equalsIgnoreCase("text")) {
BufferedWriter output = new BufferedWriter(new
OutputStreamWriter(response.getOutputStream()));
output.write("This is only a test.");
output.flush();
output.close();
}
else if (xml.equalsIgnoreCase("xml")){
response.setContentType("text/xml");
response.setHeader("Expires", (new Date()).toGMTString());
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Pragma", "no-cache");
BufferedWriter output = new BufferedWriter(response.getWriter());
String value = "<?xml version=\"1.0\" encoding=\"UTF-8\"?
><request>";
value += "<date>" + (new Date()).toGMTString() + "</date>";
Enumeration headers = request.getHeaderNames();
while (headers.hasMoreElements()) {
String header = (String) headers.nextElement();
value += "<header><name>" + header + "</name><value>" +
request.getHeader(header) + "</value></header>";
}
value += "</request>";
output.write(value);
output.flush();
output.close();
}
else if (xml.equalsIgnoreCase("json")) {
response.setContentType("text/x-json");
response.setHeader("Expires", (new Date()).toGMTString());
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Pragma", "no-cache");
BufferedWriter output = new BufferedWriter(response.getWriter());
String value = "{ \"header\": { ";
value += "\"date\": \"" + (new Date()).toGMTString() + "\", ";
Enumeration headers = request.getHeaderNames();
value += "\"headers\": [ ";
int count = 0;
while (headers.hasMoreElements()) {
String header = (String) headers.nextElement();
if (count 0)
value += ", ";
value += "{ \"name\": \"" + header + "\", \"value\": \"" +
request.getHeader(header) + "\" }";
count++;
}
value += "] } }";
output.write(value);
output.flush();
output.close();
}
else {
BufferedWriter output = new BufferedWriter(new
OutputStreamWriter(response.getOutputStream()));
output.write("This servlet does not support requests of type " +
xml);
output.flush();
output.close();
}
}

public void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
doPost(request, response);
}
}
</pre>
Feb 27 '07 #4
On Feb 27, 12:52 pm, "David Golightly" <davig...@gmail.comwrote:
On Feb 27, 7:40 am, "Tom Cole" <tco...@gmail.comwrote:


While I've done javascript work for as long as I can remember (since
Netscape first released it), I've only ever used it for trivial
things, change a color here, validate a text element there, blah blah
blah. With Ajax (actually, the uncovering of the XmlHTTPRequest
object) I absolutely see the benefit of moving more of the UI work to
the client, rather than doing page refreshes.
I know there are a bunch of libraries out there (prototype, DOJO,
etc.) but I wanted to write one that specifically dealt just with Ajax
requests, and handling the responses (i.e. parsing the DOM tree of an
XML response or the instantiation of an object for a JSON response,
etc.). As I progressed I added logging capabilities and a few touches
here and there.
The biggest part of the challenge was trying to wrap myself around the
Object vs Prototype style in javascript. I still only know 1/100th of
what I need to, but I seem to have been able to write a relatively
self contained little package that only exposes what I wanted to and
not expose what was 100% internal. Writing classes (or objects or
whatever the JS community calls them) is completely foreign to me. I
am only use to writing some inline functions that are called by
various elements on the page.
So...I put up a dumb little test page athttp://www.lamatek.com/Ajax/
and was looking for constructive criticism. Not of the test page, but
of the javascript code. Is my encapsulation okay...that sort of thing.
It all appears to work as I intended, but because of my lack of
knowedge & experience it would be nice to get your opinions.
Thanks in advance.

Nice work for a first project.

Couple of points:

Line 105:
/**Attempts to hold browser type. Only works after an AjaxRequest has
been created.*/
var browser = (navigator.appName.indexOf('Microsoft') >= 0);

This puzzles me; you're apparently using a variable named "browser" to
indicate whether you're in an IE environment. This is cryptic; I
can't tell from a given context what the variable "browser" means
without looking up its definition here. Also, this variable, as
you're defining it here, will only work internally within the context
of your AjaxRequest function, so saying it will only work after and
AjaxRequest has been creating is moot - it still isn't in scope
outside your AjaxRequest function.
Yeah, I just changed this today because I needed to know the browser
type before a request was made in case the browser logging window was
displayed. This way I would know what to set the style.position
attribute to (IE6- doesn't support position: fixed). I'll change that
comment right away.
>
Line 119ff:
Your use of both style.top and style.bottom is a little strange. For
Drag & Drop you should only need to use top and left; bottom and right
are implicit. Set the height and width using style.height and
style.width; retrieve it using offsetHeight.
I forgot to mention that I did not write the Drag section. It was
something I found last minute because I wanted to display my log in a
floating div, rather than a separate window (as I originally wrote
it). I found that script at http://www.dynamicdrive.com/dynamicindex11/domdrag/.
>
Lines 142-144:
No point in assigning dummy functions; leave these blank or assign
them to null.
See above comment.
>
Lines 241ff:
No need for a try/catch block here; nothing you're doing will throw an
error. Also, try/catch all over the place can be disastrous for
debugging.
Again see above. I did add all the try catch blocks between line 115
and 251 as I was originally having a problem with some cross-browser
functionality. I will remove them now.
>
265ff:
Use "var" keyword to declare names for the following functions.
One question here. By using var prefix, it won't change the scope or
anything? I don't want these functions (methods) called by/available
to anything but an AjaxRequest.
365ff:
Ah, the "getRequest" function. First off, you shouldn't be using the
IE-specific conditional comments here or anywhere, for that matter.
All you need is:

var xml;
var _factories = [function() { return new XMLHttprequest(); },
function() { return new ActiveXObject("Msxml2.XMLHTTP"); },
function() { return new ActiveXObject("Microsoft.XMLHTTP"); }];

for (var i=0; i<factories.length; i++) {
try {
xml = _factories[i]();
break;
} catch(e) {
continue;
}

}
I will investigate this.
>
370ff:
Your "parseResponse" functions are all so similar, you should try to
reuse functionality from them. Something along the lines of:

function makeResponseHandler(fn) {
return function() {
if (xmlhttp.readyState != 4) return;
if (xmlhttp.status == 200) {
try {
value = fn(xmlhttp);
} catch (ex) { // log errors
}
}
}

}

var parseJSONResponse = makeResponseHandler(function(response)
{ return eval(response.responseText); });
var parseTextResponse = makeResponseHandler(function(response)
{ return response.responseText; });
var parseXMLResponse = makeResponseHandler(function(response) { return
response.responseXML; });

etc.
This is a great idea. I wrote them one at a time, text first, then XML
and lastly JSON. This would be a definite first place to look to
reduce LOC.
>
Also, as a general rule, I don't like to set any element.style
properties using JS except for top, left, height, and width; all
others should be specified in a stylesheet and toggled using CSS
classnames. Helps keep your style separate from your behavior.
I didn't want the end user to have to do any special additions other
than importing the library and start making requests.
>
Anyway, just my 2c. Others may have different preferences. Good job
though!

-David- Hide quoted text -

- Show quoted text -

Feb 27 '07 #5
On Feb 27, 12:53 pm, "Tom Cole" <tco...@gmail.comwrote:
On Feb 27, 12:15 pm, "-Lost" <missed-s...@comcast.netwrote:


"Tom Cole" <tco...@gmail.comwrote in message
news:11**********************@t69g2000cwt.googlegr oups.com...
While I've done javascript work for as long as I can remember (since
Netscape first released it), I've only ever used it for trivial
things, change a color here, validate a text element there, blah blah
blah. With Ajax (actually, the uncovering of the XmlHTTPRequest
object) I absolutely see the benefit of moving more of the UI work to
the client, rather than doing page refreshes.
I know there are a bunch of libraries out there (prototype, DOJO,
etc.) but I wanted to write one that specifically dealt just with Ajax
requests, and handling the responses (i.e. parsing the DOM tree of an
XML response or the instantiation of an object for a JSON response,
etc.). As I progressed I added logging capabilities and a few touches
here and there.
The biggest part of the challenge was trying to wrap myself around the
Object vs Prototype style in javascript. I still only know 1/100th of
what I need to, but I seem to have been able to write a relatively
self contained little package that only exposes what I wanted to and
not expose what was 100% internal. Writing classes (or objects or
whatever the JS community calls them) is completely foreign to me. I
am only use to writing some inline functions that are called by
various elements on the page.
So...I put up a dumb little test page athttp://www.lamatek.com/Ajax/
and was looking for constructive criticism. Not of the test page, but
of the javascript code. Is my encapsulation okay...that sort of thing.
It all appears to work as I intended, but because of my lack of
knowedge & experience it would be nice to get your opinions.
Well, at least you are not using $() as a wrapper for getElementById().
All in all though, it looks fairly clean. (I am no expert though.) At first glance,
aside from your strange markup-in-comments style, I thought it looked similar to Kruse's
AjaxRequest (AjaxToolbox).
Also, just out of plain nosiness, are you able to publicize the contents of "test.do?"
-Lost- Hide quoted text -
- Show quoted text -

Where is Kruse's AjaxRequest? Can you give me a link to it? Maybe I've
just beat an Old Horse to death (I'm sure I have, but at least I
learned something while I was shipping him :)
Found it at: http://www.ajaxtoolbox.com/request/source.php. There are
a LOT of things there that I like. There are things in mine I like to
that I don't see there but..... But I like how he keeps an array of
all active requests, the automatic appending of unique-ids to prevent
caching, timeouts, grouping requests, etc. Seems WAY more industrial
that what I have working. I'll have to review the code a bit and see
what changes I want to make. Very,very nice, though.
>
Sure, the test.do is just a simple servlet that dishes out test data.
Here it is (sorry if the format is messed up, I use Google Groups so
who knows...):

<pre>
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Date;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Test extends HttpServlet {

public void doPost(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
String xml = request.getParameter("type");
if (xml == null) {
BufferedWriter output = new BufferedWriter(new
OutputStreamWriter(response.getOutputStream()));
output.write("This is a null test.");
output.flush();
output.close();
}
else if (xml.equalsIgnoreCase("text")) {
BufferedWriter output = new BufferedWriter(new
OutputStreamWriter(response.getOutputStream()));
output.write("This is only a test.");
output.flush();
output.close();
}
else if (xml.equalsIgnoreCase("xml")){
response.setContentType("text/xml");
response.setHeader("Expires", (new Date()).toGMTString());
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Pragma", "no-cache");
BufferedWriter output = new BufferedWriter(response.getWriter());
String value = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><request>";

value += "<date>" + (new Date()).toGMTString() + "</date>";
Enumeration headers = request.getHeaderNames();
while (headers.hasMoreElements()) {
String header = (String) headers.nextElement();
value += "<header><name>" + header + "</name><value>" +
request.getHeader(header) + "</value></header>";
}
value += "</request>";
output.write(value);
output.flush();
output.close();
}
else if (xml.equalsIgnoreCase("json")) {
response.setContentType("text/x-json");
response.setHeader("Expires", (new Date()).toGMTString());
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Pragma", "no-cache");
BufferedWriter output = new BufferedWriter(response.getWriter());
String value = "{ \"header\": { ";
value += "\"date\": \"" + (new Date()).toGMTString() + "\", ";
Enumeration headers = request.getHeaderNames();
value += "\"headers\": [ ";
int count = 0;
while (headers.hasMoreElements()) {
String header = (String) headers.nextElement();
if (count 0)
value += ", ";
value += "{ \"name\": \"" + header + "\", \"value\": \"" +
request.getHeader(header) + "\" }";
count++;
}
value += "] } }";
output.write(value);
output.flush();
output.close();
}
else {
BufferedWriter output = new BufferedWriter(new
OutputStreamWriter(response.getOutputStream()));
output.write("This servlet does not support requests of type " +
xml);
output.flush();
output.close();
}
}

public void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
doPost(request, response);
}}

</pre>- Hide quoted text -

- Show quoted text -

Feb 27 '07 #6
On Feb 27, 12:15 pm, "-Lost" <missed-s...@comcast.netwrote:
"Tom Cole" <tco...@gmail.comwrote in message

news:11**********************@t69g2000cwt.googlegr oups.com...


While I've done javascript work for as long as I can remember (since
Netscape first released it), I've only ever used it for trivial
things, change a color here, validate a text element there, blah blah
blah. With Ajax (actually, the uncovering of the XmlHTTPRequest
object) I absolutely see the benefit of moving more of the UI work to
the client, rather than doing page refreshes.
I know there are a bunch of libraries out there (prototype, DOJO,
etc.) but I wanted to write one that specifically dealt just with Ajax
requests, and handling the responses (i.e. parsing the DOM tree of an
XML response or the instantiation of an object for a JSON response,
etc.). As I progressed I added logging capabilities and a few touches
here and there.
The biggest part of the challenge was trying to wrap myself around the
Object vs Prototype style in javascript. I still only know 1/100th of
what I need to, but I seem to have been able to write a relatively
self contained little package that only exposes what I wanted to and
not expose what was 100% internal. Writing classes (or objects or
whatever the JS community calls them) is completely foreign to me. I
am only use to writing some inline functions that are called by
various elements on the page.
So...I put up a dumb little test page athttp://www.lamatek.com/Ajax/
and was looking for constructive criticism. Not of the test page, but
of the javascript code. Is my encapsulation okay...that sort of thing.
It all appears to work as I intended, but because of my lack of
knowedge & experience it would be nice to get your opinions.

Well, at least you are not using $() as a wrapper for getElementById().

All in all though, it looks fairly clean. (I am no expert though.) At first glance,
aside from your strange markup-in-comments style, I thought it looked similar to Kruse's
AjaxRequest (AjaxToolbox).
The strange markup in the comments is to facilitate a "javadocs" style
tag document generator. Makes it's simpler to provide consistent
docs... plus it's similar to what I'm used to with Java classes.
>
Also, just out of plain nosiness, are you able to publicize the contents of "test.do?"

-Lost- Hide quoted text -

- Show quoted text -

Feb 27 '07 #7
David Golightly wrote:
Also, as a general rule, I don't like to set any element.style
properties using JS except for top, left, height, and width; all
others should be specified in a stylesheet and toggled using CSS
classnames. Helps keep your style separate from your behavior.
I agree for things which are truly style-related. For example, don't set
font-size or things like that.

But for positions, display, visibility, z-index, all those kinds of things,
it doesn't make sense to use a class. Plus, using a class locks you into a
certain set of properties rather than being able to tweak just one that you
want to adjust. Further, swapping a class is often slower than simply
applying a specific style.

So, as a general rule, I would say there is no general rule ;)

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Feb 27 '07 #8
On Feb 27, 10:57 am, "Matt Kruse" <newsgro...@mattkruse.comwrote:
David Golightly wrote:
Also, as a general rule, I don't like to set any element.style
properties using JS except for top, left, height, and width; all
others should be specified in a stylesheet and toggled using CSS
classnames. Helps keep your style separate from your behavior.

I agree for things which are truly style-related. For example, don't set
font-size or things like that.

But for positions, display, visibility, z-index, all those kinds of things,
it doesn't make sense to use a class. Plus, using a class locks you into a
certain set of properties rather than being able to tweak just one that you
want to adjust. Further, swapping a class is often slower than simply
applying a specific style.

So, as a general rule, I would say there is no general rule ;)

--
Matt Krusehttp://www.JavascriptToolbox.comhttp://www.AjaxToolbox.com
Right, specifically, I'll change layout dynamically, but leave style
(fonts, colors, etc.) in a stylesheet class. That's the gist of what
I was trying to say there. Usually, you're not changing a font to
track with a mousemove event, so speed is usually not as much a
concern with style as it is with layout. And if you're working on a
larger project where you leave the CSS to a designer, they're going to
want to have control over fonts and colors without having to find
where you're setting that in a JS file. Code like:

var el = document.createElement('div');
el.style.font = '10pt verdana arial sans-serif';
el.style.color = '#084';
el.style.backgroundColor = '#fce';
el.style.border = '1px solid #08c';
el.style.margin = '10px';

etc. is really on the wrong track - you're doing in JS what you should
probably be doing in CSS. It's only when you're dynamically changing
stuff that changing el.style properties is appropriate, in my opinion.

-David

Feb 27 '07 #9
On Feb 27, 10:05 am, "Tom Cole" <tco...@gmail.comwrote:
On Feb 27, 12:52 pm, "David Golightly" <davig...@gmail.comwrote:
On Feb 27, 7:40 am, "Tom Cole" <tco...@gmail.comwrote:
/**Attempts to hold browser type. Only works after an AjaxRequest has
been created.*/
var browser = (navigator.appName.indexOf('Microsoft') >= 0);
This puzzles me; you're apparently using a variable named "browser" to
indicate whether you're in an IE environment. This is cryptic; I
can't tell from a given context what the variable "browser" means
without looking up its definition here. Also, this variable, as
you're defining it here, will only work internally within the context
of your AjaxRequest function, so saying it will only work after and
AjaxRequest has been creating is moot - it still isn't in scope
outside your AjaxRequest function.

Yeah, I just changed this today because I needed to know the browser
type before a request was made in case the browser logging window was
displayed. This way I would know what to set the style.position
attribute to (IE6- doesn't support position: fixed). I'll change that
comment right away.
Also, you might name your variable something like "isIE" rather than
"browser". I'm just saying.
265ff:
Use "var" keyword to declare names for the following functions.

One question here. By using var prefix, it won't change the scope or
anything? I don't want these functions (methods) called by/available
to anything but an AjaxRequest.
Actually, by using the "var" prefix, you'll achieve what you were
trying to do in the first place, which is give these variables local
scope. When you declare (or first assign to) a token without using
the "var" keyword, it automatically goes into the global scope. Use
"var" to keep it local.
Also, as a general rule, I don't like to set any element.style
properties using JS except for top, left, height, and width; all
others should be specified in a stylesheet and toggled using CSS
classnames. Helps keep your style separate from your behavior.

I didn't want the end user to have to do any special additions other
That's a great goal, but for a larger project/widget you might
consider importing a CSS file from your own domain for this, rather
than duplicating CSS work in JS. (A lot of widget libraries, such as
Yahoo UI and Google Maps, use this model - dynamically adding a "link"
tag to the document to import styles.) Though I understand this is
just a mockup and not for production. It's just something to think
about.

-David

Feb 27 '07 #10
"Tom Cole" <tc****@gmail.comwrote in message
news:11**********************@k78g2000cwa.googlegr oups.com...

<snipped gobs of Java>

Thanks!

-Lost
Feb 27 '07 #11

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

Similar topics

1
by: lawrence | last post by:
This PHP function prints out a bunch of Javascript (as you can see). This is all part of the open source weblog software of PDS (www.publicdomainsoftware.org). We had this javascript stuff...
6
by: Fungii | last post by:
Hello, I have a stylesheet that sets p:first-letter to a certain size and colour. I was playing around with Javascript to change paragraph stylesheets using an array like this: var paras =...
3
by: uday.das | last post by:
hi , I am not sure but I think it might be to avoid heap fragmentation due to several malloc calls. Are there any strong reason ? What are the other things that should take care when we implement...
1
by: Rolf Welskes | last post by:
Hello, I have a large web which has PartA, PartB, PartC and MainPart. MainPart WebPage01 ... WebPage_n PartA .... PartB WebPageB01 ... WebPageB_m PartC ... So I would like to do the...
4
by: nospam | last post by:
I have been running .NET 2003 for some time now. I have been working on the same C++ project for about a year now. All of a sudden, the first file in the project is being built every time I build...
2
by: SoulScriber | last post by:
This comes to be an IE vs. Firefox issue. What I'm trying to do is 'onchange' of a checkbox have a table display underneath. Firefox works fine (when you click on the 'Check Here if your...
11
by: aaapaul | last post by:
We are planning a greater .NET Application-System (WindowsForms) with Visual Studio 2005. One of my problem is, how to design the objects, that they can be reused. E.g. I have a form (with...
3
by: linn.white | last post by:
Hello. I recently began a javascript project and I was wondering if anyone would be willing to lend me a hand. I am not a very experienced programmer, and I would love someone(or someones) to go...
1
by: puzzlecracker | last post by:
Hello Group, Say I have classes A and B and the both need to maintain a pointer/ reference to each for various reasons, say optimization. And there are other classes in the project use these...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.