2014-03-14 96 views
0

有沒有人知道會檢測頁面的動態url(?q = Chicken)的JavaScript代碼,並將頁面上的iframe的url設置爲https://www.google.co.uk/?#q=Chicken。 (我實際上沒有製作谷歌搜索客戶端!)設置基於動態URL的iframe url

Get - Text added to Url 
    set 
    Url of iframe to https://www.google.co.uk/?#q=Chicken 

這將全部上傳。

+0

什麼服務器端語言使用的是? –

+0

您可以獲取動態url,從'?'做一個子字符串並獲取參數。 –

回答

1

你需要的東西是這樣的:

<script> 
function get_url_parameter(name) { 
    return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[,""])[1].replace(/\+/g, '%20'))||null 
} 

document.addEventListener('DOMContentLoaded', doc_loaded, false); 
function doc_loaded() { 
    //var url = 'https://www.google.co.uk/?#q=' + get_url_parameter('q'); 
    var url = 'http://example.com/?#q=' + get_url_parameter('q'); 
    document.getElementById('iframe_id').src = url; 
} 
</script> 
<iframe src="#" id="iframe_id" width="1000" height="500"></iframe> 

,而是因爲它發送一個「X-框選項:SAMEORIGIN」它不會爲谷歌工作的響應頭:(

+0

爲我完美工作。非常感謝! – JBithell

+0

如果沒有任何東西添加到頁面的網址,有沒有什麼辦法可以將iframe設置爲不同的url? (如果它爲null,則將其設置爲谷歌主頁) – JBithell

+1

你可以這樣做: if(get_url_parameter('q')== null){ var url ='http:// the-url-that-你需要/';其他{ var url ='http://example.com/?#q='+ get_url_parameter('q'); } –

1

試試這個,網址瀏覽「http://something.com/index.asp?search=anything

<script type="text/javascript"> 

    $(document).ready(function() 
    { 

    function getParameterByName(name) { 
     name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]"); 
     var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"), 
      results = regex.exec(location.search); 
     return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " ")); 
    } 

    var urliframe = "http://www.google.co.uk/?#q="+getParameterByName('search'); 

    $('#myIframe').attr('href',urliframe); 

    }); 

    </script> 

    <iframe id='myIframe' href='#'>