简体   繁体   English

网站导航-PHP / HTML与Ajax与CSS / Javascript

[英]Site Navigation - PHP/HTML vs. Ajax vs. CSS/Javascript

I have recently been debating whether or not to use AJAX for my site navigation to only transfer over only needed updated HTML elements, or if there was not a significant difference between new elements and current elements to just load the page in its entirety (php generated or static html) 最近,我一直在争论是否将AJAX用于我的网站导航,以便仅转移所需的更新HTML元素,或者新元素和当前元素之间是否没有显着差异,以便仅加载整个页面(生成的php)或静态html)

However, I thought how that if the new content was not large in size relative the current page...that perhaps I should send it has a hidden div (via CSS) along with the current page. 但是,我想如果新内容相对于当前页面而言尺寸不大,那该怎么办呢?也许我应该发送的内容与当前页面一起具有隐藏的div(通过CSS)。

This 3rd way seems like a simple solution. 第三种方法似乎是一个简单的解决方案。 For example just send the entire current page + any additional content that might be requested by the user as hidden divs. 例如,仅发送整个当前页面+用户可能要求作为隐藏div的任何其他内容。

When the user selects the content just hide the current content, and display the hidden content... 当用户选择内容时,只需隐藏当前内容,然后显示隐藏的内容...

All in all, each way (normal, Ajax, CSS) would look the same to the user but a CSS / Javascript solution would be provide the quickest interface and be the simplest. 总而言之,每种方式(普通,Ajax,CSS)对用户来说都是相同的,但是CSS / Javascript解决方案将提供最快的界面,并且是最简单的。 Ajax might cut down on download for example if the content is never used. 例如,如果从未使用过内容,则Ajax可能会减少下载。

This is a validation question. 这是一个验证问题。 Is this a valid way to navigate a web application? 这是浏览Web应用程序的有效方法吗? By hiding/displaying divs using the display property or opacity property to flip through content? 通过使用display属性或opacity属性隐藏/显示div来翻阅内容?

Notes (response to answers) 笔记(对答案的回应)

  1. The Hidden divs would be static data that is not changed by the user. 隐藏的div将是用户不会更改的静态数据。 I thought this would be obvious but now I've made it explicit. 我以为这很明显,但是现在我已经明确了。

Thanks! 谢谢!

You need to think about it this way. 您需要这样考虑。

If the hidden content is dynamic (changing) there is a need for AJAX because AJAX would usually be used to fetch updated content from the DB. 如果隐藏的内容是动态的(更改中的),则需要AJAX,因为AJAX通常将用于从数据库中获取更新的内容。

If your content is static (not changing) then how much new content are we talking? 如果您的内容是静态的(不变),那么我们在谈论多少新内容? Does the size of the new content have a significant impact on render time if it was in a hidden DIV? 如果新内容的大小在隐藏的DIV中,是否会对渲染时间产生重大影响? If its very little, then I would say use a hidden DIV. 如果它很小,那么我会说使用隐藏的DIV。 If its alot, then mabye it's time to consider AJAX to load it in from an external page. 如果它很多,那么就该考虑使用AJAX从外部页面加载它了。

Here is a simple solution to get you started using a hidden DIV: 这是一个简单的解决方案,可帮助您开始使用隐藏的DIV:

<script>
     function setVisibility(id, visibility){
     document.getElementById(id).style.display=visibility;}
</script>

<div id="message1" onclick="setVisibility('message1', 'none');setVisibility('message2', 'inline');"> >Hey What's Up?</div>

<div style="display:none;" id="message2">Not Much You?</div>

It does depend on the data being shown. 它确实取决于显示的数据。 Facebook could not do this because the data is updated often, there would be out of sync problems. Facebook无法执行此操作,因为数据经常更新,因此会出现不同步的问题。

Since i do most of the development by myself, having a full ajaxy site seems like a large task for me, so i usually keep page flips, but some of the content on the inside i have it as ajax generated. 由于我自己完成大部分开发工作,因此拥有一个完整的ajaxy站点对我来说似乎是一项艰巨的任务,因此我通常会进行页面翻转,但是内部的某些内容是由ajax生成的。

Like say there is a form to create something, i have that form submit in the background (excellent jquery plugin for that) then i have the new data be displayed at the top of the list ($.prepend). 就像说有一个表单可以创建一些东西,我在后台提交表单(出色的jquery插件),然后将新数据显示在列表的顶部($ .prepend)。 This way, things are still ajaxed, but not to a level of which is hard to manage for a single programmer. 这样,事情仍然被搁置,但没有达到一个程序员难以管理的水平。

One thing you need to keep in mind is to keep your site as non-breakable and accessible as possible. 您需要记住的一件事是使您的站点尽可能不易损坏且可访问。 Because Ajax content isn't on the page, it can't be indexed by spiders, etc. Ajax requires more error testing/catching, etc than "standard" CSS alteration, so make sure you're going to make it work in ALL browsers. 因为Ajax内容不在页面上,所以它不能由蜘蛛等索引。与“标准” CSS更改相比,Ajax需要更多的错误测试/捕获等操作,因此请确保将其全部使用浏览器。 There's no excuse for bad or broken navigation. 导航不正确或不存在任何借口。

Your idea is definitely a valid approach. 您的想法绝对是有效的方法。 You're basically looking for a trade-off between an initial, longer request, and many shorter ones down the line. 基本上,您正在寻找初始的较长请求和许多较短的请求之间的权衡。 As long as the amount of initially hidden content is not gratuitous, the user experience will likely benefit by getting all of the markup first, and hiding/showing it with JavaScript + CSS as needed. 只要最初隐藏的内容不是多余的,用户体验就可能会受益,因为首先获取所有标记,然后根据需要使用JavaScript + CSS进行隐藏/显示。

Suggested Approach : Consider using jQuery's 建议的方法 :考虑使用jQuery

methods, and combining with a history manager to emulate real browser history changes as the user navigate's page states. 方法,并与历史记录管理器结合使用,以模拟用户浏览页面状态时实际浏览器历史记录的更改。 This is the approach I use on my website: paislee.net . 这是我在网站上使用的方法: paislee.net IMO its clean and lightning fast because there are no new HTTP requests. 由于没有新的HTTP请求,因此IMO的清理速度快且闪电般。

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

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