微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – PHP / JS / AJAX / HTML:动态创建网页

这是我第一次使用AJAX,我一直在阅读它,这也是我第一次使用js.我想我一直困惑自己.

我正在尝试动态创建一个新的餐馆页面,因此每次管理员点击onclick按钮时,都会创建一个新网页,其中包含我已经创建的新餐厅页面中的内容.

目前我已经按下按钮,成功创建了一个新网页,但是,我不知道如何访问新网页我还希望在创建时显示新创建的网页的链接,例如使用之前.在js中显示我的时钟按钮之前的动态功能.

HTML

<html>
<body>
<button onclick="makePage()">click</button>
<script src="makePage.js">
</script>
</body>
</html>

JS

function makePage(){
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState==4 && xmlhttp.status==200)
    alert("webpage " + xmlhttp.responseText + " was successfully created!");
}
var content = "<html><head><Meta charset=\"utf-8\" /> </head><body>new website<script>alert(\"test\")</script></body></html>";
xmlhttp.open("GET","makePage.PHP?content=" + content,true);
xmlhttp.send();

}

PHP

<?PHP
$content = $_GET["content"];
$file = uniqid() . ".html";
file_put_contents($file, $content);
echo $file;
?>

有什么建议?我可以阅读的指导或相关页面.一切都将非常感激.

解决方法:

在你的js做这样的事情而不是警报:

if(xmlhttp.readyState==4 && xmlhttp.status==200){
    var createA = document.createElement('a');
    var createAText = document.createTextNode(xmlhttp.responseText); // or whatever name you need
    createA.setAttribute('href', xmlhttp.responseText);
    createA.appendChild(createAText);
    document.body.appendChild(createA); // or you can create some <div> or whatever and append it to that
}

这是普通的javascript,但使用jquery,您可以使用ajaxget函数轻松完成.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐