我如何创建一个新的.html网页点击


How do I create a new .html webpage onclick?

我想做的是当用户点击我的html按钮时,我想在服务器上动态创建一个新的网页。我需要新网页的url有它自己的url,也就是说,它应该是不同的。

更详细地说:当用户单击按钮时,我想上传一个新的.html文件到服务器。因此,例如,如果我有一个名为www.check.com/index.html的网站,当用户单击index.html中的按钮时,我需要创建一个包含一些html行的新.html(但没有CSS,页面上不会显示任何内容)。因此,当用户单击按钮时,我希望将文件上传到服务器,并使用唯一的url,如www.check/1.html,第二次它将是check.com/2.html,等等。如果是1.html, 2.html等等,我觉得很好。

Javascript代码:

 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></head><body><meta name='"twitter:card'" content='"summary_large_image'"><meta name='"twitter:site'" content='"@nytimes'"><meta name='"twitter:creator'" content='"@SarahMaslinNir'"><meta name='"twitter:title'" content='"Parade of Fans for Houston’s Funeral'"><meta name='"twitter:description'" content='"Blah Blah Blah content.'"><meta name='"twitter:image'" content='"'"><script>document.getElementById('"imgTweet'").innerHTML.write(img);</script></body></html>";
xmlhttp.open("GET","makePage.php?content=" + content,true);
xmlhttp.send();}

'img'是我的另一个页面中的变量。网页由代码创建而不执行

必须使用AJAX来执行php。使用file_put_contents()在php中创建新文件非常简单。

下面是一个例子:
makePage.html:

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

makePage.php:

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

makePage.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();
}

希望对大家有所帮助