ajax返回了一个js引用:
<script src"http://localhost/test.js"></script>
这个js本身显示一段文件。但把这个引用放到页面中时,页面中只存在这段引用字符,不会将js本身应该输出的文字展示出来。该怎样解决这个问题呢?
<script src"http://localhost/test.js"></script>
这个js本身显示一段文件。但把这个引用放到页面中时,页面中只存在这段引用字符,不会将js本身应该输出的文字展示出来。该怎样解决这个问题呢?
document.write('hello world!');我需要在ajax中返回
<script src="http://localhost/test.php"></script>
到页面上,在页面上看到hello world!。能实现吗?
<?php echo "document.write('hello world!');" ?>test.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
function getTest(){
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function(){
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
eval(xmlhttp.responseText);
}
}
xmlhttp.open("GET", "test.php", true);
xmlhttp.send();
}
</script>
</head>
<body>
<button onClick="getTest();">getAjax</button>
</body>
</html>
刚做了个简单测试,似乎不行啊//test.js
document.writeln('test');
//test.html
<script>eval("<script src='test.js'></script>");</script>
页面没有显示test
//如下
<script>eval("alert(1)");</script>
可以提示框1eval对script引用有效吗?还是对script引用的文件有test.js本身有特别的写法要求呢?
document.write('hello world!');
但如果是这样的话,将导致的的页面布局全部消失一旦页面加载完毕,document 对象将关闭文档。其后的任何 write 都将产生新的 document 对象,即写到新窗口去了
只要返回的是 js 代码才可以使用 eval 函数使之生效
那么就是说这样的使用是不可以的咯?
现在遇到的问题是,这个伪js文件直接在html文件中引用是没问题的
比如
<div><sript src="htt://localhost/test.php"></script></div>
但是有一个需求,会通过ajax获取到“<sript src="htt://localhost/test.php?type=2"></script>”这段字符,并通过js修改<div>的innerHtml。成为<div><sript src="htt://localhost/test.php?type=2"></script></div>,此时就呈现不了内容了。
通常在 js 中 使 script 标签生效,需要这样写
document.write('<scr'+'ipt src="htt://localhost/test.php?type=2"></scr' + 'ipt>');
这样的方式用在 ajax 中就不行了,因为在 ajax 应用中是不可以 write的你可以尝试创建一个 iframe 将 js 标记写到其中,再读回 body 的内容
//test.php
<?php
echo "document.write('{$_GET['name']}')";
?>其次有个html页面,这个页面开始显示“你好,1111”,我希望点击后显示“你好,222”
//test.html
<html>
<script>
function test(_name){
document.getElementById('d_name').innerHtml='你好,<script src="http://localhost/test.php?name=222"></script>';
}
</script>
<body>
<div id='d_name'>你好,<script src="http://localhost/test.php?name=111"></script><div>
<a onclick='test(222);'>点我</a>
</body>
</html>但我这样做,结果是div包含了'你好,<script src="http://localhost/test.php?name=222"></script>',却不能显示为"你好,222"