0

I found the snippet below for creating an iframe:

I'd like to only load the textual content at http://www.host.com and make the iframe invisible using display:none

Background:

I need an effective way to parse out the favicon location. Not all site have a default location. For example <link rel="SHORTCUT ICON" href="../images/logo_small.ico" />.

Hence all I need are the textual contents. PHP has a function that does this ( file_get_contents) but I want to do it on the client side.

Here is the mdn documentation on iframe

For server-side PHP use file_get_contents.

function makeFrame() {
   ifrm = document.createElement("IFRAME");
   ifrm.setAttribute("src", "http://www.host.com");
   ifrm.style.width = 640+"px";
   ifrm.style.height = 480+"px";
   document.body.appendChild(ifrm);
} 

Example of Delicious Bookmarklet:

javascript:(function()
{
    f='http://www.delicious.com/save?url='+encodeURIComponent(window.location.href)+
    '&title='+encodeURIComponent(document.title)+'&notes='+
    encodeURIComponent(''+
    (window.getSelection?window.getSelection():document.getSelection?
    document.getSelection():document.selection.createRange().text))+'&v=6&';
    a=function()
    {
        if(!window.open(f+'noui=1&jump=doclose','deliciousuiv6','location=1,links=0,scrollbars=0,to
        olbar=0,width=710,height=660'))
        {
            location.href=f+'jump=yes'
        }
    };
    if(/Firefox/.test(navigator.userAgent))
    {
        setTimeout(a,0);
    }
    else
    {
        a();
    }
})()
CS_2013
  • 1,158
  • 3
  • 13
  • 24

2 Answers2

1

You will only have access to the link element in the iframe when both yours and the framed page are in the same domain. To cite the docs you found:

Scripts trying to access a frame's content are subject to the same-origin policy, and cannot access most of the properties in the other window object if it was loaded from a different domain.

Therefore, it will be needed to use a PHP solution. Load a given domain, use a tagsoup parser and query the dom for link[rel="SHORTCUT ICON"].

You can create a JSON API with that solution, which can be used over Ajax from the clientside js code of your application.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • The page in the iframe would not have access to my DOM not the other way around. – CS_2013 May 15 '12 at 23:39
  • anyways this is how bookmarklets work...if a bookmarklet can do it...so can I...i would think – CS_2013 May 15 '12 at 23:57
  • Umm, bookmarklets don't have to do anything with that. The example from Delicous you've posted does not access any other window/frame. The bookmarklet code will run in the scope of the current window, extracts some data (here: selection) and opens a popup, the data encoded as URL parameters. – Bergi May 16 '12 at 00:08
  • Well the source in the bookmarklet can come from a different domain ( say delicious.com ) and work on another domain ( which ever domain you are in )... – CS_2013 May 16 '12 at 18:06
  • 1
    When you click on the bookmarklet, the JavaScript is executed with respect to the current page by the browser. It has the same origin as the page it is executed on. – Blender May 16 '12 at 18:19
0

I think you will struggle to get the information client side if the page is located on a different server, due to cross-site scripting restrictions within the browser.

If it is all local you want to look at:

XMLHttpRequest();

and

getResponseText();

I think you are probably best using the PHP method though.

Edit:

This is a similar question I think:

XMLHttpRequest to get HTTP response from remote host

Responses there may be of further assistance.

Community
  • 1
  • 1
Sharpedges
  • 405
  • 1
  • 4
  • 17