WebAssembly in Action

Author of the book "WebAssembly in Action"
Save 40% with the code: ggallantbl
The book's original source code can be downloaded from the Manning website and GitHub. The GitHub repository includes an updated-code branch that has been adjusted to work with the latest version of Emscripten (currently version 3.1.44).
Showing posts with label XMLHttpRequest. Show all posts
Showing posts with label XMLHttpRequest. Show all posts

Saturday, March 19, 2011

XMLHttpRequest calls when in IE 9 Standards mode

With Internet Explorer 10 (IE 10), the issue described in this article has been fixed if the browser is using IE 10 Standards Mode.

With all versions of Internet Explorer, even when IE becomes more standards compliant, there are usually workarounds that are still needed.

Even though IE 9 is the most standards compliant Microsoft browser to date, there are still several things that have been discovered that we need to be aware of.

Feature detection tests should always test for W3C features first and fall back to IE specific features if the W3C feature is unavailable. For example, the following would be the feature detection code to use to create an XML object from a string of XML:
function ConvertStringToXMLObject(sXML) {
// Test for W3C feature
if (DOMParser) {
var dpDOMParser = new DOMParser();
return dpDOMParser.parseFromString(sXML, "text/xml");
} else { // IE 8 and previous or IE 9 when *not* in Standards mode...
var xdDoc = new ActiveXObject("Microsoft.XMLDOM");
xdDoc.loadXML(sXML);
return xdDoc;
}
}

When Standards mode is on, IE 9 now supports the DOMParser object. The issue with this, however, is that the XMLHttpRequest object still returns an MSXML ActiveX object from the reponseXML property no matter which document compatibility mode IE 9 is using.

The MSXML ActiveX XML object is not compatible with the DOMParser XML object and will result in a 'Type mismatch' error if you try to append the one XML object to the other.

The workaround for this issue is to pass the responseText property, from your XMLHttpRequest object, to your client-side XML object creation function so that you get an XML object of the proper type as in the following example:
function MakeServerSideCall(sURL, sPostData) {
var xhrRequest = GetXMLHttpRequestObject();
xhrRequest.open("POST", sURL, false);
xhrRequest.send(sPostData);

return ConvertStringToXMLObject(xhrRequest.responseText);
}


function GetXMLHttpRequestObject(){
var xhrRequest = null;

if (window.XMLHttpRequest) { xhrRequest = new XMLHttpRequest(); }
else if (window.ActiveXObject) { xhrRequest = new ActiveXObject("Microsoft.XMLHTTP"); }

return xhrRequest;
}

The following is a Microsoft article which talks about the DOMParser object, XMLSerializer object, as well as this workaround for the XMLHttpRequest object:
http://blogs.msdn.com/b/ie/archive/2010/10/15/domparser-and-xmlserializer-in-ie9-beta.aspx

Friday, March 4, 2011

Document Compatibility Modes in Internet Explorer 9

Document compatibility determines how Internet Explorer renders your web page and how the JavaScript will work.

IE 8 and later will first determine how to render a page based on the X-UA-Compatible header which I'll discuss in a moment.

If the X-UA-Compatible header is not present, IE then looks for the DocType directive to determine how to render the page.

If the DocType directive is missing or does not specify a standards-based document type then the web page is rendered in IE 5 mode (Quirks mode of IE 7).

The recommended DocType in IE 9 is the HTML 5 DocType which will cause IE 9 to use Standards mode:
<!DOCTYPE html>

In my reading on the DocType directive I ran across an MSDN article that indicated that IE will also switch on Standards mode if the DocType includes a URI.

In my testing, including the URI in a DocType does appear to cause IE 9 to use Standards mode. Being able to use Standards mode even if the HTML is not HTML 5 allows us to make use of the latest JavaScript functionality in IE 9.

The following is an example of an HTML 4 DocType that causes IE 9 to use Standards mode:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">


If a web site does not work correctly in Standards mode it's recommended that the site be updated to use the most up-to-date features.

There are times, however, when you may want to force a web site to display using a specific document compatibility mode.

An example situation where you may want to force a web site to display using a specific document compatibility mode could be that perhaps you have a product that is live and you don't have time to upgrade it before people start using the latest IE browser.

Even when a web site's JavaScript has been written to do feature detection for W3C compliant features and only fall back to IE specific features if the W3C feature is not available, IE 9 still throws some curve balls.

One curve ball that IE 9 throws is that, even in Standards mode, an MSXML ActiveX object is returned rather than a DOMParser object when making an XMLHttpRequest. The resulting XML document is incompatible with any XML documents that are created using the DOMParser.

The workaround is to return XML as text and turn the responseText value into an XML document object by using the DOMParser (the following MSDN article explains this process a bit more: http://blogs.msdn.com/b/ie/archive/2010/10/15/domparser-and-xmlserializer-in-ie9-beta.aspx).

There are several options available to cause IE to use a document compatibility mode other than IE 9 Standards mode.


Compatibility View button

The first option for changing the document compatibility mode is that an end user can click the Compatibility View button which will cause IE 9 to switch to IE 7 Standards mode.

(click to view the image full size)


The web site is listed in a Compatibility View list until the user removes the web site from the list by either clicking the Compatibility View button again or uses the Compatibility View Settings dialog.

You can find the Compatibility View Settings dialog by pressing Alt on your keyboard to show the menu bar and then select the Tools, Compatibility View settings menu item.

The Compatibility View Settings dialog allows the user to cause all Intranet sites, as well as all web sites, to use the Compatibility View setting by means of two checkboxes at the bottom of the dialog.

(click to view the image full size)

If the Compatibility View Settings checkboxes are checked, and you are viewing one of the respective types of web sites, you will not see the Compatibility View button in the address bar.


IE 9 Developer Tools

A temporary way to change the document compatibility mode of the browser is through the IE 9 Developer Tools.

You can display the Developer Tools by pressing F12.

Changing the selection of the Browser Mode or Document Mode drop-downs causes the web page to refresh and render according to the selected modes:

(click to view the image full size)

This technique is helpful if a developer wants to test how a web page renders in IE 9, IE 8, and IE 7 without having to switch to another computer since you can't install Internet Explorer versions side-by-side.

This saves a lot of time when trying to debug why something suddenly started breaking in IE 9 that didn't break in IE 8 for example.

In my testing so far, these settings appear to cause IE 9 to behave as expected but it's not a replacement to testing with the actual IE 7 and IE 8 browsers.


X-UA-Compatible meta tag

As a web developer you may decide that a page needs to be rendered using a certain document compatibility mode but only certain pages have this requirement.

You can include an X-UA-Compatible meta tag on one or more pages within the web site to tell IE how to render the page.

The meta tag needs to be the first tag in the Head section of a page following only the Title tag as in the following example:
<head>
<title>The Page Title</title>
<meta http-equiv="X-UA-Compatible" content="IE=8" />
</head>

Available values for the X-UA-Compatible setting are:
  • 5 (renders as if IE 7 in Quirks mode which is very similar to how content was rendered in IE 5)
  • 7, 8, and 9 (these ignore the DocType if present and display as if in Standards mode of the browser specified)
  • EmulateIE7, EmulateIE8, EmulateIE9 (these take the DocType into consideration and displays using Standards mode or Quirks mode of the browser specified)
  • Edge (this tells the browser to use the highest mode available and is not recommended in production environments)


The X-UA-Compatible value as an HTTP Response Header

For a small web site simply dropping in the X-UA-Compatible meta tag is not a large task but if you have a web site with hundreds of pages, adding and maintaining the meta tags would be quite the task.

The better approach if you need to include the X-UA-Compatible value for an entire web site is to include the value as an HTTP Response Header


In conclusion

It is recommended that you always try to make sure your web pages work correctly in the latest versions of IE.

There are times, however, when you may need to temporarily tell Internet Explorer to display a page or website in a document mode used by a previous version of the browser which makes being able to specify the document compatibility modes a handy feature to have.

The following is a link to an MSDN article on Defining Document Compatibility if you would like to read more on the subject:http://msdn.microsoft.com/en-us/library/cc288325%28v=vs.85%29.aspx

Friday, August 6, 2010

A Deeper Look at HTML 5 Web Workers


In this post we will have a deeper look at some of the aspects of HTML 5 Web Workers than we did in my previous article 'An Introduction to HTML 5 Web Workers'.

If you are new to HTML 5 Web Workers then I recommend that you read my previous post which details the different types of web workers that are available, how to create the worker objects and how to communicate between the worker and its creator.


Multiple Shared Workers

When you have a web site (or web application) with multiple windows each needing access to a worker thread you don't really want to create a new thread in each window because it takes time and system resources to create each worker thread.

The ability to share a single worker thread among each window from the same origin comes as a great benefit in this case.

The following is the simplest way to create a SharedWorker thread that multiple windows from the same origin can make use of:

// Window 1
var aSharedWorker = new SharedWorker("SharedWorker.js");

// Window 2
var aSharedWorker = new SharedWorker("SharedWorker.js");

The SharedWorker object accepts an optional 2nd parameter in the constructor that serves as the name of the worker.

When you don't specify the 2nd parameter, it's pretty much the same as specifying a name that is exactly the same each time. With that in mind, the above example can be rewritten as follows and will do the exact same thing:

// Window 1
var aSharedWorker = new SharedWorker("SharedWorker.js", "SharedWorkerName");

// Window 2
var aSharedWorker = new SharedWorker("SharedWorker.js", "SharedWorkerName");

Most of the time having one shared worker will give the needed functionality. If you simply have a desire to add more parallel processing, the shared worker can always spawn web workers of its own (dedicated web workers can also spawn additional worker threads if need be).

What if you run into a scenario where you have a need for several windows to share several workers rather than just the one?

That's where the 2nd parameter of the SharedWorker constructor comes into play.

You can create several different SharedWorker threads by specifying different names when creating the worker objects.

The following is an example of two windows each sharing two worker threads 'Worker1' and 'Worker2':

// Window 1 - Shared Worker 1 & 2
var aSharedWorker1 = new SharedWorker("SharedWorker.js", "Worker1");
var aSharedWorker2 = new SharedWorker("SharedWorker.js", "Worker2");

// Window 2 - Shared Worker 1 & 2
var aSharedWorker1 = new SharedWorker("SharedWorker.js", "Worker1");
var aSharedWorker2 = new SharedWorker("SharedWorker.js", "Worker2");

When it comes to giving shared workers a name it's important to be aware that the names are case sensitive.

If you give one worker the name Worker1 and the other worker the name worker1 they may look the same but the case difference with the 'w' causes there to be two worker threads created rather than simply creating and reusing a single worker thread.


Scope of Shared Workers

Something that you may be curious about is what happens when the window that created a shared worker is closed? Is the worker thread still accessible by other windows from the same origin?

A shared worker will remain active as long as one window has a connection to it. For example:

Window 1 creates a shared worker and Window 2 connects to it.

Later Window 1 is closed but, because Window 2 is still connected, the shared worker thread remains even though Window 2 didn't originally create it.



Importing JavaScript into a Worker Thread

Most modern web development now includes several JavaScript files and, in a lot of cases, JavaScript libraries/frameworks.

Fortunately, worker threads have access to a global function called 'importScripts' which allows you to pull the necessary JavaScript files into the worker thread's scope.

The importScripts function allows you to specify one or more JavaScript files to be imported.

If multiple files are specified, they are downloaded in parallel but will be loaded and processed synchronously in the order specified.

Also, the importScripts function does not return until the JavaScript files specified have been loaded and processed.

The following are some examples of how the importScripts function can be used:

// Import just one file
importScripts("file.js");

// Import multiple files with one call (careful of their order if they have
// global variables that depend on other js files being included in the
// request...the JS files will be downloaded in parallel but loaded and
// processed synchronously based on their order here).
//
// Note: I stopped at 3 files to import but you can include any amount

importScripts("file1.js", "file2.js", "file3.js");

Technically, the W3C specification allows for the importScripts function to be used with no parameters but doing so has no effect.


Attaching to 'onmessage' Within a Shared Worker

In my previous post, I had shown the following method of attaching to a shared worker's onmessage event (within the thread):

onconnect = function (evt) {
var port = evt.ports[0];
port.onmessage = function (e) { OnControllerMessage(e, port); }
}

function OnControllerMessage(e, port) {
var sReturnMessage = ("Hello from the sharedworker thread!...This is what you sent my way: " + e.data);

port.postMessage(sReturnMessage);
}

Unfortunately, the above example was accomplished by creating a closure and I'm not a fan of using closures.

I did some digging and found a workaround that avoids the needed for a closures by using the target property of the event object. The target property, in this case, is the port.

The following example demonstrates connecting to the onmessage event and posting data back to the caller via the event's target property:

onconnect = function (evt) {
evt.ports[0].onmessage = OnControllerMessage;
}

function OnControllerMessage(e) {
var sReturnMessage = ("Hello from the sharedworker thread!...This is what you sent my way: " + e.data);

e.target.postMessage(sReturnMessage);
}



Timeouts

Worker threads support timeouts (setTimeout, clearTimeout, setInterval, and clearInterval) which is useful if you only want to process information at set intervals rather than be constantly processing.

One scenario that comes to mind could be if you had a need to check for updates from a 3rd party site and didn't want to constantly hammer the site with requests.

You could set up a timer to check the site every five minutes or so and if new information is available then pass it up to the UI layer so that it can be included in the display information.


The XMLHttpRequest Object

The XMLHttpRequest object can be used in a worker thread and can be used synchronously or asynchronously. It's totally up to you if you use it asynchronously since a worker thread is already separate from the UI thread so waiting for a synchronous return won't hurt the UI's responsiveness.

Something to be aware of about using the XMLHttpRequest object, from within a worker thread, is that the 'responseXML' property will always be null.

The Gecko-specific 'channel' attribute in the XMLHttpRequest instance will also be null when the XMLHttpRequest object is used within a worker thread.

The return data from an XMLHttpRequest, when within a worker thread, will always be in the 'responseText' property as shown in the following example:

var xhrRequest = new XMLHttpRequest();
xhrRequest.open("GET", sURL, false);
xhrRequest.send();

return xhrRequest.responseText;



Error Handling

Unhandled exceptions can be caught within the worker thread by attaching a function to the global 'onerror' event.

There are some browser differences to note here:
  • Chrome 5 and Safari 5 both just pass the error as a string to the error handler in the thread
  • Firefox 3.6.8 and 4.0 beta 2 pass in an ErrorEvent object to the error handler in the thread


The ErrorEvent object's attributes are:
  • message - A human-readable error message
  • filename - The name of the script file in which the error occurred
  • lineno - The line number of the script file on which the error occurred


The following is an example of attaching to the onerror event of a dedicated worker thread (the example will also work for shared workers with the exception that with shared workers postMessage needs to be called on a port):

// Attach to the global error handler of the thread
onerror = OnErrorHandler;

function OnErrorHandler(e) {
// In Chrome 5/Safari 5, 'e' is a string for both dedicated and shared
// workers within the thread

if (typeof (e) == "string"){
postMessage("Error Message: " + e);
} else { // Dedicated worker in Firefox...(Firefox does not yet support
// shared workers)
postMessage("Error Message: " + e.message + " File Name: " + e.filename + " Line Number: " + e.lineno);
}
}

// to test the error handler, throw an error
throw "This is a test error";

You can also attach to an error event of the worker object instance itself so that the creating thread can also have access to the error information.

All browsers (Chrome 5, Safari 5, Firefox 3.6.8 / 4.0 beta 2) implement the dedicated worker instance error event in the same way by passing in the ErrorEvent object.

When it comes to shared workers, however, I have not been able to get the shared worker object instance to trigger the onerror event in Chrome 5 or Safari 5. From my research, it appears that for shared workers the onerror event will only be triggered for the shared worker instance if there was a network error while the worker thread was being created.


The following is an example of attaching to the onerror event of a dedicated worker instance (you set up a shared worker the exact same way):

var aWorker = new Worker("DedicatedWorker.js");
aWorker.onerror = OnErrorMsg;

function OnErrorMsg(e) {
alert("Error Message: " + e.message + " File Name: " + e.filename + " Line Number: " + e.lineno);
}



Closing a Worker Thread

The creator of a worker can close the thread by calling 'terminate' on the worker instance as in the following example:

var aDedicatedWorker = new Worker("DedicatedWorker.js");

...

// We're done with the tread so let the browser release the system
// resources

aDedicatedWorker.terminate();

An alternative to using the terminate method on the worker instance is that the thread has the ability to close itself as in the following example:

// JavaScript of the thread itself

...

close();



In Closing

Between my previous post 'An Introduction to HTML 5 Web Workers' and this one, I hope I have been able to help you in your understanding of web workers so that you can make use of all the possibilities this exciting new technology brings to web development!

If you would like more information on the HTML 5 Web Workers specification you can click on the following links:


New: I have recently written an article for DZone.com which combines the Web Worker information presented in my blog posts and introduces some new information like transferable objects, inline workers, and includes a link to a sample project stored on github.com. The article can be found here: http://refcardz.dzone.com/refcardz/html5-web-workers