Asked  6 Months ago    Answers:  5   Viewed   54 times

Is there a simple way to get the current URL from an iframe?

The viewer would going through multiple sites. I'm guessing I would be using something in javascript.

 Answers

23

For security reasons, you can only get the url for as long as the contents of the iframe, and the referencing javascript, are served from the same domain. As long as that is true, something like this will work:

document.getElementById("iframe_id").contentWindow.location.href

If the two domains are mismatched, you'll run into cross site reference scripting security restrictions.

See also answers to a similar question.

Tuesday, June 1, 2021
 
Szenis
answered 6 Months ago
62

Hate to say it but I'm like 99% sure that ain't happening directly because of security.

You can try it out here.

bhh

Tuesday, June 1, 2021
 
Strae
answered 6 Months ago
84
Path.GetFileName( Request.Url.AbsolutePath )
Wednesday, August 11, 2021
 
muaddhib
answered 4 Months ago
78

If to use Selenium for web navigation:

from selenium import webdriver
driver = webdriver.Firefox()
print (driver.current_url)
Friday, August 13, 2021
 
Jesse
answered 4 Months ago
24

Had a little time at hand and tried a jQuery less variant. Doesn't seem to leak anymore according to SIEve.

function pos(obj) {
    var curleft = 0;
    var curtop = 0;
    if (obj.offsetParent) {
        do {
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
        } while (obj = obj.offsetParent);
    }
    obj = null;
    return {left:curleft, top:curtop};
}

function loadPage(url) {
    var y = document.getElementById('container');
    var x = document.getElementById('applicationFrame');
    var p = pos(y);
    if (x.src) {
        var tmp = y.cloneNode(false);
        var tmpIF = x.cloneNode(false);
        tmpIF.src = url;
        tmp.appendChild(tmpIF);
        tmp.style.position = 'absolute';
        tmp.style.visibility = 'hidden';
        tmp.style["z-index"] = 20;
        tmp.style.top = p.top;
        tmp.style.left = p.left;
        y.id = "delete";
        x.id = "deleteApplicationFrame";
        document.getElementsByTagName("body")[0].appendChild(tmp);
        tmpIF = null; tmp = null;
    }
    setTimeout("document.getElementById('applicationFrame').style.visibility = 'visible'; var i = document.getElementById('deleteApplicationFrame'); i = i.contentDocument || i.contentWindow.document; i.documentElement.parentNode.removeChild(i.documentElement); i=null; i=document.getElementById('delete'); i.parentNode.removeChild(i); i=null;", 500);
    y = null; x = null; p = null;
}

<div id="container">
    <iframe id="applicationFrame" application="yes" trusted="yes" frameborder="0" src="main.php"/>
</div>

Very hard to tell what could possibly be going on without knowing the whole application. Especially IE6 is a b..ch with memory leaking.

A few reading links

Understanding and Solving Internet Explorer Leak Patterns

Fixing Leaks

Memory Leakage in Internet Explorer - revisited


Just a thought, AFAIK the behavior when setting src to a different value isn't really specified in the W3C HTML DOM specification or is it (link anyone?)?

I suggest that you set an initial src="main.php" value on the iframe instead of using loadPage('main.php'); and set a name for the iframe.

Ideally your menu items are <a> tags then you could test using <a href="notmain.php" target="nameOfYourIFrame">FooBar</a> instead of the javascript based solution

Friday, September 17, 2021
 
Manmay
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share