2014-09-13 70 views
0

書籤是一個地址爲JavaScript代碼的書籤。Javascript書籤將當前頁面的url發送給bing

我想獲取當前頁面的URL,並將其粘貼到Bing搜索頁面的文本框中。

我能得到的URL很輕鬆地:

javascript:(function(){var%20url=window.location.href;alert(url);})(); 

但後來如何設置Bing的頁面上的文本框,我的變量,url然後使搜索?

這不起作用:

javascript:(function(){var%20url=window.location.href;window.open%20("https://www.bing.com/search?q=&url");})(); 

回答

3

使用下面的書籤代碼:

javascript:{window.location='http://bing.com/search?q='+encodeURIComponent(window.location.href)} 
+0

謝謝。我嘗試使用相同的代碼爲另一個網站名稱'whoishostingthis'用下面的代碼:'javascript:{window.location ='http://www.whoishostingthis.com/search?q ='+ encodeURIComponent(window.location.href )}'但它失敗了。我知道我在同一個主題中提出另一個問題,但似乎密切相關。 – aquagremlin 2014-09-13 04:47:27

+0

@acquagremlin既然你已經問過,正確的代碼是這個'javascript:{window.location ='https://www.whoishostingthis.com/?q ='+ encodeURIComponent(window.location.href)}' – shantanuo 2018-01-23 02:24:42

0

當然,你可以做你上面所看到的方式。但是,我一直處於這種情況下,我想控制從應用程序中顯示的內容。

然後我決定從Bing API連接我的應用程序。好處是它是免費的,你不會把用戶帶離你的網站。

您將需要從Azure的市場廣場

這裏的API關鍵在於你可能想給它一個嘗試,可能是,在未來的代碼。

<html> 
<head> 
<title>BING API Integration</title> 
<SCRIPT type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
<script type="text/javascript"> 


$(function() { 
    $('#searchButton').click(function(e){ 
     $("#results").empty(); 
     var query=$('#searchTerm').val(); 
     if (query) { 
      serviceOp = "Web"; 
      search(query, serviceOp); 
     } 
    }); 
}); 

function search(query, serviceOp){ 


    // your account key that youw ill get from https://datamarket.azure.com 
    var acctKey = '<Your Key>'; 

    var rootUri = 'https://api.datamarket.azure.com/Bing/Search'; 

    var requestUri = rootUri + "/" + serviceOp + "?$format=json&Query='" + query + "'"; 


    $.ajax({ 
     type: "GET", 
     url: requestUri, 
     headers: { 
     "Authorization": "Basic " + window.btoa(acctKey + ":" + acctKey) 
     }, 

    }).done(function(o){ 

     if (o.d !== undefined){ 
       var items = o.d.results; 
        for(var idx=0, len= items.length; idx < len; idx++){ 
         var item = items[idx]; 
         switch(item.__metadata.type){ 
          case 'WebResult': 
          showWebResult(item); 
         } 
        } 
       } 
    }); 
} 

// Shows one item of Web result. 

function showWebResult(item) { 

    var p = document.createElement('p'); 
    var a = document.createElement('a'); 
    a.href = item.Url; 
    $(a).append(item.Title); 
    $(p).append(item.Description); 
    $('#results').append(a, p); 
} 
</script> 
</head> 
<body> 
    <label for="searchTerm">Search: </label> 
    <input id="searchTerm" type="text"/> 
    <button id="searchButton">Search</button> 

    <div id="results"> 
    </div> 
</body> 
</html> 
相關問題