JS non puo` includere pagine o frammenti HTML.
Diciamo che in presenza di un browser che supporta una richiesta XMLHttp e se la necessità è includere pagine locali allo script non è vero:
Pagina contenitore:
codice:
<html>
<title>Include lato client</title>
<script type="text/javascript" src="include.js"></script>
</head>
<body>
<script type="text/javascript">
document.write('<div id="header"></div>')
include("header.htm");
</script>
<hr>Corpo<hr>
<script type="text/javascript">
document.write('<div id="footer"></div>')
include("footer.htm");
</script>
</body>
</html>
Header:
codice:
<table width="100%" border="0" cellspacing="1" cellpadding="1">
<tr>
<td>header è bello</td>
</tr>
</table>
Footer:
codice:
<table width="100%" border="0" cellspacing="1" cellpadding="1">
<tr>
<td bgcolor="#FF0000">footer</td>
</tr>
</table>
Script:
codice:
function include(xUrl){
var root = document.documentElement;
var head = root.firstChild;
var elId = (xUrl).replace(".htm", "")
if (head.nextSibling.nodeType == 3){
var body = head.nextSibling.nextSibling;
} else {
var body = head.nextSibling;
}
var xmlhttp=false;
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
xmlhttp = new XMLHttpRequest();
}
xmlhttp.open("GET", xUrl,true);
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4) {
var nel = document.createElement("DIV");
var el = document.getElementById(elId);
el.appendChild(nel)
nel.innerHTML = xmlhttp.responseText;
}
}
xmlhttp.send(null)
}
Ciao.