PHP与JavaScript的动态HTML页面 [英] PHP vs JavaScript For Dynamic HTML Pages
问题描述
通常,当我将动态生成的HTML标记放在一起时,我一直使用PHP来存储信息,然后通过循环来在页面上创建元素。一个例子是导航;创建一个对象数组,然后遍历它们以回显标记。这在开发或维护过程中可能需要进行较小(或主要)更改的时候会有所帮助。
最近我一直在想我是否应该使用JavaScript代之以做到这一点。同样的原则,但使用addElement。
只是想获得一些意见,
谢谢大家!
在决定是否应该在客户端而不是服务器端做一些事情时,作为一个经验法则问自己两个问题:
- 用户是否可以获得即时反馈,以响应他们做的事情?例如他们尝试提交的表单中的数据不正确的错误消息。如果是这样,那么做客户端将是有益的。
- 它可以在服务器端完成吗?如果是这样,首先做服务器端,因为它更可靠(并且非美化的东西,更难以干预)。 建立在工作之上的东西。
Typically when I put together dynamically generated HTML markup, I've been using PHP to store the information and then looping through that to create elements on the page. One example is navigation; create an array of objects and then loop through them to echo the markup. This helps out a lot for times that I might have to make minor (or major) changes during development or maintenance.
Lately I've been wondering if I should use JavaScript to do this instead. Same principle, but using addElement.
Just wanted to get some opinions on this; pros, cons, php vs js, seo considerations, etc.
Thanks folks!
Doing it client side means:
- Doing it in lots of different environments instead of a single one
- Having it break whenever a user comes along without JS (for whatever reason)
- Having it fail to work for the vast majority of bots (including search engines)
- Investing development time in converting all your logic
- Requiring the browser to make additional requests to the server, slowing down load times
When deciding if you should do something client side instead of server side, as a rule of thumb ask yourself two questions:
- Would the user benefit from getting instant feedback in response to them doing something? e.g. An error message for improper data in a form they are trying to submit. If so, then doing it client side would be beneficial.
- Can it be done server side? If so, do it server side first as it is more reliable (and for non-cosmetic things, harder to interfere with). Build on things that work.
这篇关于PHP与JavaScript的动态HTML页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!