Closed Thread
Results 1 to 8 of 8

Thread: Change querystring with Javascript

  1. #1
    CygnetGames's Avatar
    CygnetGames is offline Programmer
    Join Date
    May 2007
    Location
    York, England
    Posts
    119
    Rep Power
    0

    Change querystring with Javascript

    I'm making an ajax search page. I've got all the ajax working, but there's a usability issue with the query string.

    In my setup, the user types a search query and clicks a button. Then javascript fetches the results via ajax and injects them into the page. All good.

    The problem is - I want the user to be able to bookmark their search, or email the url to a friend, etc. With a naive way of doing ajax, they won't be able to do this as the url of the page doesn't change when a search is made.

    So what I want to be able to do is to alter the query string of the url using javascript without reloading the page. Is this possible?

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    fahlyn's Avatar
    fahlyn is offline Learning Programmer
    Join Date
    Nov 2007
    Posts
    35
    Rep Power
    0

    Re: Change querystring with Javascript

    I don't believe that is possible. I don't think you'd want to save the actual URL you're using for your ajax request anyway....would you? Wouldn't that either be XML, JSON or just an HTML snippet, and not your entire page? Why don't you add an optional argument to your page....like http://www.domain.com/search/?crit=s...teriaHere...or something....then have a link or button that says Bookmark these results or send these results to a friend...and send them the link above so you load your entire page, and not just the results from your ajax call. Just change searchCriteriaHere each time they do a different search.

    Does that make sense?
    Visit My Google Group Here: Web Development Innovation

  4. #3
    CygnetGames's Avatar
    CygnetGames is offline Programmer
    Join Date
    May 2007
    Location
    York, England
    Posts
    119
    Rep Power
    0

    Re: Change querystring with Javascript

    Thanks for the reply.
    I've got half of what you are saying already. I have a parameter so that if someone visits the url http://www.domain.com/search/?crit=searchCriteria, then the search will be performed.
    What I want is, if they visit the url http://www.domain.com/search/, enter their criteria and click on the button, the url will change to http://www.domain.com/search/?crit=searchCriteria but the page will not reload. The search will be performed using ajax.

    Anything I've tried with javascript (like assigning to document.location.search) makes the page reload, which is what I don't want to happen.

  5. #4
    fahlyn's Avatar
    fahlyn is offline Learning Programmer
    Join Date
    Nov 2007
    Posts
    35
    Rep Power
    0

    Re: Change querystring with Javascript

    When you change document.location the browser is going to navigate to that URL. You could create a text box for the "Search Location" that has the URL you want so the user can copy it...other than that...unless you want the page to refresh, i think you're out of luck.
    Visit My Google Group Here: Web Development Innovation

  6. #5
    CygnetGames's Avatar
    CygnetGames is offline Programmer
    Join Date
    May 2007
    Location
    York, England
    Posts
    119
    Rep Power
    0

    Re: Change querystring with Javascript

    I've come up with a solution now.
    I'm using (not sure what it's called, but...) the part of the url after the # character.
    So instead of visiting http://www.domain.com/search/?crit=searchCriteria, the user visits http://www.domain.com/search/#crit=searchCriteria.
    Javascript can change document.location without the page reloading if only the part after the # has been changed. Then I use javascript to parse the "query string" after the # and do the relevant ajax.

  7. #6
    fahlyn's Avatar
    fahlyn is offline Learning Programmer
    Join Date
    Nov 2007
    Posts
    35
    Rep Power
    0

    Re: Change querystring with Javascript

    Interesting. I believe that's called an anchor link. Usually, that is used to link from one section of an HTML document to another via the "name" attribute of the "a" tag.

    Good deal, I'm glad you were able to figure something out. So do you have some on page load event that triggers to parse the query string and do your Ajax search?

    One thing that you may consider depending on your site's traffic is efficiency. When a user clicks the link with the anchor link info in it for your ajax search you will hit the server and do the work for the initial page load, then your javascript will automatically hit the server again to perform your search. Just something you may want to keep in mind that each one of those page loads will result in (a minimum of) 2 round trips.
    Visit My Google Group Here: Web Development Innovation

  8. #7
    CygnetGames's Avatar
    CygnetGames is offline Programmer
    Join Date
    May 2007
    Location
    York, England
    Posts
    119
    Rep Power
    0

    Re: Change querystring with Javascript

    Quote Originally Posted by fahlyn View Post
    Interesting. I believe that's called an anchor link.
    Cheers - I was wondering what it was called.

    Quote Originally Posted by fahlyn View Post
    Just something you may want to keep in mind that each one of those page loads will result in (a minimum of) 2 round trips.
    Yeah, that's not a worry for my application though.
    The main reason for using the ajax is to make sure that the rest of the page is loaded before the search is initiated.

  9. #8
    tsalvad is offline Newbie
    Join Date
    Aug 2009
    Posts
    1
    Rep Power
    0

    Re: Change querystring with Javascript

    Hello CygnetGames, can u please post an example, i'm just trying to do that, thxn!

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Change value of several ids
    By Bertan in forum Database & Database Programming
    Replies: 2
    Last Post: 10-19-2010, 11:35 AM
  2. how to pass special character in a querystring in asp.net
    By Sanu in forum ASP, ASP.NET and Coldfusion
    Replies: 1
    Last Post: 04-27-2010, 04:47 AM
  3. JavaScript:Tutorial, MouseOver Image Change
    By TcM in forum JavaScript Tutorials
    Replies: 4
    Last Post: 06-06-2009, 06:49 AM
  4. Problem with a querystring
    By Pepozzo in forum PHP Development
    Replies: 2
    Last Post: 04-09-2009, 08:52 AM
  5. Change Resolution, Change Size
    By Void in forum C# Programming
    Replies: 1
    Last Post: 06-13-2006, 12:48 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts