Sie können mich buchen für:
Individuelle Schulungen für XPages, JavaScript und Appcelerator Titanium Software-Entwicklung für IBM XPages, Appcelerator Titanium (Mobile Apps iPhone, iPad, Android, Blackberry 10), Mobile Web und IBM Notes
How to use document.write() in HTML loaded via Ajax (and why regular expressions are that beautiful...)
Julian Buss, January 2nd, 2010 11:59:30
Tags:  Development 
While tinkering around with searchlotus.com I just had the problem that I use document.write() to reformat HTML produced by a Notes view. That works without any problem, until I use Ajax for lazy loading futher data from the view.
In the HTML loaded via Ajax the document.write() does not work, since the browser runs document.write() only during the intial loading of the page.

The view produces HTML like this:



[td]document.write(writetime(2010,0,1,9,30))[/td]



(I use this to get a human friendly representation of datetime values).

I'm loading the first 10 lines of the view directly, and then I load further lines via Ajax like this:


dojo.xhrGet({url:'/web/searchlotus/searchlotus.nsf/(wAlertsWithChannels)?OpenView&restrictToCategory=Blog&start=11&Count=100',
load: function(data) {
      dojo.byId("alertmoreall").innerHTML = data;
}
})


As a result, the document.write() does not work in the HTML I loaded via dojo.xhrGet(). Luckily, we have regular expressions and eval() to the rescue!
I changes my xhrGet call to this:


dojo.xhrGet({url:'/web/searchlotus/searchlotus.nsf/(wAlertsWithChannels)?OpenView&restrictToCategory=Blog&start=11&Count=100',
load: function(data) {
      dojo.byId("alertmoreall").innerHTML = evalLazyHTML(data);
}
})


and then wrote a function like this:


function evalLazyHTML( html ){            
var script,
    regexp = /]*>document\.write\(([\s\S]*?)\);<\/script>/gi,
    s;
while ((script = regexp.exec(html))) {
        html = html.replace(script[0], eval(script[1]));
}
return html;
}


The function is basically very simple: it looks for all "[script]document.write(*)[/script]" tags, and replaces them all with the evaluation of inside javascript code.
Remarkable are the following points:
  • Searching via regular expressions is very fast. Don't be shy to use it many times on your page.
  • When using "regexp.exec()" the resulting variable becomes an array. The array stores at least two elements:
    1) the whole pattern (in my case: "[script]document.write.....[/script]"
    2) the pattern inside brackets (in my case: "writetime(...)")
  • Therefore, I can do a search for script[0] and replace it with an eval(script[1])


Comments (0) | Permanent Link

Comments:
No Comments Found
Add a comment
Subject:
   
Name:
Mail:
Web:
 
Comment:  (No HTML - URLs with leading http://)
 
remember me?   
You can hire me.
See my Linkedin profile for details.

Thanks for reading and have a nice time here!

Please note my Apps for iPhone and iPad: NotesBook: takes your Lotus Notes Notebook (Journal) to your iPhone and iPad xpageswiki.com: huge XPages Tips & Tricks collection for iPhone and iPad