|
 |
|
Dev Articles Community Forums
> Programming
> Programming Tools
|
How do I get the URL of an iframe
Discuss How do I get the URL of an iframe in the Programming Tools forum on Dev Articles. How do I get the URL of an iframe Programming Tools forum for discussing any topics that don't fit into the programming categories above. Find information on tools and utilities to increase any programmer's productivity.
|
|
 |
|
|
|
|

Dev Articles Community Forums Sponsor:
|
|
|

January 7th, 2005, 03:37 AM
|
|
Registered User
|
|
Join Date: Jan 2005
Posts: 3
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
How do I get the URL of an iframe
if I use the getURL() command, i get the source of the current page, right?
Now I want to get the source of an iframe...
can someone help me?
thanks!
|

January 7th, 2005, 09:37 AM
|
 |
I'm Internet Famous
|
|
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,886
 
Time spent in forums: 1 Week 16 h 19 m 35 sec
Reputation Power: 13
|
|
Here's an example I just wrote up to get/set the source of an IFRAME... It uses DOM...
Its may me complicated for what it's worth, but all you really need to do is one of the following:
window.frames['my_frame_name'].src
document.getElementById('my_frame_id').src
Depends on your code... notice the difference between name and Id...
My example uses the second example above...
Code:
<?xml version="1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Changing IFRAME Src</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script type="text/javascript">
<!--
function setSrc(obj, url) {
obj.src = url;
}
function getSrc(obj) {
alert(obj.src);
return obj.src;
}
//-->
</script>
</head>
<body>
<h1>Changing IFRAME src</h1>
<p><a href="#" onclick="javascript:setSrc(document.getElementById('myframe'),'new.htm ')">Change src</a> <b>|</b>
<a href="#" onclick="javascript:getSrc(document.getElementById('myframe'))">Get src</a></p>
<iframe id="myframe" src="old.htm"></iframe>
</body>
</html>
I've highlighted the key points of the code.
|

February 16th, 2006, 12:54 AM
|
|
Registered User
|
|
Join Date: Feb 2006
Posts: 1
Time spent in forums: 21 m 25 sec
Reputation Power: 0
|
|
|
hi,
Can you show me how to use the first example? When I use the second one (document.getElementById..), even after my page URL has change , the value is still old URL. I tyr to use frames[iframe_name].location.href;
but i get permission denied.
Please help.
|

March 15th, 2006, 02:14 PM
|
|
Registered User
|
|
Join Date: Mar 2006
Posts: 1
Time spent in forums: 17 m 44 sec
Reputation Power: 0
|
|
|
I have exactly the same question. I have been able to get the original (old) src url with:
alert(document.getElementById('I1').src);
but this does not change as visitors click on navigation links within the iframe.
From another angle, the urls are present in the history array and available with back and forward, but can't seem to display the actual iframe url address text - must be a security thing (probably good).
The iframe onload event does fire, but likewise, can't seem to get anything useful here.
I don't like it, but I haven't seen any discussion that we can access the current url for an iframe. Wonder if this is a general issue with frames or just iframe. I'm going to try to do an example with frames instead of iframes and see if the same issue exists. Any one try this?
|

March 22nd, 2006, 11:34 AM
|
|
Registered User
|
|
Join Date: Mar 2006
Posts: 1
Time spent in forums: 3 m
Reputation Power: 0
|
|
|
I'm also having this exact problem. Does anyone have a clue what I can do?
|

May 11th, 2009, 01:26 PM
|
|
Registered User
|
|
Join Date: May 2009
Posts: 1
Time spent in forums: 2 m 31 sec
Reputation Power: 0
|
|
|
var CurrentUrl = document.getElementById('MyIFrame').contentWindow. location.href;
|

December 1st, 2011, 09:04 AM
|
|
Registered User
|
|
Join Date: Dec 2011
Posts: 1
Time spent in forums: 8 m 24 sec
Reputation Power: 0
|
|
|
Permission denied
Quote: | Originally Posted by rowsell var CurrentUrl = document.getElementById('MyIFrame').contentWindow. location.href; |
I tried the above but get an error -
"Microsoft JScript runtime error: Permission denied"
So although you can navigate - back and forward - it looks like you cannot access the URL.
I suppose it’s for security but if you open in a normal browser window you can get the url - so I don't see the point in hiding/protecting it? -
 anyone?
|

December 28th, 2012, 10:29 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 2
Time spent in forums: 16 m 32 sec
Reputation Power: 0
|
|
Thanks for sharing...! 
|

February 18th, 2013, 06:40 AM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 14
Time spent in forums: 3 h 5 m 16 sec
Reputation Power: 0
|
|
|
You can try this code:
<html>
<head>
<script type="text/javascript">
<!--
function setSrc(obj, url) {
obj.src = url;
}
function getSrc(obj) {
alert(obj.src);
return obj.src;
}
//-->
// above is the code to get the url from your frame
// now we place that url into the input box:
document.getElementById("frameid").value = myframe; // with frameURL defined earlier
</script>
</head>
<body>
<p><b>|</b>
<a href="#" onclick="javascript:getSrc(document.getElementById('myframe '))">Get src</a> - displays with alert</p>
<iframe id="myframe" src="http://www.google.com"></iframe>
<br>
<br>
<input Id="frameid"><p>frameurl not showing in input</p>
</body>
</html>
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|