Click here to Skip to main content
15,889,200 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am having a simple HTML page, inside which i have an iFrame. below is the code snippet for this.

<html>
<head></head>
<body>
<input id="currentSrc" type="text" value="" />
<iframe id="myframe" src="http://www.bing.com">
<p>your browser doesn't support iframes
</p></iframe>
</body>
</html>


now what i want is, that when ever user clicks or does any activity inside the iframe which causes the frame to load a new page then the text box "currentSrc" should get populated with current url of the page inside iframe.

i have tried getting the url with $("myframe").attr('src') but it always returns "bing.com"... After googling i found two properties like contentWindow and contentDocument which even though contain proper urls but gives "Permission denied" exception because of cross domain access. I wonder if there is any way may be using jquery, json or jsonp to get a work around this.

Please help, any guidance will be highly appreciated. Thanks in advance.
Posted

1 solution

The property "src" does not change when you change location and reload the frame content (so I confirm your problem). Instead, try to use contentDocument of the frame. You need to handle the frame's event "onload".

—SA
 
Share this answer
 
v2
Comments
SujayC 20-Jun-11 3:25am    
Thanks for the prompt response. As i mentioned above i tried using contentDocumnet and contentWindow both but they only work when page inside iframe belongs to same domain of the parnet page (that's what cross domain policy of browser says). So my requirement here is something like say for example, the iframe first defaults with "bing.com" then user search something inside that iframe, say like "jQuery", after getting the list of website links of other domain user clicks on one of them, now i would like to know which link user has currently clicked and would like to populate the text box with that url.... I completely agree that i need to do this with onload event of iframe, but i believe the same can be acheived with $(document).ready(function(){ }); as well inside iframe. But i am unable to find a way to trace the url of currently loaded page as contentDocument.href is giving "Permission Denied" exception !!!
Sergey Alexandrovich Kryukov 20-Jun-11 3:53am    
"Permission denied"? Interesting... never knew it happens. Needs some more research. Maybe someone else already faced with that.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900