duanqiao2006 2017-05-02 05:00
浏览 232

为什么iframe或对象不支持google / yahoo

Here i am using the code like

<iframe src="https://www.google.com" style="width: 90%; height: 300px"></iframe>
<object data="https://www.yahoo.com" style="width: 90%; height: 300px"></object>

It showing the error like

Load denied by X-Frame-Options: https://www.google.co.in/?gfe_rd=cr&ei=TRAIWY7jO-KK8Qek6aOwDA&gws_rd=ssl does not permit cross-origin framing.

How can i fix it and show these url's in my iframe or object

Thanks.

  • 写回答

1条回答 默认 最新

  • doutui6644 2017-05-02 05:08
    关注

    The X-Frame-Options HTTP response header can be used to indicate whether or not a browser should be allowed to render a page in a , or

    found: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Frame-Options

    why not explore a solution like this: How can I add a Google search box to my website?

    Look at this fiddle

    var iframe = document.getElementsByTagName('iframe')[0];
    var url = iframe.src;
    var getData = function (data) {
        if (data && data.query && data.query.results && data.query.results.resources && data.query.results.resources.content && data.query.results.resources.status == 200) loadHTML(data.query.results.resources.content);
        else if (data && data.error && data.error.description) loadHTML(data.error.description);
        else loadHTML('Error: Cannot load ' + url);
    };
    var loadURL = function (src) {
        url = src;
        var script = document.createElement('script');
        script.src = 'http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20data.headers%20where%20url%3D%22' + encodeURIComponent(url) + '%22&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=getData';
        document.body.appendChild(script);
    };
    var loadHTML = function (html) {
        iframe.src = 'about:blank';
        iframe.contentWindow.document.open();
        iframe.contentWindow.document.write(html.replace(/<head>/i, '<head><base href="' + url + '"><scr' + 'ipt>document.addEventListener("click", function(e) { if(e.target && e.target.nodeName == "A") { e.preventDefault(); parent.loadURL(e.target.href); } });</scr' + 'ipt>'));
        iframe.contentWindow.document.close();
    }
    
    loadURL(iframe.src);
    

    http://jsfiddle.net/2gou4yen/

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据