简体   繁体   English

如何使用HTML或JS动态创建新的html文件?

[英]How to create a new html file dynmically using HTML or JS?

I am sure that there are many tutorial that explains how can I do it, but I dont know how it called. 我敢肯定有很多教程可以解释我该怎么做,但是我不知道它怎么称呼。

So what I want is somthing like this: 所以我想要的是这样的东西:

The user create is page, and he can hit "save" and a new html page created, for example: 用户创建页面,他可以单击“保存”并创建一个新的html页面,例如:

At first the user in index.html, and when he hit save he will get into somthing like: ../username.html 首先,使用index.html的用户,当他单击save时,将进入类似以下内容:../username.html

How can I do that? 我怎样才能做到这一点?

You need something server side to dynamically generate the code for the page. 您需要服务器端来动态生成页面代码。 EG you have a static page name (user.php or user.aspx etc) and then depending what the user does, you populate the code on this page from the server. 例如,您有一个静态页面名称(user.php或user.aspx等),然后根据用户的操作,从服务器填充此页面上的代码。

It can be done on the client side using JS but it would be very very bad practice to have this level of processing happening in the client. 可以使用JS在客户端完成此操作,但是在客户端进行这种级别的处理将是非常非常糟糕的做法。

There are many ways to do this. 有很多方法可以做到这一点。

  1. Save the page to the database using mysql or any other query language. 使用mysql或任何其他查询语言将页面保存到数据库。

    1.1 Create a table eg "pages" with columns: filename | 1.1创建一个带有列的表,例如“ pages”:filename | contents 内容

    1.2 Now use mod_rewrite or nodejs routing to look up the filename in the requested path, select the contents from the database and send the contents to the user. 1.2现在,使用mod_rewrite或nodejs路由在请求的路径中查找文件名,从数据库中选择内容并将其发送给用户。

  2. Save the file directly to the harddrive. 将文件直接保存到硬盘驱动器。

    2.1 in php you could use file_put_contents(filename, contents); 在PHP 2.1中,您可以使用file_put_contents(filename,contents); and in nodejs you could use fs.writeFile(filename, data[, options], callback). 在nodejs中,您可以使用fs.writeFile(filename,data [,options],callback)。

But it's important to keep in mind that the page contents could have malicious code in it. 但是,请记住页面内容中可能包含恶意代码,这一点很重要。 So you would need to double check the contents of the file. 因此,您需要仔细检查文件的内容。

The contents for the page should be send using the POST method. 该页面的内容应使用POST方法发送。 Here you could use a form with method="POST" or ajax with type: "post". 在这里,您可以使用method =“ POST”或ajax类型的表单:“ post”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM