Showing posts with label Programming. Show all posts
Showing posts with label Programming. Show all posts

Sunday, October 17, 2010

The danger of Javascript

As I use more and more AJAX on my applications, and thus more javascript, I am impressed by the power of the language, but have one really large concern.

With server side programs, one has complete control over the environment, and the client, other than some CSS issues, is not that great an issue.

However, with javascript, the code is being executed by the browser, which means one is at the mercy of a limitless number of interpreters. I have found numerous cases where one browser worked differently than all the others, with real consequences. Such as:

  • IE7 cannot properly create radio buttons with a pure DOM creation of the objects. There is a workaround, but it is bizarre, and I would never have figured it out. Luckily someone else did (You have to add html code the document.createElement call)
  • Safari returns the typeof on an array of objects as a function. All the other browsers I tested return the typeof as 'object'
  • IE7 actually throws an exception if you mispess lightgrey in CSS.
And that was all just last week. I'm sure others have more examples.

So, while javascript really does turn web programming into an interactive environment, it comes at a price. And a question: When do you stop supporting older, non-compliant browsers? IE6 is off my list already, and I would do the same with IE7 - but I have too many users that are trapped on IE7 due to large scale use of terminal servers.

Saturday, September 19, 2009

bluej

I'm taking a new class at Capella for java programming. The class uses an IDE called bluej. Of course, the text explains how to install it on OS X and Windows, but no mention was made of Linux. A little digging found out how to install it on Linux:

First, there are installation instructions for all non-OS X/Windows machine, which involves a straight forward java app instalation:

java -jar ./bluej-252.jar

The app asks for the jdk directory for your system. As a complete java novice, this is where I had to dig a little. For fedora it was /usr/java/jdk[xxx] (where xxx is your version).

Also - one should create a directory for bluej, and the obvious place is /usr/java/bluej

It's trivial, but it would be nice if the install app could figure out the jdk directory for you.

Monday, December 29, 2008

The power of eval

In my programming environment we use PHP and javascript as our primary programming languages. One very handy construct that both of these languages have is the eval statement, which essentially allows a text string to be converted into and executed as code.

An example in PHP:

if(strpos($cToolTip,'$')) {eval("\$cToolTip = \"$cToolTip\";");}

An example in javascript:

eval("day.onclick= function() {goToDate('" + xday + "');};");

In the PHP example, we are checking if the string $cToolTips has any embedded variables (which always begin with a '
So, if we have:

$fubar = 'abc123'
$cToolTip = "Once upon a $fubar night"

After the eval statement, $cToolTip = "Once upon a abc123 night"

The javascript example, while functionally similar, has another very important advantage when programming in js. Lets say you have a loop, and within that loop you are creating a new elements on a page, and are assigning onclick events to these elements. So far, so good.

However, in our example,

eval("day.onclick= function() {goToDate('" + xday + "');};");

Note that we are assigning xday as a paramater in the goToDate function. This is great, except for one thing. If we did not use eval, every time we incremented or changed xday, it would change in every function in which it had been used.

Thus, the eval statement lets us, in effect, turn a variable into a constant so that we do not have to worry about the value of that variable once the eval statement has been run. That can be very handy.