/    Sign up×
Community /Pin to ProfileBookmark

Return current iframe src??

Hi all

I’m sure I’ve done this before, but seem unable to today. I want to return the current src (location) of an iframe that’s on my page. I’ve tried a mixture of elements, including calling the src as a property, and also using location.pathname. Have also tried calling it FROM the iframe, i.e. onMouseOver, alert(this.src); etc.

Any offers? Cheers.

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@sciguyryanJul 25.2004 — Try:

<i>
</i>&lt;script type="text/javascript"&gt;
&lt;!--
var TheElm = document.getElementById("YourIframesID");
var TheSrc = TheElm.src;
//--&gt;
&lt;/script&gt;
Copy linkTweet thisAlerts:
@mityaauthorJul 25.2004 — Still blank I'm afraid, thanks for trying guys.
Copy linkTweet thisAlerts:
@CharlesJul 25.2004 — [i]Originally posted by sciguyryan [/i]

[B]Try:



<i>
</i>&lt;script type="text/javascript"&gt;
&lt;!--
var TheElm = document.getElementById("YourIframesID");
var TheSrc = TheElm.src;
//--&gt;
&lt;/script&gt;
[/B][/QUOTE]
That'll give you the value of the "src" attribute but it won't give you the URL of the document if the user has followed any links in the frame. And I suspect that's what mitya is after. Something more like this, perhaps:

[font=monospace]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

"http://www.w3.org/TR/html4/loose.dtd">

<html lang="en">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<meta name="Content-Script-Type" content="text/javascript">

<title>Example</title>

</head>

<body>

<iframe id="myFrame" src="http://www.w3.org/"><a href="http://www.w3.org/">W3C</a></iframe>

<button onclick="alert(myFrame.location)">&amp;nbsp;</button>

</body>

</html>[/font]

Except that we are subject to the usual JavaScript access restrictions. We can never know anything about somebody else's page.
Copy linkTweet thisAlerts:
@Willy_DuittJul 26.2004 — Try using [b]href[/b] as opposed to [b]src[/b]....

[b]IE:[/b]

<script type="text/javascript">

<!--

var TheElm = document.getElementById("YourIframesID");

var TheSrc = TheElm.[color=red]href[/color];

//-->

</script>

[b]Or:[/b] document.frames['YourIframesID'].href

.....Willy
Copy linkTweet thisAlerts:
@mityaauthorJul 26.2004 — All return undefined or with errors I'm afraid, thanks anyway guys.
Copy linkTweet thisAlerts:
@sciguyryanJul 26.2004 — Have you dynamically loaded the URL? If so javascript can not retrieve it.
Copy linkTweet thisAlerts:
@CharlesJul 26.2004 — [i]Originally posted by sciguyryan [/i]

[B]Have you dynamically loaded the URL? If so javascript can not retrieve it. [/B][/QUOTE]
It can, but not across domains.
Copy linkTweet thisAlerts:
@mityaauthorJul 26.2004 — Have cracked it. It comes down to javascript's often inconsistent referencing system (from my point of view as a relative newcomer to js).

Anything in the form of document.getElementById(... will fail.

However, document.frames['ifr'].location works.

Thanks for trying everyone.
×

Success!

Help @mitya spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...