是不是AJAX页面加载上是一件坏事? [英] Isn't AJAX on pageload a bad thing?

查看:147
本文介绍了是不是AJAX页面加载上是一件坏事?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在书呆子晚餐和其他网站。在页面加载(在JavaScript中,通过浏览器),一个AJAX请求将走出去从那个呈现初始页面相同的服务器的一些数据。这些数据将是小的,有将否则$ P $从刚刚拉下pvented他们没有技术上的限制,在第一个地方的数据。

I've seen this on nerd dinner and other sites. On page load (in JavaScript, via the browser), an AJAX request will go out to get some data from the same server that rendered the initial page. The data will be small and there are no technical limitations that would have otherwise prevented them from just pulling down that data in the first place.

这不是一件坏事?这似乎是一种浪费AJAX调用的,因为他们可能只是使这个数据的JavaScript与页面的其余部分。

Isn't this a bad thing? It seems like a waste of an AJAX call, since they could just render this data as JavaScript with the rest of the page.

我的银行使用AJAX拉来构建表单元素的转帐形式的信息。这些信息是几千字节,一个AJAX请求似乎有矫枉过正。

My bank uses AJAX to pull the information to build form elements for a "Transfer Funds" form. That information is a few kilobytes, an AJAX request there seems overkill.

在书呆子晚餐,至少在,我看到,他们正在查询MIX09影片(通过AJAX)一套晚宴上呈现在页面加载地图控件。

In nerd dinner, at least in the MIX09 video that I saw, they are querying (via AJAX) a set of dinners to render on the map control on page load.

我能理解,如果我们谈论的大量数据,否则将需要很长时间才能拔下来的,但如果它在10-15kb,不只是更好拉数据下来的标记?难道他们这样做是为了避免数据的缓存?

I could understand if we're talking large amounts of data that would otherwise take too long to pull down, but if its under 10-15kb, wouldn't it just be better to pull the data down with the markup? Are they doing this to avoid caching of the data?

编辑:什么我的建议是,而不是开放的一个AJAX调用服务器拉下JSON数据在客户端的onload,只​​需asp.net(或其他)呈现在网页中JSON内容时,使一切。我只是觉得有必要指出这一点,因为实际的客户端code将是完全一样的,除了那里的JSON变量来源。

What I'm proposing is that instead of opening up an AJAX call to the server to pull down json data on the clients onload, simply have asp.net (or whatever) render the json in the pages content when it renders everything else. I just felt the need to point that out because the actual client side code would be exactly the same, except for where the json variable originates.

推荐答案

一般来说,根据我的经验,要避免任何的Javascript在网页上,你可以。我的意思是,如果你能做到这一点的服务器端,而不是使用Javascript,那么你应该。页面加载速度更快,你只是有一个更好的用户体验。

Generally speaking, in my experience, you want to avoid any Javascript on your page that you can. By this I mean, if you can do it on serverside instead of with Javascript then you should. Your page will load faster and you'll just have a better user experience.

这有时可能是更多的工作,尤其是当同一个载AJAX调用是在点以后的页面中使用。你可以通过做服务器端进行重复code。因此,有业绩,有多少code你想写之间的权衡。

That can sometimes be more work, particularly if that same on-load AJAX call is used during the page at later points. You might be duplicating code by doing it serverside. So there's a tradeoff between performance and how much code you want to write.

另一个方面这是JavaScript是有时防守用来对付僵尸,刮削器,恶意软件(如keysniffers等)等为您的安全和该网站的。这可能意味着,例如,加载页面元素的JavaScript只是因为它使我们更难破解或刮擦。没有做不到midn你,只是更难了。

The other aspect to this is that Javascript is sometimes used defensively against bots, scrapers, malware (like keysniffers and so on), etc for both your security and that of the site. This can mean, for example, loading page elements with Javascript simply because it makes it harder to hack or scrape. Not impossible midn you, just harder.

这篇关于是不是AJAX页面加载上是一件坏事?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆