Using Ajax Request Generated JavaScript

by Andrew De Ponte on July 1st, 2009

Hi all. Recently I was working on a web application that utilized JavaScript and AJAX. Who would have guessed. Anyways, I ran into a bit of a snag when I had the AJAX requests result defining a JavaScript snippet. The problem was that AJAX requests strip out script tags and don’t let them get inserted into the DOM tree. I am not sure why this is the case, maybe for some security reason that I have yet to see. Anyways, this creates a bit of a problem when you are trying to do things like use a JavaScript calendar widget inside the AJAX result set when the calendar widget needs to be initialized by some JavaScript code that is unique to that calendar.

Despite all my efforts in googling I failed to find a clearly defined solution. Bits of the sites have clues but none of them had actual solutions with enough detail to make it reproducible. Hence, after exploring all of these hints and all of the combinations of the hints I managed to find a working solution for using JavaScript that is dynamically generated as part of the result of an AJAX call.

Lets assume that you are using Prototype because I was, and it is a very common AJAX JavaScript library. The first thing I looked to find out was if there was an event fired off to the portion that is generated by AJAX request. The answer is, not that I could find. There are events in the top level document but none which really help you. Since, there are no events or callbacks in the child document (one produced by the ajax call and loaded into the top level) and no script tags carried through to the top level document we first have to figure out a way to get the JavaScript code to the top level document and then secondarily have JavaScript evaluate it.

In order to get the script tags content to the top level document I had to create a div that contained the raw content of the script tag that I wanted available. Beyond that the div had to have a unique ID that was able to be obtained programatically in the top level document using JavaScript. An example of this might look as follows (html.erb template of JavaScript):

  1 <div id=“<%= calendar_id %>_script” style=“display: none;”>
  2 <%= calendar_id %>SelectHandler = function(type, args, obj){
  3     var selected = args[0];
  4     var selDate = selected[0][0] + “-” + selected[0][1] + “-” + selected[0][2];
  5     <% if txt_input_id.nil? %>
  6       document.getElementById(“<%= calendar_namespace %>_<%= calendar_id %>”).value = selDate;
  7         <%= calendar_id %>hide();
  8     <% else %>
  9       document.getElementById(“<%= txt_input_id %>”).value = selDate;
 10         <%= calendar_id %>hide();
 11     <% end %>
 12 }
 13
 14 <%= calendar_id %>show = function (){
 15     var cur_z = $(‘<%= calendar_container %>’).style.zIndex;
 16     if (!cur_z) {
 17       cur_z = 1;
 18     } else {
 19       cur_z = parseInt(cur_z)
 20     }
 21     $(‘<%= calendar_container %>’).show();
 22     $(‘<%= calendar_container %>’).style.zIndex = (cur_z + 1);
 23 }
 24 <%= calendar_id %>hide = function(){
 25     var cur_z = $(‘<%= calendar_container %>’).style.zIndex;
 26     if (!cur_z) {
 27       cur_z = 1;
 28     } else {
 29       cur_z = parseInt(cur_z)
 30     }
 31     $(‘<%= calendar_container %>’).hide();
 32     $(‘<%= calendar_container %>’).style.zIndex = (cur_z - 1);
 33 }
 34
 35 YAHOO.namespace(“<%= calendar_namespace %>.<%= calendar_id %>”);
 36 YAHOO.<%= calendar_namespace %>.<%= calendar_id %>.init = function() {
 37     YAHOO.<%= calendar_namespace %>.<%= calendar_id %>.Cal = new YAHOO.widget.Calendar(“<%= calendar_id %>”,“<%= calendar_container %>”, <%= @opts %>);
 38     YAHOO.<%= calendar_namespace %>.<%= calendar_id %>.Cal.selectEvent.subscribe(<%= calendar_id %>SelectHandler, YAHOO.<%= calendar_namespace %>.<%= calendar_id %>.Cal, true);
 39     YAHOO.<%= calendar_namespace %>.<%= calendar_id %>.Cal.render();
 40 }
 41 </div>

This div with a unique id gives me the capability of pulling out the associated raw JavaScript that is the innerHTML content of this div in the top level document and having the top level documents JavaScript evaluate it. This can be done with a JavaScript function like the following:

  1 function createRowEditCalendar(id) {  
  2   var script = document.getElementById(“cal_visited_on_” + id + “_script”).innerHTML;
  3   eval(script);
  4   var my_init_cmd = “YAHOO.namespace_o_hell.cal_visited_on_” + id + “.init();”;
  5   eval(my_init_cmd);
  6   return true;
  7 }

The next logical question of course is what event or callback launches the above JavaScript function in the top level document. The answer in this case is simply to use the Prototypes Ajax.Updater onComplete callback by having it first programatically figure out the name of the div using the reproducible naming scheme and then call the above JavaScript function passing it the necessary id to access the hidden JavaScript div. This might look something like the following:

  1 <input type=“button” id=“rowedit_create_button” value=“Create” onclick=“new Ajax.Updater(<% if prefix %> ’<%= prefix %>_rowedit_data_rows’ <% else %> ’rowedit_data_rows’ <% end %>, ’<%= urls[:create] %>’, {asynchronous:true, parameters:$(<% if prefix %> ’<%= prefix %>_rowedit_create_form’ <% else %> ’rowedit_create_form’ <% end %>).serialize(), insertion: Insertion.Bottom, onComplete: function() { var drows = document.getElementById(’rowedit_data_rows’); var last_row_id = drows.lastChild.id; last_row_id = last_row_id.replace(’edit_’, ”); last_row_id = last_row_id.replace(’_row’, ”); createRowEditCalendar(last_row_id); } });” />

Assuming that you have those three components in place and properly setup with all the syntax correct you should have a working solution. Note: Be very careful of syntax errors in the JavaScript inside the eval() method because FireBug won’t detect them, or run-time errors in the JavaScript anywhere because not all of them will be detected by FireBug. Anyways, hope this helps someone out there, it sure would have helped me if I had it.

iPhone Application Basic Loading Screen

by Andrew De Ponte on June 21st, 2009

Hi all. If you don’t know already I have been playing with some iPhone app development pretty heavy lately. One of the interesting things which was some what more difficult to find was how to create a loading screens in iPhone apps. After I bit of googling and some reading of different forums I finally narrowed it down. It is handled automatically if you simply created a PNG file called Default.png that is 320 x 240 pixels with a 160 ppi and place it in your projects Resources file. Once in you Resources it will be displayed during the time that the application is loading. If you need to extend the time to get your splash screen to stay on the screen because your code is god like and fast simply tack a sleep or something in your applicationDidFinishLaunching method.

Trac Ticket Creator

by Andrew De Ponte on February 28th, 2009

Hello all. I have been using Trac for many years now and I have always had one issue with. Creation of large numbers of tickets, large numbers being 10 to 20. The interface just doesn’t make it easy. So early this evening I finally decided I was going to do something about it. So, I went to http://trac-hacks.org/wiki/XmlRpcPlugin and checkout out and installed the code on one of our Trac servers. Then, I wrote a simple python wxWidgets script that connects via XML RPC to the Trac environment and provides a much quicker and easier interface for creating large numbers of tickets.

The interface simply provides a milestone dropdown and then a textbox and that is it. Once you have selected a milestone you want the tickets to be associated with (generally a batch of tickets is associated with a milestone), you can type your ticket summary into the text box and simply press enter to create the ticket. Once you press enter and it creates the ticket it will clear the text box so that you can type the next ticket summary in and hit enter to create the next ticket.

Soon enough I will clean this script up and officially release it as on an open source app on the main site. If you are interested in this app before that time, feel free to hit me up.

Welcome

by admin on February 10th, 2009

Hello all and welcome to Offical Blackbridge Software Blog. Here you will find posts by our developers discussing things that they have been working on and feel like sharing, news posts about releases, etc.

Enjoy!