Attach JavaScript loaded as part from an external html page


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE>JavaScript On The Fly</TITLE>
 </HEAD>

 <BODY>

<input type="button" id="dialogOpener"  value="Show Dialog" onClick="handleDialogOpener();" />
<div id="dialog" style="position:absolute; width=200; height=200; left:100px; top:100px">
</div>

<SCRIPT LANGUAGE="JavaScript">
var dynamicConetnts = '<select name="number" onchange="handleNumberSelect(this);"><option value="1">one</option><option value="2">two</option><option value="3">three</option></select><script language="JavaScript">function handleNumberSelect(obj) {alert("selectedIndex = " + obj.selectedIndex);}<\/script>';
</SCRIPT>

<SCRIPT LANGUAGE="JavaScript">
function handleDialogOpener() {

	var scriptInnerText = parseScriptInnerText(dynamicConetnts);

	var head = document.getElementsByTagName("head")[0];
	var scriptElement = createScriptElement(scriptInnerText);
	head.appendChild(scriptElement);

	document.getElementById('dialog').innerHTML = dynamicConetnts;
}

function parseScriptInnerText(scriptString) {
	scriptString = scriptString.substring(scriptString.indexOf("<script"));
	scriptString = scriptString.substring(scriptString.indexOf(">")+1);
	scriptString = scriptString.substring(0, scriptString.indexOf("<\/script>"));
	return scriptString;
}

function createScriptElement(scriptInnerText) {
	var scriptElement = document.createElement('script');
	var is_ie = navigator.userAgent.toLowerCase().indexOf("msie")!=-1?true:false;
	if (is_ie) {
		scriptElement.text = scriptInnerText;
	} else {
		var textNode = document.createTextNode(scriptInnerText);
		scriptElement.appendChild(textNode);
	}
	return scriptElement;
}

</SCRIPT>

 </BODY>
</HTML>

You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.

AddThis Social Bookmark Button

Similar Posts

Comments are closed.