Author Topic: Fun with AJAX  (Read 4858 times)

0 Members and 1 Guest are viewing this topic.

Offline Netham45

  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2103
  • Rating: +213/-4
  • *explodes*
    • View Profile
Fun with AJAX
« 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.
Omnimaga Admin

Offline Juju

  • Incredibly sexy mare
  • Coder Of Tomorrow
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 5730
  • Rating: +500/-19
  • Weird programmer
    • View Profile
    • juju2143's shed
Re: Fun with AJAX
« Reply #1 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.

Remember the day the walrus started to fly...

I finally cleared my sig after 4 years you're happy now?
THEGAME
This signature is ridiculously large you've been warned.

The cute mare that used to be in my avatar is Yuki Kagayaki, you can follow her on Facebook and Tumblr.

Offline Netham45

  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2103
  • Rating: +213/-4
  • *explodes*
    • View Profile
Re: Fun with AJAX
« Reply #2 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.
« Last Edit: November 19, 2011, 05:37:37 pm by Netham45 »
Omnimaga Admin

Offline Scipi

  • Omni Kitten Meow~ =^ω^=
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1547
  • Rating: +192/-3
  • Meow :3
    • View Profile
    • ScipiSoftware
Re: Fun with AJAX
« Reply #3 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.

Imma Cat! =^_^= :3 (It's an emoticon now!)
Spoiler For Things I find interesting:
Spoiler For AI Programming:
Spoiler For Shameless advertising:

Spoiler For OldSig:





Spoiler For IMPORTANT NEWS!:
Late last night, Quebec was invaded by a group calling themselves, "Omnimaga". Not much is known about these mysterious people except that they all carried calculators of some kind and they all seemed to converge on one house in particular. Experts estimate that the combined power of their fabled calculators is greater than all the worlds super computers put together. The group seems to be holding out in the home of a certain DJ_O, who the Omnimagians claim to be their founder. Such power has put the world at a standstill with everyone waiting to see what the Omnimagians will do...

Wait... This just in, the Omnimagians have sent the UN a list of demands that must be met or else the world will be "submitted to the wrath of Netham45's Lobster Army". Such demands include >9001 crates of peanuts, sacrificial blue lobsters, and a wide assortment of cherry flavored items. With such computing power stored in the hands of such people, we can only hope these demands are met.

In the wake of these events, we can only ask, Why? Why do these people make these demands, what caused them to gather, and what are their future plans...

Offline Netham45

  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2103
  • Rating: +213/-4
  • *explodes*
    • View Profile
Re: Fun with AJAX
« Reply #4 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.
« Last Edit: November 19, 2011, 05:46:17 pm by Netham45 »
Omnimaga Admin

Offline Juju

  • Incredibly sexy mare
  • Coder Of Tomorrow
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 5730
  • Rating: +500/-19
  • Weird programmer
    • View Profile
    • juju2143's shed
Re: Fun with AJAX
« Reply #5 on: November 19, 2011, 05:54:30 pm »
Yeah it works now. Looks kinda faster indeed. Kinda.
« Last Edit: November 19, 2011, 06:06:49 pm by Juju »

Remember the day the walrus started to fly...

I finally cleared my sig after 4 years you're happy now?
THEGAME
This signature is ridiculously large you've been warned.

The cute mare that used to be in my avatar is Yuki Kagayaki, you can follow her on Facebook and Tumblr.

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55941
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Fun with AJAX
« Reply #6 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.
« Last Edit: November 19, 2011, 11:49:05 pm by DJ_O »

Offline Netham45

  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2103
  • Rating: +213/-4
  • *explodes*
    • View Profile
Re: Fun with AJAX
« Reply #7 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.
« Last Edit: November 19, 2011, 11:51:08 pm by Netham45 »
Omnimaga Admin

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55941
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Fun with AJAX
« Reply #8 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.

Offline Netham45

  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2103
  • Rating: +213/-4
  • *explodes*
    • View Profile
Re: Fun with AJAX
« Reply #9 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.
Omnimaga Admin

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55941
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Fun with AJAX
« Reply #10 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.

Offline Netham45

  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2103
  • Rating: +213/-4
  • *explodes*
    • View Profile
Re: Fun with AJAX
« Reply #11 on: November 19, 2011, 11:55:11 pm »
Nah, XDA-Developers takes that long to load, not Omni.
Omnimaga Admin

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55941
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Fun with AJAX
« Reply #12 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.