0

I am making an all ajax site and loading my pages with jQuery's .load(). I am also changing the URL when I load a new page so my account url would look something like this : http://mysite.com/#!/account/ and my home screen looks like this http://mysite.com/#!/. This is the code I am using currently:

var oldLoc = window.location.hash.replace(/^#\!/,"");
if (oldLoc == '/account/') {
    $('#reload_section').load('account.php');
} else if (check for other urls) {
}

This works fine until I throw my search in there. When a user submits a search form, the url changes to this http://mysite.com/#!/search/objects/Query Here where objects has 2 values objects and files. How can I incorporate the regex to separate the search URL into three parts but then also works if it is just http://mysite.com/#!/accounts or http://mysite.com/#!/. Also, how can I find the search parameters like if I search for Taco Truck the url would reflect it as http://mysite.com/search/objects/taco%20truck/.


This is what I was thinking of using:

var oldLoc = window.location.hash.replace(/^#\!/,"");
var m = /([\/^\/]+\/)([^\/]+\/)([^\/]+)/.exec(oldLoc);
if (oldLoc == '/account/') {
    $('#reload_section').load('account.php');
} else if (m[1] == '/search/') {
    if (m[2] == 'objects/') {
        currentQuery = 'objects';
    } else if (m[2] == 'files/') {
        currentQuery = 'files';
    }
}  

Thanks!!!


P.S. The reason I am asking so much in one thing is that the regex might break if I don't mention it.

Joe Torraca
  • 1,849
  • 5
  • 29
  • 49
  • It seems odd that you're trying to solve your problem in this way. Wouldn't it be better to have proper URL routing on the server-side and then just do a request to whatever comes after the `#!`? e.g. `/account/` instead of `/account.php`? – beerbajay Feb 21 '12 at 22:01
  • well, why does this seem weird? The url doesn't change to account.php, I am loading that in the background. – Joe Torraca Feb 21 '12 at 22:06
  • Because you're using ugly/old-timey URLs and trying to mask them in javascript. Your real URL is `account.php` but then you want it to look nice in the browser so you use `/account/`. What I'm saying is that you should **actually have** pretty URLs on the server, then you can just do `/#!(.*)$/.exec(url)[1]` to get the URL to fetch. – beerbajay Feb 21 '12 at 22:15
  • How would I go about server-side routing – Joe Torraca Feb 21 '12 at 22:47
  • You can either do this in `mod_rewrite` if you have a very limited number of views (see [this answer](http://stackoverflow.com/a/522513/320220)) or you can use a front controller of some sort; this depends on if you're already using a framework for other things, see the answers to [this question](http://stackoverflow.com/questions/115629/simplest-php-routing-framework). – beerbajay Feb 21 '12 at 23:01
  • The main reason I am doing this is because of Google's AJAX parsing. Does server-side routing get parsed by Google and other search engine's servers? – Joe Torraca Feb 22 '12 at 00:31
  • I don't know anything about how google deals with ajaxy pages; if google indexes pages on your server **not** via the ajax front-end, it doesn't matter if it's `/account/` or `/account.php`; they would both get indexed. – beerbajay Feb 22 '12 at 06:47

0 Answers0