Dynamic Javascript Execution
In : Uncategorized, Posted by admin on Mar.03, 2009
A lot of time when you do an AJAX call, you want to dynamic execute the javascript within the AJAX updated region. For example:
<div id="ajax_update_here">
<!-- this is ajax return result -->
<script>
alert("this is a dynamic message from the ajax backend");
</script>
<!-- end of ajax return result -->
</div>
http://kratcode.wordpress.com/tag/javascript/ is a description of how to do it. Just call: execJS(‘ajax_update_here’); and it will execute the javascript within the div block.
function execJS(node)
{
var bSaf = (navigator.userAgent.indexOf('Safari') != -1);
var bOpera = (navigator.userAgent.indexOf('Opera') != -1);
var bMoz = (navigator.appName == 'Netscape');
if (!node) return;
/* IE wants it uppercase */
var st = node.getElementsByTagName('SCRIPT');
var strExec;
for(var i=0;i<st.length; i++)
{
if (bSaf) {
strExec = st[i].innerHTML;
st[i].innerHTML = "";
} else if (bOpera) {
strExec = st[i].text;
st[i].text = "";
} else if (bMoz) {
strExec = st[i].textContent;
st[i].textContent = "";
} else {
strExec = st[i].text;
st[i].text = "";
}
try {
var x = document.createElement("script");
x.type = "text/javascript";
/* In IE we must use .text! */
if ((bSaf) || (bOpera) || (bMoz))
x.innerHTML = strExec;
else x.text = strExec;
document.getElementsByTagName("head")[0].appendChild(x);
} catch(e) {
alert(e);
}
}
};
Tags : javascript
Add New Comment
Thanks. Your comment is awaiting approval by a moderator.
Do you already have an account? Log in and claim this comment.
Add New Comment