2014-03-14 101 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='#'>