简体   繁体   English

如何使用javascript生成一组多个HTML页面

[英]How to generate a set of multiple HTML pages with javascript

I'm creating web application which would generate a set of related html pages (each page is like a step) from data in imported json file and each would follow more or less predefined format and I can not use any server language.我正在创建 Web 应用程序,它将从导入的 json 文件中的数据生成一组相关的 html 页面(每个页面就像一个步骤),每个页面都或多或少地遵循预定义的格式,我不能使用任何服务器语言。

What would be possible ways to implement this?实现这一点的可能方法是什么?

I know I could probably use templating framework but that I think would generate everything in one file and pages would not be bookmarkable or I could use #ids in url.我知道我可能可以使用模板框架,但我认为会在一个文件中生成所有内容,并且页面将不可添加书签,或者我可以在 url 中使用 #ids。

Are there any other/better ways?还有其他/更好的方法吗?

You're using JavaScript in the browser as a client side language, you can't create HTML pages on the server with it.您在浏览器中使用 JavaScript 作为客户端语言,您无法使用它在服务器上创建 HTML 页面。 You're probably referring to a single-page website where JavaScript makes it seem like there are subpages.您可能指的是一个单页网站,其中 JavaScript 使它看起来像是有子页面。

In the simplest case, it's nothing more than changing the content of a <p> tag when a “link” which is really just an element with an attached event is clicked.在最简单的情况下,当一个“链接”实际上只是一个带有附加事件的元素被点击时,它只不过是更改<p>标签的内容。

As for the content, you could either hardcode all the subpages into your one page, or load it via AJAX from other, real HTML pages on the server, or some API.至于内容,您可以将所有子页面硬编码到一个页面中,或者通过 AJAX 从服务器上的其他真实 HTML 页面或某些 API 加载它。

What you're referring to with你指的是什么

use #ids in url在网址中使用#ids

is just using hashes to store the state, as in the “location” within the possible subpages.只是使用散列来存储状态,就像在可能的子页面中的“位置”一样。 This has advantages like allowing direct access to a subpage with a link that contains a hash, as well as enabling the browsers native back and forward functions within your system.这具有一些优点,例如允许通过包含哈希的链接直接访问子页面,以及在您的系统中启用浏览器本机后退和前进功能。

Without server-side scripting you will have a single html page which will build itself to represent one of your steps.如果没有服务器端脚本,您将拥有一个单独的 html 页面,该页面将自行构建以表示您的步骤之一。

If you want to be able to arrive at a specific step then using the #id in url is the best way.如果您希望能够到达特定步骤,那么在 url 中使用 #id 是最好的方法。

If you really want to have a separate page per step then you will have to create a static page per step, and the page can set itself up based on the window.location.pathname but that becomes unwieldy and only works if you have a fixed number of steps.如果您真的希望每一步都有一个单独的页面,那么您将必须为每一步创建一个静态页面,并且该页面可以根据window.location.pathname自行设置,但这变得笨拙,只有在您有固定步数。

You might initially avoid using a templating framework but the reality is that you will only end up inventing your own!您最初可能会避免使用模板框架,但事实是您最终只会发明自己的模板!

I recommend that you use Knockout .我建议您使用Knockout Their tutorials are extremely informative.他们的教程非常有用。 Do not be put off by it being an MVVM platform, it is small and simple to learn.不要因为它是一个 MVVM 平台而被推迟,它小巧且易于学习。

In addition to Knockout to perform the dynamic content creation, you can use Sammy to handle routing based on the #id in the url.除了使用 Knockout 执行动态内容创建之外,您还可以使用Sammy根据 url 中的 #id 处理路由。

Good luck祝你好运

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

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