1

I use ajax-solr as a front-end for Solr search. In order to get the search results, ajax-solr uses jQuery.getJSON() to GET a specific URL from the server. The relevant code is here:

AjaxSolr.Manager = AjaxSolr.AbstractManager.extend(
  /** @lends AjaxSolr.Manager.prototype */
  {
  executeRequest: function (servlet) {
    var self = this;
    if (this.proxyUrl) {
      jQuery.post(this.proxyUrl, { query: this.store.string() }, function (data) { self.handleResponse(data); }, 'json');
    }
    else {
      jQuery.getJSON(this.solrUrl + servlet + '?' + this.store.string() + '&wt=json&json.wrf=?', {}, function (data) { self.handleResponse(data); });
    }
  }
});

The problem is that the URL shown in the browser address bar doesn't change no matter what the GET URL is.

I want to allow users to share the search URL over email or twitter. So I need to make the URL loaded by jQuery.getJSON() visible in the browser address bar. Is there any way to do that?

Thanks.

Continuation
  • 12,722
  • 20
  • 82
  • 106

5 Answers5

1

It is possible, but you have to get a little more complicated. Basically, you have to add something past the hashtag, like this:

www.mysite.com/searchWithAjax#customData

Then, on your page load, you can read what's after the # and perform an ajax search with that information. To access what's after the #, just use location.hash: http://www.w3schools.com/jsref/prop_loc_hash.asp

Unfortunately, location.hash is a little hard to do in an anonymous jsFiddle. Check out this guy's example, I think it's almost exactly what you need and you can get the code by viewing the source:

http://jsfiddle.net/cowboy/CukUH/show/

And a very old writeup of the basic technique:

http://aspektas.com/blog/using-the-location-hash-with-ajax/

Milimetric
  • 13,411
  • 4
  • 44
  • 56
  • how do I add the hashtag to the URL? Right now ajax-solr "composes" the URL from different components and then send it using jQuery.getJSON(). Do I still use the same mechanism but just add the hashtag in the middle of my URL? Or do I need to do something completely different? – Continuation Apr 19 '11 at 20:45
  • I'll make you a jsfiddle and post it, hang on a minute. – Milimetric Apr 19 '11 at 20:46
  • Great. Really appreciate your help! – Continuation Apr 19 '11 at 20:49
1

It sounds a like you are wanting the html5 history api and history.pushState(). There is a good writeup at http://diveintohtml5.ep.io/history.html. It is the future of all that hashbang nonsense that developers are suppose to loathe but end up using because up until now we haven't had pushstate.

You should be able to change the url doing something like this. window.history.pushState(data, "Title", "/new-url"); Unfortunately, IE9 doesn't support it yet.

For something more cross browser I found this on stackoverflow - Cross-browser jquery ajax history with window.history.pushState and fallback

Community
  • 1
  • 1
LeRoy
  • 3,195
  • 4
  • 26
  • 23
0

This should be an ajax-solr question. The same framework allows this with the ParameterHashStore.

juan
  • 1,917
  • 2
  • 16
  • 14
0

It is not possible, since this works using xmlhttprequest (ajax) object.

So the request is made in the background, without user notice.

You can make a little button/something with the text: link to this search, or something like that, to allow users to get a direct link to this search, like google maps does.

jcarlosn
  • 398
  • 1
  • 4
0

Browser's address bar shows only address of current webpage or text inserted by user. You cannot change it by scripts unless you will change location.href.

pepkin88
  • 2,742
  • 20
  • 19
  • So if I change location.href then I can change the address bar? How do I do that? thanks. – Continuation Apr 19 '11 at 20:35
  • good thought, but wrong, you can use location.hash. You can't change location.href without forcing the browser to reload (defeating the purpose of ajax). – Milimetric Apr 19 '11 at 20:36
  • @Continuation You simply go on another page, just like clicking on a link. Address bar is reflection of real address of page, that is why you can for example verify if page comes from trusted source or not. It is for security reasons. – pepkin88 Apr 19 '11 at 20:38
  • @Milimetric That is correct, I wanted to show that it is in fact possible to change value of address bar, but worthless. – pepkin88 Apr 19 '11 at 20:40