PDA

Click to See Complete Forum and Search --> : location.href of iframe contents



MyFriendIsATaco
12-30-2006, 05:39 AM
now this is really weird:
<html>
<head>
<title>test</title>
</head>

<body>
<iframe name="test_frame" width="600" height="300"></iframe><br />
<input type="button" value="change iframe" onclick="test_frame.location.href='http://google.com';" />
<input type="button" value="check href" onclick="alert(test_frame.location.href);" />
</body>
</html>using that code, if you click the "check href" button first, you get an alert that says "about:blank" like it should, but if you click "change iframe" and allow google to load into the iframe, then click "check href", NOTHING. no alert, no nothing. How can i retrieve the location from the iframe after a page has been loaded into it?

This is confusing me because I've never really taken the time to learn javascript, and i guess this is why! :yikes:

catbert303
12-30-2006, 04:03 PM
I think it's possible location.href is not allowed to be read for security reasons. Although it seems that if you work with the iframe using it's src attribute things will work. Here's a quick example,



<html>
<head>
<script type="text/javascript">

function getSrc() {
return document.getElementById('test-frame').src;
}

function setSrc(value) {
document.getElementById('test-frame').src = value;
}

</script>

</head>
<body>

<iframe id="test-frame" src="about:blank" width="500" height="400"> </iframe>

<a href="javascript:alert(getSrc());">getSrc</a>
<a href="javascript:setSrc('http://www.google.com');">setSrc('http://www.google.com')</a>

</body>
</html>

MyFriendIsATaco
12-31-2006, 12:45 AM
yeah, i actually stumbled upon reading the src attribute. but then, my ultimate goal is to have a form post variables into a page which is loaded in the iframe. the page inside the frame evaluates the vars that were posted, then it kicks to a different page depending on what the variables were. then i wanted to grab the new href of the page, and do something based on what it was. but, im thinking its not going to happen. :) oh well, thanks anyways!