Wasteful Ajax Page Loading
I've started a new job, and the portion of the project I'm working has a very odd structure. Every pages is a .Net aspx page, and it loads just fine, but nothing is really done at load time. Everything is really loaded from a jquery document.onready handler.
What is even more...interesting...is that the onready handler calls some ajax calls that drop entire .aspx pages into divs on the page, but first it strips out several parts of the the returned page. This is the "magic" script the previous programmer ran on all the returned html from his ajax calls:
function CleanupResponseText(responseText, uniqueName) {
responseText = responseText.replace("theForm.submit();", "SubmitSubForm(theForm, $(theForm).parent());");
responseText = responseText.replace(new RegExp("theForm", "g"), uniqueName);
responseText = responseText.replace(new RegExp("doPostBack", "g"), "doPostBack" + uniqueName);
return responseText;
}
He then intercepts any kind of form postback and runs his own form submission function:
function SubmitSubForm(form, container) {
//ShowLoading(container);
$(form).ajaxSubmit( {
url: $(form).attr("action"),
success: function(responseText) {
$(container).html(CleanupResponseText(responseText, form.id));
$("form", container).css("margin-top", "0").css("padding-top", "0");
//HideLoading(container);
}
}
);
}
Am I way offbase in thinking that this is less than optimal? I mean, how does a browser take out the html and head and other tags that don't hav开发者_JAVA百科e anything to do with what you are really trying to drop into that div?
Also, he's returning things like asp:gridview controls, and the associate viewstate, which can be quite large if his dataset is big.
Has anyone seen this before?
I have, earlier in 2006 when I first learned Ajax, used it to retrieve the main content of my page after page loads. (or rather, at the bottom of the html). But I soon discarded it.
It doesn't feel right.
- If the page is accessible on the internet then this is pretty bad for marketing/SEO. Since what the search engines will see is an empty page. Most crawlers don't bother to run javascript I think.
- Performance wise it doesn't help much. As time goes by we've learned that Ajax is best used to update small segments of html. If the content returned through ajax is larger than the "load size", then you are not really getting any benefit. And with caching the load time is quite minimal.
- Compatibility. Even though all major browsers support ajax pretty well, it doesn't worth making the page totally depends on js just because it doesn't break. Plus Ajax request will unnecessary use more cpu and consequently more battery on mobile devices.
- History implementation is probably harder. If you go back and forth in the history, most likely the page will be empty or have to call the server to retrieve the contents again.
Even though it works it still feels like it's more of a hack than solid code.
ASP.NET doesn't support having multiple forms on the same page. It looks like this is circumventing that.
Does your site run slowly? (do you need to worry about this performance?)
Do your pages have multiple <html> <body> <head>
tags in them? Or is that stripped out before the controls are returned for rendering on the page? If they are left in, browsers are just handling it however they please, and the behavior (as far as I know) isn't defined and can't be determined to be consistent.
...and it loads just fine...
No problem, no worry :-) Not that I would choose this approach.
However, to answer the end of that: Modern web-browsers simply discard invalid HTML tags for a given context (HTML/HEAD/BODY don't fly inside a DIV ;-) while making a best effort to add all that they can. So, while ugly and relying on implementation details, the best-effort of the browser makes this possible for a wide range of inputs. YMMV.
精彩评论