2015-12-25 4 views
6

के साथ ऑटो फॉर्म पोस्ट मुझे जावास्क्रिप्ट के साथ कोई समस्या है। मैं google api का उपयोग करता हूं और इसमें AJAX शामिल है। यहां समस्या यह है कि, मुझे http://examplesite.com/index.php?s=some+values जैसे URL से मूल्यों को पकड़ने की आवश्यकता है। मुझे मूल्यों को स्वचालित रूप से खोजना होगा। मैं समय के साथ ऐसा करने की कोशिश करता हूं। हालांकि, मैं नहीं कर सका। मैं यह कैसे कर सकता हूँ ? मेरा उत्तर 012,375 मेंयूआरएल फ़ंक्शन

$(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" />

यहाँ मेरी जावास्क्रिप्ट कोड है:

यह मेरा प्रस्तुत रूप है। जैसा कि आप देख सकते हैं, प्राप्त पैरामीटर सेट करना बहुत मुश्किल नहीं है। अब, मैं आप कैसे आप एक प्राप्त पैरामीटर प्राप्त कर सकते हैं दिखाएगा:

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

स्वचालित रूप से मूल्यों के लिए खोज के बारे में के रूप में, आप क्या और कैसे आप के लिए खोज करने के लिए, के रूप में यह आवश्यक हो सकता है/सचमुच किया चाहते हैं निर्दिष्ट करने की आवश्यकता अनगिनत कई तरीकों से।

0

शायद यह समस्या है: आप एक एपीआई का उपयोग करने की कोशिश कर रहे हैं और यह अब उपलब्ध नहीं है।

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

अधिक यहाँ जानकारी: https://developers.google.com/image-search/v1/jsondevguide

अब, मैं संस्करण के लिए एक प्रवास 2.

संबंधित मुद्दे