Executing Javascript in loaded pages

2 views
Skip to first unread message

uffa

unread,
Jul 17, 2008, 9:59:34 AM7/17/08
to CiUI
I've been working with an implementation of CIUI for an iphone app.
The subpages that are being loaded by CIUI contain some javascript in
the page body. However, this javascript doesn't seem to get executed,
due to the way CIUI loads the page. Is there another way to execute
javascript from the pages being loaded with CIUI?

Any info is appreciated

Vladimir Olexa

unread,
Jul 17, 2008, 3:13:17 PM7/17/08
to CiUI
that's an interesting feature request, definitely something worth
implementing.

can you go ahead and open a bug in google code for this?

also, you're more than welcome to add this feature yourself ;) i'd be
happy to include it with the next release...

uffa

unread,
Jul 20, 2008, 3:16:35 PM7/20/08
to CiUI
I re-worked some code that seems to do it:

BEGIN-CODE

/*
// PASS IN AN HTML NODE, THIS WILL EXECUTE THE JAVASCRIPT IN THE
INNERHTML
*/

var js_ScriptFragment = '(?:<script.*?>)((\n|\r|.)*?)(?:<\/script>)';

// doesn't work for external javascript files, ie. <script src="http://
www....">
function js_updateElement(element) // PASS IN AN HTML NODE
{
var content = element.innerHTML;
setTimeout(function() {js_evalScripts(content)}, 10);
}


function js_extractScripts(str)
{
var matchAll = new RegExp(js_ScriptFragment, 'img');
var matchOne = new RegExp(js_ScriptFragment, 'im');

var arr = str.match(matchAll) || [];
var res = [];

for (var i = 0; i < arr.length; i++) res.push((arr[i].match(matchOne)
|| ['', ''])[1]);
return res;
}

function js_evalScripts(str)
{
var arr = js_extractScripts(str);
for (var i = 0; i < arr.length; i++) eval(arr[i]);
}

END-CODE

then you can integrate it into your existing function,
updatePageContent....

pages[currentPage].innerHTML = content;
js_updateElement(pages[currentPage]); // executes the javascript on
the page
pages[-currentPage].innerHTML = "";

Vladimir Olexa

unread,
Jul 31, 2008, 4:04:04 AM7/31/08
to CiUI
thanks for this. i haven't looked at it yet but will do so shortly and
see how we can integrate it into the framework.
Reply all
Reply to author
Forward
0 new messages