Omnimaga

General Discussion => Technology and Development => Computer Projects and Ideas => Topic started by: Netham45 on November 19, 2011, 05:26:35 pm

Title: Fun with AJAX
Post by: Netham45 on November 19, 2011, 05:26:35 pm
I've had an idea for a while to accelerate page changes on one site with AJAX. This is what I came up with.

Code: [Select]
function getAjaxObject()
{
xmlhttp=new XMLHttpRequest(); //Decent Browsers
if (!xmlhttp || xmlhttp == undefined || xmlhttp == null) xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");  //IE7+
if (!xmlhttp || xmlhttp == undefined || xmlhttp == null) xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); //IE6-
return xmlhttp;
}

function loadPage(url)
{
xmlhttp = getAjaxObject();
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementsByTagName("html")[0].innerHTML=xmlhttp.responseText;
processTags();
}
}
xmlhttp.open("GET",url,true);
xmlhttp.send();
}

function processTags()
{
var atags=document.getElementsByTagName("a");
for (x in atags)
{
atags[x].onclick="loadPage('"+atags[x].href+"');";
atags[x].href="#"; //This also causes it to scroll to the top of the page.
}
}

processTags();

This basically replaces all the 'A' tags on a page with a call to an AJAX function that loads the new page w/o refreshing.

Obviously there are incompatibility issues with this. On Omnimaga, where I tested, posting is a bit wonky, you also can't click links to sites outside the current domain.

It is considerably faster loading pages for me, though.

Also, this doesn't take a considerable amount of real-world situations into consideration, so don't be surprised if it breaks more than it does good. :P

If you want to give it a try, here's the JS to paste in your address bar. This should, at least in theory, work with (most) any site.

Code: [Select]
javascript:head=document.getElementsByTagName('head')[0];script=document.createElement('script');script.type='text/javascript';script.src='http://netham45.org/fastload.js?t=6';head.appendChild(script);void(0);

Or, click here.
Title: Re: Fun with AJAX
Post by: Juju on November 19, 2011, 05:30:32 pm
It doesn't work for me. Plus, I don't think it makes stuff faster. You still have to load the page.
Title: Re: Fun with AJAX
Post by: Netham45 on November 19, 2011, 05:31:39 pm
It doesn't work for me. Plus, I don't think it makes stuff faster. You still have to load the page.

It doesn't actually reload the page, though. It's noticeably faster for me on a lot of sites, but it seems to break Opera randomly.

Edit: Appears to not work in FF at all.

Edit 2: Fixed in FF.
Title: Re: Fun with AJAX
Post by: Scipi on November 19, 2011, 05:38:46 pm
Seems to work in Chrome... I think :P

I see that there is no loading icon when I load a new page.
Title: Re: Fun with AJAX
Post by: Netham45 on November 19, 2011, 05:43:37 pm
I'm seeing a slight performance boost in FF/Chrome. The effect seems to be a bit more pronounced in Opera. It's broken in IE (as if I had to say).

Edit: It'll be obvious when it works 'cause the address bar in your browser won't change.
Title: Re: Fun with AJAX
Post by: Juju on November 19, 2011, 05:54:30 pm
Yeah it works now. Looks kinda faster indeed. Kinda.
Title: Re: Fun with AJAX
Post by: DJ Omnimaga on November 19, 2011, 11:48:35 pm
I'M not sure if it's a good idea, given the problem some browsers have at handling AJAX stuff, especially Opera and IE.

Also I'm worried about SEO. Could this hurt our Google ranking?

Btw there's a bug with AJAX posting: When the post appears, my avatar doesn't get auto-resized until I reload the page.
Title: Re: Fun with AJAX
Post by: Netham45 on November 19, 2011, 11:49:12 pm
I'm not going to implement this on Omnimaga, it was just something I was toying around with. It's not too stable in its current state and there are numerous scenarios that I'm not accommodating for with it that it pretty much breaks entirely in.
Title: Re: Fun with AJAX
Post by: DJ Omnimaga on November 19, 2011, 11:51:03 pm
Ah ok. I was worried about SEO and link pasting, because when I clicked copy link to clipboard, I got javascript:loadPage('http://www.omnimaga.org/index.php?action=profile;u=3');void(0); instead of http://www.omnimaga.org/index.php?action=profile;u=3 . I think this will make it more of a PITA than anything else. It could definitively be useful for some stuff, though.
Title: Re: Fun with AJAX
Post by: Netham45 on November 19, 2011, 11:53:18 pm
I've noticed it makes browsing some sites a lot faster, like XDA-Developers is considerably faster with it. It's as fast as Omni is with it, but without it it sometimes takes >30 seconds to load a page.
Title: Re: Fun with AJAX
Post by: DJ Omnimaga on November 19, 2011, 11:54:19 pm
Weird I didn't notice such slow speed on Omni except Thursday evening. The only page that sometimes take close to that is the posting stats page, because it's incredibly MySQL-intensive.
Title: Re: Fun with AJAX
Post by: Netham45 on November 19, 2011, 11:55:11 pm
Nah, XDA-Developers takes that long to load, not Omni.
Title: Re: Fun with AJAX
Post by: DJ Omnimaga on November 20, 2011, 12:04:51 am
Oh ok phew lol. As for the Omnis tats page, I think the problem is that the default SMF page is already intensive, but then we added plenty of EzPortal in it, then extra stats added from another mod (monthly/daily/weekly posters) and then post ratings. This is why it was disabled for guests near our final days on 1and1.