Posts Tagged ‘JavaScript’

One reason for using Flash instead of HTML5

I think, I finally found an answer to the question, why one should use Flash instead of HTML5: Sockets
Update: Nevermind. HTML5 also supports websockets. Darn, is there anything that HTML5 can’t do??

Newer Flash Player versions allow developers to open sockets, which is not possible using pure HTML5/JavaScript in the browser.

Thus, for example, it would not be possible to create a FTP client without Flash or a backend system since such an attempt would require one to open a socket in order to implement the FTP protocol.

XMLHttpRequest and subdomains

If you’re trying to do a XMLHttpRequest in JavaScript but keep failing, this might help you:

First, you need to know two important things:

  1. If you try to call a file using the XMLHttpRequest object in JavaScript, which is located on a different server, relative to the running JavaScript source, you’ll probably get an error.
    The simple reason for this is, that many servers don’t allow crossdomain http-requests.
  2. If both files, the JavaScript source, and the file to parse, are on the same server, using the same domain, you should be safe.

Now, here comes the nasty problem.

Let’s assume, we have a JavaScript file called test.js, which is located at http://www.mydomain.com/test.js.

Further, we have a test file, which we want to load though our Ajax call, which is located at http://www.mydomain.com/test.xml.

Now try to do the Ajax call like this:

var req = new XMLHttpRequest();
req.open("GET", "http://www.mydomain.com/test.xml", false);
req.overrideMimeType("text/plain");
req.setRequestHeader('Content-Type', 'text/plain');
req.onload = function(){
alert("Ajax call was successful");
};
req.send(null);

Open the JavaScript file http://mydomain.com/test.js in your browser.

You’ll notice that this won’t work. Why is that so?

The reason is simply, that we forgot to type “www” in the browser: http://www.mydomain.com/test.js.

Remember that “www” is just a subdomain, nothing more, but enough to change the domain name so that our Ajax call won’t work. In many browsers, you can just type cnn.com instead of www.cnn.com and it’ll work fine as long as you just want to surf the web, but if you’re doing Ajax calls you’ll really have to be careful. Might save you a lot of time and nerves :-)

JavaScript weirdness: “ReferenceError XXX is not defined”

If you’re getting a “ReferenceError XXX is not defined”-error while calling JavaScript functions (which definitive exist), you might want to check for any for each(…) – constructs in your source.
In case you use this way of iterating through a sequence, the browser will disrupt parsing parts of your JavaScript code, which leads to the error message above, if you use a different browser than Firefox.

I was bangin my head for the past 3 hours against my desk just to find out that the for each-expression only exists in Firefox (seems be some proprietary implementation) but not in all other browsers like Safari, Chrome etc…

So, in case you have something like this:

for each (var item in myArray){
   doSomething(item);
}

…you might want to replace it with this:

for (var i=0; i<myArray.length; i++){
   var item = myArray[i];
   doSomething(item);
}

One more thing: This won’t have to be the cause for your problems. Many JavaScript engines simply abort executing your code without any further notification on many kind of errors. The above solution is just one of the many, many cases that can lead to this kinda problems, but since it’s such a nasty one, I thought it would be worth to post it here.

God, this is one of the reasons why I dun really like JavaScript that much.. Every browser manufacturer cooks his own soup..

Tip: If you have troubles running a JavaScript program that runs on Firefox, but not on other browsers, you can try using Google Chrome, which has a quite decent JavaScript debugger built-in. To open just hit alt+cmd+J (on Mac OS).

Flash + Chrome OS = ?

I am a passionate RIA developer, especially if it comes up to using Adobe’s Flex SDK. It’s so easy to use and one can built great web applications in no time.
The best thing I always liked (and still do) is that everything comes “from one hand”. Which means, that the whole API is made by Adobe and most common use-cases are already build-in.
Ever tried to create a div box with rounded corners and shadows just with html and css? Good luck. Using the Flash Builder, it’s just two clicks and you’re done. In my opinion, this makes Flex a development environment which is very consistent and easy to handle, unlike pure JavaScript-solutions that often rely on multiple 3rd-party frameworks.

Since I started coding RIA applications, I was always wondering what would the future look like? Would Flash-based apps conquer the web or will JavaScript (with a little help from Google) be the next step for the evolution of the www?
To be honest, I haven’t really found an answer yet.

As good as Flex can be used to efficiently write rich internet applications, so bad is its reputation on “real” programmers like C/C++, Java developers and even Webengineers that completely focus on pure JavaScript solutions not only cause of Flash’s serious security issues. Adobe seems to have both hands busy with fixing security holes while only a minority of the internet users runs the updaters frequently enough to be safe from a wide range of potential attacks on the flash plugin.

However, the one big advantage of Flash, which would be fast and easy video playback, seems to be melting down since W3C’s announcement that html5 will support real movie embedding and playback. I can already foresee how Youtube will be completely flash-free one day.

So, what if it is not necessary anymore to use Flash to embed a little video snipped on your favourite website? What is the reason for web developers to decide for the Flex framework instead of trying a pure JavaScript solution?

Yesterday, something funny came to my mind. I was thinking about Java and how great it failed to reach the desktop while becoming the standard technology if it comes up to web backends.
I find it funny, if you compare Java and Flash. Both rely on an own virtual machine that makes applications written for these framworks always look and behave the same, no matter which operating system you use.
You could also compare Javascript with C/C++ pretty much the same way. If you want to code applications in C, you have to keep many platform-specific things in mind. It’s pretty much the same with JavaScript. If you finished developing your brand new killer website, noone’s gonna say that it will work well in the Internet Explorer 6, only cause you tested it in Firefox 3.5. Even other “w3c-compatible” browsers like Safari, Opera or Chrome might give you a headache as a webengineer. To avoid these problems, most people rely on frameworks like Dojo, GWT and/or jQuery but I’d lie if I said that these would guarantee a clean and fluent development of web applications without any problems. On top of this, one needs to learn how to handle these frameworks, which is very often a pain in the a**.

Since the release of the sourcecode of Google’s Chrome OS, I can finally see how web applications could start conquering over classic desktop applications and I’m really exited about it.

Just imagine, you’d had to write an app for Chrome OS, like let’s say an online graphic editor like Photoshop, but on the web. Why’d you choose Ajax over Flash? Remember that using the Flex framework will speed up the development process a lot since so many components already work out of the box like menu-bars, for instance.

I hope Flash will do better here than Java did in the desktop world and become the cross-platform development enviroment for Chrome OS but without the drawbacks of Java like a slow loading time and low application performance.

Return top
Original theme by monolab. Modified by Timo Ernst.