views:

178

answers:

2

Hi all,

I have got an iframe that displays a form from an external site,once the form is submitted it is redirected to another page that has got a thankyou message.Is it posiible to know from the iframe if the location of src webpage has changed? i hope im making some sense...

i got this code which will do the job for me from some website,its working while im in their website but when i try to do it locally im getting Access Denied javascript error....

<html>
<body>
<script language="JavaScript">
    function myLocation() {
        alert(document.all.myFrame.contentWindow.location);
    }
</script>
<iframe id="myFrame" src="http://www.java2s.com" style="width:200;">
</iframe>
<br>
<button onclick="myLocation();">Location of Frame</button>
</body>
</html>

thank you

+1  A: 

You may want to use the onLoad event, as in the following example:

<iframe src="http://www.google.com/" onLoad="alert('Test');"></iframe>

The alert will pop-up whenever the location within the iframe has changed. The only problem with this technique is that it may not work with some older browsers like IE5 and early Opera. (Source)

UPDATE:

Further to your edit, you are getting the "Access Denied" error because you cannot access the contentWindow.location of a website that is not within the same domain of the parent window. While this is unfortunate for your genuine requirement, this is considered a security restriction to prevent cross-site scripting.

Daniel Vassallo
yeah thanks mate,i just found that out after some googling.thanx for ur comments though...is there a wrk around?what im trying to do is to enable a disabled button when the url changes.it doesnt necessarily have to be the url,i just want see if the anything has changed in the iframe...
manraj82
@manraj82: Would it be possible to use the onLoad method described above? It will fire whenever anything changes in the iFrame, and will work even with cross-domain content, because it does not reveal anything about the site in the iframe.
Daniel Vassallo
thanks a lot man,the onLoad method worked....i got it working by storing a counter value in a hidden field and then made the button enabled based on the counter value in the hidden field...
manraj82
Daniel Vassalo:could you pls chk my code and see if its ok?
manraj82
The code looks good. Keep in mind that the onLoad event will fire whenever the iframe body is reloaded, so for example it will fire again if the user right clicks within the iframe and chooses 'This Frame->Reload Frame' (in Firefox). This may or may not be acceptable, depending on how critical such an issue might be. In addition, this event may not fire in older browsers from the IE5 era.
Daniel Vassallo
A: 
function chkCounter(counterValue) {
       var tmp=document.getElementById('txtCounter').value;
       document.getElementById('txtCounter').value=tmp+counterValue;

       if(document.getElementById('txtCounter').value>1)
       {
        document.getElementById("btnClose").disabled = false;
       }
    }

<input align="middle" type="button" onClick="self.close();" value="CLOSE" disabled="true" id="btnClose">
<input type="hidden" id="txtCounter">
manraj82