简体   繁体   English

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

[英]Including dynamic content on a static HTML page

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. 我在IGN和Gamespot等网站上注意到,他们的专题文章和评论页面是HTML(.html),其动态内容(如用户评论)包含在页面上。

How do they include this type of dynamic content in a static HTML page? 它们如何在静态HTML页面中包含这种动态内容?

If I turn Javascript off and view one of their pages, they dynamic content disappears so I assume it's done with Javascript. 如果我关闭Javascript并查看他们的页面之一,它们的动态内容就会消失,因此我认为这是使用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). 我有兴趣提供类似的内容,并且希望通过HTML而不是将动态内容存储在数据库中的动态PHP页面中(除了注释之类的东西)来实现。

Hope that makes sense. 希望有道理。

It's done with Javascript. 它是用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). 最常见的方法是在页面上使用Javascript来访问某种API,以检索适当的注释(也称为AJAX)。 You won't be able to access a database this way unless you expose that database using some kind of API (probably HTTP). 除非您使用某种API(可能是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. 如果您想在自己的网站上找到类似的东西,请访问类似Disqus的服务,这些评论将评论存储在其服务器上并使用Javascript进行检索。

One commonly used technique to achieve this is to use AJAX . 实现此目的的一种常用技术是使用AJAX Obviously the dynamic content must come from a dynamic server side script. 显然,动态内容必须来自动态服务器端脚本。

I don't know, but I'm guessing it's JavaScript ajax. 我不知道,但是我猜这是JavaScript ajax。 There are a few solutions that could do this, but JavaScript is the best, check out MooTools (my personal favourite) and JQuery. 有几种解决方案可以做到这一点,但是JavaScript是最好的,请查看MooTools(我个人的最爱)和JQuery。

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. 例如,可以将Apache配置为将任何以.xxx结尾的“文件扩展名”解析为PHP。 So, you can configure your Apache instance to parse PHP within .html files just as it would with .php files: 因此,您可以配置Apache实例以解析.html文件中的PHP,就像解析.php文件一样:

http://www.electrictoolbox.com/apache-parse-html-as-php/ 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: 同样,您可以为浏览器提供不同的“内容类型”,以便PHP解析的页面可以将PDF内容发送到浏览器:

http://php.net/manual/en/function.header.php 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: 现在,一种可能更简单的方法是使用Apache的重写来获取URL请求并将其重写为PHP页面,并将其余URL请求添加为请求属性,例如在Model-View-Controller模式中:

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

http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller 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 因此,服务器的PHP解析器实际上会将诸如http://www.example.com/article/feature/my_story.html之类的URL视为http://www.example.com/index.php?class=article&method =特征&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. 然后,您可以使用AJAX方法专门更新页面的一部分,如其他答案所述。

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

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