君子以厚德载物
有朋自远方来,不宜乐乎。

1、iframe方法

test.php

<html>
<head>
<script>
var url = "http://localhost/ajax/test2.php"
var oldHash = null;
var timer = null;

function getHash()
{
var hash = window.location.hash;
if ((hash.length >= 1) && (hash.charAt(0) == '#'))
{
hash = hash.substring(1);
}

return hash;
}

function sendRequest()
{
var d = document;
var t = d.getElementById('request');
var f = d.getElementById('alienFrame');
f.src = url + "#" + t.value + "<br/>" + new Date();
}

function setDivHtml(v)
{
var d = document;
var dv = d.getElementById('response');
dv.innerHTML = v;
}

function idle()
{
var newHash = getHash();

if (newHash != oldHash)
{
setDivHtml(newHash);
oldHash = newHash;
}

timer = window.setTimeout(idle, 100);
}

window.onload = function()
{
timer = window.setTimeout(idle, 100);
}
</script>
</head>
<body>

请求:<input id="request"/> <input type="button" value="发送" onclick="sendRequest()"/><br/>
回复:<div id="response"></div>

<iframe id="alienFrame" src="http://localhost/ajax/test2.php#894516653215"></iframe>

</body>
</html>

test2.php

<html>
<head>
<script>
var url = "http://localhost/ajax/test.php"
var oldHash = null;
var timer = null;

function getHash()
{
var hash = window.location.hash;
if ((hash.length >= 1) && (hash.charAt(0) == '#'))
{
hash = hash.substring(1);
}

return hash;
}

function sendRequest()
{
var d = document;
var t = d.getElementById('request');
var f = parent;
//alert(f.document); //试着去掉这个注释,你会得到“Access is denied”
f.location.href = url + "#" + t.value + "<br/>" + new Date();
}

function setDivHtml(v)
{
var d = document;
var dv = d.getElementById('response');
dv.innerHTML = v;
}

function idle()
{
var newHash = getHash();

if (newHash != oldHash)
{
setDivHtml(newHash);
oldHash = newHash;
}

timer = window.setTimeout(idle, 100);
}

window.onload = function ()
{
timer = window.setTimeout(idle, 100);
console.log(window.location);
}
</script>
</head>
<body>

请求:<input id="request"/> <input type="button" value="发送" onclick="sendRequest()"/><br/>
回复:<div id="response"></div>

</body>
</html>

 

2、建script方法

JsServer_js.php

<html>
<head>
<script>
var scriptBlock = document.createElement("script");
var a = {};

function StartGet()
{
window.reni_cid = {};

scriptBlock.src = "";
scriptBlock.src = "http://localhost/ajax/JsServer_server.php";
scriptBlock.type = "text/javascript";
scriptBlock.language = "javascript";
document.getElementsByTagName("head")[0].appendChild(scriptBlock);
scriptBlock.onreadystatechange = ReturnData();  //仅IE浏览器有onreadystatechange方法
}
function ReturnData()
{
if("loaded" == scriptBlock.readyState)
{
alert(scriptBlock.readyState);
var div = document.getElementById("htmldiv");
alert(reni_cid);
alert(reni_cid.project);
div.innerHTML = reni_cid.project[0].name1; //a是返回的json里面的变量
}
}

 

function loadContent()
{
var s=document.createElement('SCRIPT');
s.src='http://localhost/ajax/JsServer_server.php';
document.body.appendChild(s);
}

function setDivContent(v)
{
var dv = document.getElementById("dv");
dv.innerHTML = v;
}
</script>
</head>
<body>
<div id="htmldiv"></div>

<input type="button" value="Click Me" onclick="StartGet()">
</body>
</html>

JsServer_server.php

<?php
$arr = array(
b=>"hello",
2=>"world"
);
echo "reni_cid='hello'";
//echo json_encode($arr);
//echo "window.reni_cid = {'project':[{'name1':'a1'},{'name2':'a2'}]};";
?>

//目前这样返回的数据有误,不知道php如何返回数据来更改请求方的js对象值

posted on 2013-07-25 17:34  snowcolin  阅读(319)  评论(0编辑  收藏  举报

期待你的评论交流