7

I have a problem with javascript. I use google api and it contains ajax. The problem here is that, I need to catch values from URL like http://examplesite.com/index.php?s=some+values . I need to search values automatically. I try to do this for along time. However, I couldn't. How can I do this ?

This is my submit form:

    <form id="searchForm" method="post">
  
    <fieldset style="width: 520; height: 68">
        
    <input id="s" type="text" name="s" />
          
    <input type="submit" value="Submit" id="submitButton" />

Here is my javascript codes:

$(document).ready(function(){
 
 var config = {
  siteURL  : 'stackoverflow.com', // Change this to your site
  searchSite : true,
  type  : 'web',
  append  : false,
  perPage  : 8,   // A maximum of 8 is allowed by Google
  page  : 0    // The start page
 }
 
 // The small arrow that marks the active search icon:
 var arrow = $('<span>',{className:'arrow'}).appendTo('ul.icons');
 
 $('ul.icons li').click(function(){
  var el = $(this);
  
  if(el.hasClass('active')){
   // The icon is already active, exit
   return false;
  }
  
  el.siblings().removeClass('active');
  el.addClass('active');
  
  // Move the arrow below this icon
  arrow.stop().animate({
   left  : el.position().left,
   marginLeft : (el.width()/2)-4
  });
  
  // Set the search type
  config.type = el.attr('data-searchType');
  $('#more').fadeOut();
 });
 
 // Adding the site domain as a label for the first radio button:
 $('#siteNameLabel').append(' '+config.siteURL);
 
 // Marking the Search tutorialzine.com radio as active:
 $('#searchSite').click(); 
 
 // Marking the web search icon as active:
 $('li.web').click();
 
 // Focusing the input text box:
 $('#s').focus();

 $('#searchForm').submit(function(){
  googleSearch();
  return false;
 });
 
 $('#searchSite,#searchWeb').change(function(){
  // Listening for a click on one of the radio buttons.
  // config.searchSite is either true or false.
  
  config.searchSite = this.id == 'searchSite';
 });
 
 
 function googleSearch(settings){
  
  // If no parameters are supplied to the function,
  // it takes its defaults from the config object above:
  
  settings = $.extend({},config,settings);
  settings.term = settings.term || $('#s').val();
  
  if(settings.searchSite){
   // Using the Google site:example.com to limit the search to a
   // specific domain:
   settings.term = 'site:'+settings.siteURL+' '+settings.term;
  }
  
  // URL of Google's AJAX search API
  var apiURL = 'http://ajax.googleapis.com/ajax/services/search/'+settings.type+'?v=1.0&callback=?';
  var resultsDiv = $('#resultsDiv');
  
  $.getJSON(apiURL,{q:settings.term,rsz:settings.perPage,start:settings.page*settings.perPage},function(r){
   
   var results = r.responseData.results;
   $('#more').remove();
   
   if(results.length){
    
    // If results were returned, add them to a pageContainer div,
    // after which append them to the #resultsDiv:
    
    var pageContainer = $('<div>',{className:'pageContainer'});
    
    for(var i=0;i<results.length;i++){
     // Creating a new result object and firing its toString method:
     pageContainer.append(new result(results[i]) + '');
    }
    
    if(!settings.append){
     // This is executed when running a new search, 
     // instead of clicking on the More button:
     resultsDiv.empty();
    }
    
    pageContainer.append('<div class="clear"></div>')
        .hide().appendTo(resultsDiv)
        .fadeIn('slow');
    
    var cursor = r.responseData.cursor;
    
    // Checking if there are more pages with results, 
    // and deciding whether to show the More button:
    
    if( +cursor.estimatedResultCount > (settings.page+1)*settings.perPage){
     $('<div>',{id:'more'}).appendTo(resultsDiv).click(function(){
      googleSearch({append:true,page:settings.page+1});
      $(this).fadeOut();
     });
    }
   }
   else {
    
    // No results were found for this search.
    
    resultsDiv.empty();
    $('<p>',{className:'notFound',html:'No Results Were Found!'}).hide().appendTo(resultsDiv).fadeIn();
   }
  });
 }
 
 function result(r){
  
  // This is class definition. Object of this class are created for
  // each result. The markup is generated by the .toString() method.
  
  var arr = [];
  
  // GsearchResultClass is passed by the google API
  switch(r.GsearchResultClass){

   case 'GwebSearch':
    arr = [
     '<div class="webResult">',
     '<h2><a href="',r.unescapedUrl,'" target="_blank">',r.title,'</a></h2>',
     '<p>',r.content,'</p>',
     '<a href="',r.unescapedUrl,'" target="_blank">',r.visibleUrl,'</a>',
     '</div>'
    ];
   
  }
  
  // The toString method.
  this.toString = function(){
   return arr.join('');
  }
 }
 
 
});
Captain Obvlious
  • 19,754
  • 5
  • 44
  • 74
  • What do you try to achieve? Do you want to read some get params? – Lajos Arpad Dec 25 '15 at 18:54
  • Yes , this is one of my objectives @LajosArpad –  Dec 25 '15 at 18:58
  • No need to, it is just Christmas, so most of us is not online. I am sneaking here every now and then as well. – Lajos Arpad Dec 25 '15 at 19:25
  • Thank you :) Merry Christmas @LajosArpad –  Dec 25 '15 at 19:35
  • Alperen, thank you, I have a great time. I have given you an answer, which does not deal with all your problems, as I do not understand them all yet, but I hope this answer will help you take some steps forwards. – Lajos Arpad Dec 25 '15 at 19:40

2 Answers2

1

Look at my answer here. As you can see, it is not too difficult to set a get parameter. Now, I will show you how you can get a get parameter:

function getGetParameter(paramName)
{
    var url = window.location.href;
    if (url.indexOf(paramName + "=") >= 0)
    {
        var returnValue = url.substring(url.indexOf(paramName + "="));
        if (returnValue.indexOf("&") >= 0)
        {
            returnValue = returnValue.substring(0, returnValue.indexOf("&"));
        }
        return returnValue.substring(returnValue.indexOf("=") + 1);
    }
    return null;
}

As about searching for values automatically, you need to specify what and how would you like to search for, as this can be needed/done literally in infinitely many ways.

Community
  • 1
  • 1
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
0

maybe this is the problem: you're trying to use an API and it's no longer avaiable.

Object {responseData: null, responseDetails: "This API is no longer available.", responseStatus: 403}

More information here: https://developers.google.com/image-search/v1/jsondevguide

Now, I'm trying to find a migration to version 2.

pablorsk
  • 3,861
  • 1
  • 32
  • 37