dscrn1974 2013-09-26 19:02
浏览 126

使用AJAX引入页面元素,或者只是隐藏和显示它们是否更有效?

In web development (jQuery more specifically) am I better off "pre-loading" my page before hand and just use jQuery to manipulate the DOM or the other way around.

It is a question of the following:

<div id="item1"></div>
<div id="item2"></div>
<div id="element><!-- AJAX --></div>

$("#item1").click(function(e) {
     $("#element").load("ajax/response/containing/HTML_1.php");
});

$("#item2").click(function(e) {
     $("#element").load("ajax/response/containing/HTML_2.php");
});
.
.
.

versus

<div id="item1"></div>
<div id="item2"></div>
<div id="element>
   <div id="item1_content" style="display:none"><? include('/path/to/html_1'); ?></div>
   <div id="item2_content" style="display:none"><? include('/path/to/html_2'); ?></div>
</div>

$("#item1").click(function(e) {
     $("#item1_content").show();
     $("#item2_content").hide();
     //Possibly do an AJAX call that simply returns JSON data and do something with it
});

$("#item2").click(function(e) {
     $("#item2_content").show();
     $("#item1_content").hide();
     //Possibly do an AJAX call that simply returns JSON data and do something with it
});
.
.
.

I find the second method to be more elegant since the backend only returns JSON data which the front-end manipulates. With the first method the PHP scripts have to actually return HTML.

I would think the first method would hammer on the server harder since simple things such as displaying a dialog would require an AJAX call (even if it didn't need one because all the content is included in method 2).

The second method seems like it would hammer on the client harder since I am doing all my data manipulation in JS. The first method however dumps the HTML responses into the divs and reformats the DOM so I am not sure if it's just as intense.

What about the impact of load times? The first method has to essentially render a giant document containing elements that may never be shown but with a single request, The second method has to make a million requests but results in a smaller DOM.

  • 写回答

1条回答 默认 最新

  • duanqie8549 2013-09-26 19:14
    关注

    Do you care what visitors see when viewing the page source? If you do, then use AJAX.

    Is there any chance that server speed could be an issue? There can be a very slight (but noticeable) delay when using AJAX. If that is a problem, use hide/show.

    However, as Jack and Rory pointed out, everything depends on what you are doing. If you can render the initial page much faster by making some data loading conditional, then use AJAX.

    However, AJAX will introduce an additional level of complexity to your page. Not much, but a little. Will future developers be able to maintain your page? (Almost certainly, yes)

    Final consideration: AJAX is way fun.

    评论

报告相同问题?

悬赏问题

  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)