2015-12-25 17 views
6

JavaScriptに問題があります。私はGoogleのAPIを使用し、それはajaxが含まれています。ここの問題は、http://examplesite.com/index.php?s=some+valuesのようなURLから値をキャッチする必要があることです。私は値を自動的に検索する必要があります。私はこれをやっていくつもりです。しかし、私はできませんでした。これどうやってするの ?私の答えで自動フォーム投稿機能付きURL

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

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

 
\t \t \t case 'GwebSearch': 
 
\t \t \t \t arr = [ 
 
\t \t \t \t \t '<div class="webResult">', 
 
\t \t \t \t \t '<h2><a href="',r.unescapedUrl,'" target="_blank">',r.title,'</a></h2>', 
 
\t \t \t \t \t '<p>',r.content,'</p>', 
 
\t \t \t \t \t '<a href="',r.unescapedUrl,'" target="_blank">',r.visibleUrl,'</a>', 
 
\t \t \t \t \t '</div>' 
 
\t \t \t \t ]; 
 
\t \t \t 
 
\t \t } 
 
\t \t 
 
\t \t // The toString method. 
 
\t \t this.toString = function(){ 
 
\t \t \t return arr.join(''); 
 
\t \t } 
 
\t } 
 
\t 
 
\t 
 
});

+0

何を達成しようとしないバージョンへの移行を見つけようとしていますか?あなたはいくつかのパラメータを取得したいですか? –

+0

はい、これは私の目的の一つです@LajosArpad –

+0

それはちょうどクリスマスなので、私たちのほとんどはオンラインではありません。私は今も毎回ここに潜入しています。 –

答えて

1

ルック:ここ

<form id="searchForm" method="post"> 
 
\t \t 
 
    <fieldset style="width: 520; height: 68"> 
 
     
 
    <input id="s" type="text" name="s" /> 
 
      
 
    <input type="submit" value="Submit" id="submitButton" />
は私のjavascriptのコードです:

これは私の提出フォームです。ご覧のとおり、getパラメータを設定することはあまり難しくありません。自動的に値を検索について、あなたはこれを文字通りに行う/必要になることができるようあなたは、を検索したいものを、どのように指定する必要があるとして

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; 
} 

:今、私はあなたがGETパラメータを取得する方法を紹介します無限に多くの方法で。

0

おそらくこれは問題です。あなたはAPIを使用しようとしており、もはや利用できません。ここ

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

詳細情報:今すぐhttps://developers.google.com/image-search/v1/jsondevguide

は、私は2

関連する問題