Vik*_*Rao 3 javascript function
嗨,我不知道javascript,但我需要在我的一个模板中使用它.我在Google上搜索,找到了一个像这样的解决方案:
我的Index.php文件.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test</title>
<script type="text/javascript">
window.onload = function(){
document.getElementById("aside").innerHTML="<img src='loadingImage.gif'>";
if(XMLHttpRequest) var x = new XMLHttpRequest();
else var x = new ActiveXObject("Microsoft.XMLHTTP");
x.open("GET", "other_content_1.php", true);
x.send("");
x.onreadystatechange = function(){
if(x.readyState == 4){
if(x.status==200) document.getElementById("aside").innerHTML = x.responseText;
else document.getElementById("aside").innerHTML = "Error loading document";
}
}
}
</script>
</head>
<body>
<div id="aside">This is aside</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我的other_content_1.php文件
<div id='other-content-1'>
<?php echo 'This text is loading via php command'; ?>
</div>
Run Code Online (Sandbox Code Playgroud)
由于这是我网站中唯一的Javascript函数,我认为没有理由为它加载额外的JQuery.我确信必须有一种方法可以使用普通的JavaScript/ajax来实现这一点,而无需加载JQuery.任何人都可以建议正确的语法吗?
我希望在页面继续加载时异步发生.
来自http://www.tutorialspoint.com/ajax/,使用普通XHR(XmlHttpRequest)
function loadPage() {
var ajaxRequest;
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
}catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
}catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
}catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var ajaxDisplay = document.getElementById('ajaxDiv');
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
ajaxRequest.open("GET", "ajax-example.php", true);
ajaxRequest.send(null);
}
loadPage();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5721 次 |
| 最近记录: |