在静态 HTML 页面上包含动态内容

发布于 2024-10-26 13:41:27 字数 278 浏览 1 评论 0原文

我注意到 IGN 和 Gamespot 等网站上的专题文章和评论页面都是 HTML (.html),页面上包含动态内容,例如用户评论。

他们如何在静态 HTML 页面中包含这种类型的动态内容?

如果我关闭 Javascript 并查看他们的其中一个页面,它们的动态内容就会消失,所以我认为它是用 Javascript 完成的。

我对提供类似的内容感兴趣,并且希望通过 HTML 而不是动态 PHP 页面来实现,所有内容都存储在数据库中(除了评论之类的内容)。

希望这是有道理的。

I've noticed on websites like IGN and Gamespot that their feature article and review pages are HTML (.html) with dynamic content like user comments included on the page.

How do they include this type of dynamic content in a static HTML page?

If I turn Javascript off and view one of their pages, they dynamic content disappears so I assume it's done with Javascript.

I'm interested in serving similar content and would like to do it via HTML instead of a dynamic PHP page with everything stored in a database (except for stuff like comments).

Hope that makes sense.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

温柔女人霸气范 2024-11-02 13:41:27

它是用 JavaScript 完成的。最常见的方法是在页面上使用 Javascript 来访问某种 API,以检索适当的评论(也称为 AJAX)。除非您使用某种 API(可能是 HTTP)公开该数据库,否则您将无法以这种方式访问​​数据库。

如果您想在自己的网站上使用类似的内容,请查看 Disqus 等服务,这些服务在其服务器上存储评论并检索它们使用 JavaScript。

It's done with Javascript. The most common way to do this is to have Javascript on the page that accesses some sort of API that retrieves the appropriate comments (aka AJAX). You won't be able to access a database this way unless you expose that database using some kind of API (probably HTTP).

If you want something like this on your own site, look in to services like Disqus which store comments on their servers and retrieve them using Javascript.

唐婉 2024-11-02 13:41:27

当您具有客户端-服务器关系时,后台通常会发生许多事情。

例如,Apache 可以配置为将任何文件结尾为 .xxx 的“文件扩展名”解析为 PHP。因此,您可以将 Apache 实例配置为解析 .html 文件中的 PHP,就像解析 .php 文件一样:

http://www.electrictoolbox.com/apache-parse-html-as-php/

而且,您可以为浏览器提供不同的“内容类型”,以便PHP 解析的页面可以将 PDF 内容发送到浏览器:

http://php .net/manual/en/function.header.php

现在,一种可能更简单的方法是使用 Apache 的重写,用于获取 URL 请求并将其重写为 PHP 页面,并将 URL 请求的其余部分添加为请求属性,例如在模型-视图-控制器模式中:

http://expressionengine.com/wiki/Remove_index.php_From_URLs/

http://en.wikipedia.org/wiki/Model%E2%80%93view%E2 %80%93controller

这样的 URL 例如 http://www.example .com/article/feature/my_story.html 实际上会被服务器的 PHP 解析器视为 http://www.example.com/index.php?class=article&method=feature&id=my_story.html

然后就可以使用AJAX了专门更新页面一部分的方法,正如其他答案中提到的。

When you have the client-server relationship, there are often many things happening in the background.

For instance, Apache can be configured to parse any "file extension" with file ending .xxx as PHP. So, you can configure your Apache instance to parse PHP within .html files just as it would with .php files:

http://www.electrictoolbox.com/apache-parse-html-as-php/

And as well, you can serve different "content-types" to the browser, so that a PHP-parsed page can send, for instance, PDF content to the browser:

http://php.net/manual/en/function.header.php

Now, a possibly easier way is to use Apache's rewrite to take a URL request and rewrite it to a PHP page with the rest of the URL request being added as request attributes, such as in the Model-View-Controller pattern:

http://expressionengine.com/wiki/Remove_index.php_From_URLs/

http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller

So that a URL such as http://www.example.com/article/feature/my_story.html will actually be seen by the server's PHP parser as http://www.example.com/index.php?class=article&method=feature&id=my_story.html

And then you can use AJAX methods to specifically update a portion of a page, as has been mentioned in other answers.

牵强ㄟ 2024-11-02 13:41:27

实现此目的的一种常用技术是使用 AJAX。显然,动态内容必须来自动态服务器端脚本。

One commonly used technique to achieve this is to use AJAX. Obviously the dynamic content must come from a dynamic server side script.

少年亿悲伤 2024-11-02 13:41:27

我不知道,但我猜它是 JavaScript ajax。有一些解决方案可以做到这一点,但 JavaScript 是最好的,请查看 MooTools(我个人最喜欢的)和 JQuery。

I don't know, but I'm guessing it's JavaScript ajax. There are a few solutions that could do this, but JavaScript is the best, check out MooTools (my personal favourite) and JQuery.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文