On Thu, Dec 1, 2011 at 11:49 AM, Nate Hill <[log in to unmask]> wrote:
> As I was struggling with the syntax trying to figure out how to use
> javascript to load a .txt file, process it and then spit out some html on a
> web page, I suddenly found myself asking why I was trying to do it with
> javascript rather than PHP.
>
> Is there a right/wrong or better/worse approach for doing something like
> that? Why would I want to choose one approach rather then the other?
>
I tend to try to do most stuff server-side. Javascript I try to keep
just to enhance the GUI system and perhaps do some AJAXy stuff. There
is the fact that if you're using an external API that's not crucial
you might want to just do it javascript side. So think about cover
images in a catalog for example.
You could have the server-side script go out, grab the image, put it
in a local cache, then prepare the link within the actual html. But
if something goes wrong, you might either take really long to return
that page or never return it.
The approach that most folks do is that they have some javascript that
does an AJAX call. So the page loads on the client and then when the
image comes back the cover image will be added. If it never happens,
you've sent the page at least.
I know some who tend to always go to javascript because they're used
to not having control of the underlying system except for to add html
to templates and sneak in javascript that way.
However, that's awkward, difficult to maintain, error-prone, and
likely horrible for accessibility. If you control the underlying
PHP....then yeah, do it on the PHP side ;).
My advice here is somewhat simplistic and general.
You do have my curiosity up now though. What was you goal with trying
to load that text file?
Jon Gorman
|