www.webdeveloper.com
Results 1 to 5 of 5

Thread: Building query strings with javascript

Hybrid View

  1. #1
    Join Date
    Apr 2011
    Posts
    8

    Building query strings with javascript

    I am wondering if it is possible to build a query string like:

    fq=model:book+model:cds+model:journal+model:new_member+model:member

    I want to use the below in the browser’s address bar:

    //localhost:8080/search/q=son&fq=model:book+model:cds+model:journal+model:new_member+model:member

    I don’t want to use the “=” (model=book) and “&” (model=book&model=cds) signs, but I need pair values (model:book) and the “+” sign (model:book+model:cds).

    Thank you in advance,
    Tom

  2. #2
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,671
    build from what?

  3. #3
    Join Date
    Apr 2011
    Posts
    8
    Hi,
    “model” is a parameter and “book” , “cds” etc it’s values. Unfortunately for me, this is the syntax that the search engine I have chosen is accepting

  4. #4
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,671
    what do you mean 'build'? do you have some list of these models/books/cds etc.? or you need just 1 string? is it a page with some fields to input all thiese params? you question is not clear

  5. #5
    Join Date
    Apr 2011
    Posts
    8
    Sorry for not been clear. But i got my solution:
    Code:
    <script type="text/javascript">
    
    var surl = "http://localhost:8080/solr/select/?q=";
    	var pre = "&fq=";
    		var value = "*:*";
    			var a = "member";
    				var b = "new_member";
    					var c = "book";
    						var d = "journal";
    							var e = "cds";
    								var f = "pdfs";
    					
    serialize = function(obj) {
      var str = [];
      for(var p in obj)
         str.push(encodeURIComponent(p) + ":" + encodeURIComponent(obj[p]));
      return str.join("+");
    }
    
    serialize_1 = function(obj, prefix) {
            var str = [];
            for(var p in obj) {
                var k = prefix ? prefix + "[" + p + "]" : p, v = obj[p];
                str.push(typeof v == "object" ? 
                    serialize(v, k) :
                    encodeURIComponent(k) + "=" + encodeURIComponent(v));
            }
            return str.join("+");
        }
    
    var enc = serialize_1([{model: a},{model: b},{model: c},{model: d},{model: e},{model: f}]);
    	var query = surl + value + pre + enc;
    
    console.log(query);
    
    </script>

Thread Information

Users Browsing this Thread

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

Tags for this Thread

Posting Permissions

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



Recent Articles