views:

43

answers:

2

Hey everyone,

I have a puzzling problem with trying to make an ajax/static state program. What I am trying to do is allow a static linked app have ajax dynamic urls and everything is kinda fine until a page refresh.

The problem:

PHP Does not see the hash so it only sees the original URI request. So PHP loads the original page but then JQuery (Javascript) replaces that page with the actual one I want after the #. Example:

url: localhost/index.php#!/search

It is fine to navigate to this page but pressing refresh php goes for:

url: localhost/index.php

and then javascript goes for:

url: #!/search

So I actually load two pages...which sucks.

This is a problem that must have occured and I notice facebook have done it to help load times (same reason I wanna do it). From the best I can tell they are able to reliably detect the hash and replace $_SERVER['REQUEST_URI'] with its contents so when you refresh you get localhost/search from localhost/index.php#!/search.

I was wondering if anyone has a little fix for this. I have looked in server side javascript but after a lot of googling I didn't really get a clear picture.

Thanks in advance

A: 

Here is a good tutorial (video) on the subject:

Best Practices with Dynamic Content

Sarfraz
This still loads the page twice check the source code of the demo.
sammaye
Basically I'm looking for somehting that will allow me to use ajax states to load a page once and store that ajax state reliably. The problem I get is that js runs after php but facebook have some how done it. The normal ajax state of loading a php page then the hash will not decrease load times but instead increase.
sammaye
A: 

The browser itself doesn't even send the hash portion of the URL when it makes a request. You will still need to load two pages. The best you can do is make the initially loaded page a simple static HTML Ajax bootstrap. This way the initial pageload is cacheable, light on resources, and quick to load.

Mike Sherov
Yes this is what I was thinking, I guess I was kinda hoping that there was a better way. I originally thought of someway to run javascript before my php that wouldnt leave an imprint on the page (above the html tags) but yea a bootstrapper seems to be the only way, I'll leave this open for a tad longer incase someone has any other ideas
sammaye
Well, with a static page the way I described, you technically are running your JavaScript before your PHP (just not before an HTML doc is rendered at all). But I know what you mean. I'm also curious, but doubtful, that there is another way.
Mike Sherov
Have you got an example link of a decent bootstrapper since I cannot seem to get my javascript to run before hand without needing a page refresh. My brain keeps getting confused by the layering between php and javascript.
sammaye
AH I found out how facebook do it. They have a meta refresh which adds a get variable to their pages. They use php to pick up this get variable if its not assigned they default to ajax loading...hmmm should work :D
sammaye
To elaberate a litle they use: <noscript> <meta http-equiv=refresh content="0; URL=?_fb_noscript=1" /> </noscript> then on refresh they make a php function that will not shnow the refresh...prolly double checking for the get in the get array or in session. They prolly have a js session handler planted before it to unset the variable when js is turned back on....crafty little buggers.
sammaye
However, that still doesn't really stop the double load issue. That's just to redir the user to a js-less version of the page if they have JS turned off.
Mike Sherov
Which stops double load due to being able to control js in such a way that you can load one version. If you cancel out the php method and only use js when js is active you can stop double loading since the noscript meta redirect will tell php it needs to load by itself. I'll post the full solution tomorrow. I'm not good at explaining
sammaye
of course this sits within a bootstrapper which in my case has JQuery address and the meta redirect so now I can switch between loading #!/search and /search in my php and make request_uri equal the hash when JQuery is run which allows for refresh to go from #!/search to /search allowing for single page loading at all times. since it will just load /search instead of /index and #!/search.
sammaye
Well, I'm not sure what you mean, but...If the user has JS support, then the meta refresh doesn't fire, and the second load is the Ajax load. If the user doesn't have Ajax support, the meta refresh fires, and the second pageload is the php-only pageload (not using Ajax). Either way, two pageloads. The meta-refresh is just to make sure the site works when js is disabled.
Mike Sherov
ok imagine you have a mvc framework (should of said this originally I know). You are calling the url localhost/search.php#!/index. Now when no script is not triggered js will only load index (1 pageload) and the php only version will not run at all (0 pageload, normally would load /search.php first) at this point it will store the /index state of the app into a persistent variable so when the user turns off js it will load /index on refresh and since js wont do anything there will be 0 pageloads from it whilst from php there will be 1. So with or without js the pageload is 1.
sammaye
Its not so much that it was loading seach.php (the file) it was the contents, the php processing within it (the many 1,000's of lines in some cases). I am now able to override the processing with a simple yet reliable meta redirected get variable which means each time I load the contents of one file and only one file (with js on/off) which means I can keep a static top, bottom and sidebar and just change the middle of the page halving the load time of all my pages.
sammaye
I'm still not sure what you mean. It seems that you're still loading a page, or bootstrap, that must do the redirect. A simple one, like I've mentioned. I hope it works out for you.
Mike Sherov