user1855017
user1855017

Reputation: 41

How to get iFrame info (referring URL) through JavaScript?

How can we access the iFrame info (right click on an iFrame in the browser -> This Frame -> View frame info)? Mainly I want to get the referring URL.

I was able to get the address by contentDocument.location.href but I can't get the other attributes (type, render mode, referring URL, ...).

By the way, document.referrer is not the thing I am searching for.

Upvotes: 1

Views: 274

Answers (2)

J.Wells
J.Wells

Reputation: 1759

I didn't write this and do not recall where it came from, but it's dead useful and it's a snippet that I always keep around:

        function parseUri(str) {
            var o = parseUri.options,
                m = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
                uri = {},
                i = 14;

            while (i--) uri[o.key[i]] = m[i] || "";

            uri[o.q.name] = {};
            uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
                if ($1) uri[o.q.name][$1] = $2;
            });

            return uri;
        };

        parseUri.options = {
            strictMode: false,
            key: ["source", "protocol", "authority", "userInfo", "user", "password", "host", "port", "relative", "path", "directory", "file", "query", "anchor"],
            q: {
                name: "queryKey",
                parser: /(?:^|&)([^&=]*)=?([^&]*)/g
            },
            parser: {
                strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
                loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
            }
        };

Then use it like this:

       parseUri(document.referrer).host //<-- gets host name

Upvotes: 0

mbelow
mbelow

Reputation: 1133

Is the host page and the iframe on the same domain? Since "cross-frame-communication" is heavily restricted in all modern browsers, I think there is no way of accomplishing this.

Upvotes: 1

Related Questions