Tag Archive for 'prototype'

Microsoft JScript runtime error: Object doesn’t support this property or method.

Yesterday I spent two hours analyzing this issue, which of course I had only with Internet Explorer…

As the background: I wrote a library that would make a table sortable and paginable. I decided to use prototype.js and script.aculo.us. I tried to test the result not only in Firefox which I use by default, but then I simply forgot… Then, when I run the page I got the error:

Microsoft JScript runtime error: Object doesn’t support this property or method.

At the end of the day, (after cutting JavaScript files into slices) I found the reason. All because I created HTML elements like follows (this example uses Builder of script.aculo.us):

$(this._sPaginationSecId).appendChild(Builder.node('span', { class: 'disabled' }, '<'));

If you look at that closer, you will find out that class is not put inside apostrophes (‘). Changing the above snippet to the one below will solve the case:

$(this._sPaginationSecId).appendChild(Builder.node('span', { 'class': 'disabled' }, '<'));

Important!
Be careful with defining properties like href and class with Builder.node() (of script.aculo.us) and new Element() (of prototype).


It’s also possible to define class name of a DOM object created with prototype.js or script.aculo.us using className as a parameter (instead of od 'class'):

$(this._sPaginationSecId).appendChild(Builder.node('span', { className: 'disabled' }, '<'));
 Microsoft JScript runtime error: Object doesnt support this property or method.

JavaScript: what if nested ‘this’ doesn’t work?

Note: Examples used in that post require usage of prototype.js.

Imagine you have a JavaScript class with some features related to the current view. One of the method in that class is supposed to add an observer on all anchors marked with class “delete”. That observer is also defined in this class. In other words, you have a number of “delete” links (similar to one below) and you want to register an event handler that would fire when either of those links is clicked.

The link exmple:

<a href="#" class="delete">delete</a>

Let’s assume the aforementioned methods are defined as follows:

registerDeleteHandlers: function() {
	$$('a.delete').each(function(anchor) {
		anchor.observe('click', this.deleteItem);
	});
},
deleteItem: function(event) {
	...
}

As you would discover the handler wouldn’t be registered… It’s because this inside the inner function (which really registers the observer) is binded to a different context!

Simple solution (smarter)

Define the registerDeleteHandlers function as follows:

registerDeleteHandlers: function() {
	$$('a.delete').each(function(anchor) {
		anchor.observe('click', this.deleteItem);
	}.bind(this));
}

Another way around

Inside the registerDeleteHandlers function you can bind this to a temporary variable and use it in the inner function instead of this. So the registerDeleteHandlers function would look like that:

registerDeleteHandlers: function() {
	var self = this;
	$$('a.delete').each(function(anchor) {
		anchor.observe('click', self.deleteItem);
	});
}

AJAX activity indicator

Users are familiar with indications of work performed in background since first versions of MS Windows. Besides being fancy, they are also informative.

AJAX, a Web 2.0 technique, aim at exchanging only small amounts of data with a server; this should be performed behind the scenes. If so, why not expose the moments when user interaction brings about reqest and response from a server? Remeber my previous post about using prototype.js for making AJAX request? I use prototype also for indicating background actions on web pages that support AJAX.

You’d never guess how easy it is to such indicator.

First, you must register an action which accurs in case of an AJAX-related event. The best way to do that is add the following code in the head section of the HTML code (remember to include prototype.js library before it!):

<script type="text/javascript">
<![CDATA[
Ajax.Responders.register({
    onCreate: function(){ Element.show('spinner')}, 
    onComplete: function(){Element.hide('spinner')}
});
]]>
</script>

Then, further in the code inside body section, add this:

<img alt="spinner" id="spinner" src="gfx/spinner.gif" 
    style="display:none;" />

Actually, it’s all. Whenever you click an object which sends an AJAX request to the server, the indicator defined by img appears and is visible until the response is obtained.

Wonder, how to create an indicator animation? Generate one on ajaxload.info

Using PUT and DELETE methods in AJAX requests with prototype.js

Whenerver I write JavaScript I use prototype.js. I find it brilliant, especially when you deal with AJAX. With this library it’s easy to create an AJAX request and process its response from the server.

To send an AJAX request you create a following HTML code:

<a href="#" id="ahref">a link</a>
...
$('ahref').onclick = function() {
    new Ajax.Request('/some_url', {
        method:'get',	
        parameters:'param1=value',
        onSuccess: function(transport){
            var responseXml = transport.responseXML;
            ...
        },
        onFailure:function(){ alert('Something went wrong...') }
    });
    return false;
}

This way, you create a request with GET and POST method. Recently, while developing a SOA system based on REST (REpresenational State Transfer), I faced a problem of creating requests also with other HTTP methods.

In short, REST allows using a few HTTP methods. The most popular are:

  • GET – for obtaining representation of a resource
  • POST – for updating or creating a representation of a resource
  • PUT – for creating a representation of a resource
  • DELETE – for removing a representation of a resource

Again, prototype.js helps a lot. Since version 1.5, it is possible to use all those methods (formerly, it supported only GET and POST). PUT and DELETE methods are realized by tunneling over POST. Also, “_method=METHOD_NAME” is added as a query parameter with the request. The only thing to do is to prepare relevant logic on the server side to manage the request.

Suppose you have a servlet that allows adding a resource to a database and it is invoked with the following URL:
http://example.com/servlet/RESOURCE_URL.

A simplified definition of a Ajax.Request object in prototype would be as follows:

new Ajax.Request('/some_url', {
    method:'put',
    parameters:'url=RESOURCE_URL'
});

In the JavaServlet, you obtain the request and manage it like that:

@Override
protected void doPost(
    HttpServletRequest req, 
    HttpServletResponse res) 
    throws ServletException, IOException {
    if (req.getParameter("_method") != null 
        && "put".equalsIgnoreCase(req.getParameter("_method"))) {
        doPut(req, res);
    }
}