1

I have this code which sucessfully tracks the url as they change in the iframe:

<html>
<body>
<iframe src="start.php"></iframe>
<a href="#" onclick="alert(frames[0].location);">Get Link.</a>
</body>
</html>

When 'Get Link' is clicked on it shows the right link. When the user navigates inside the iframe to another page (on the same server), and 'Get Link' is clicked again, it displays the current iframe url.

But I need to get the iframe URL without having to 'click' something.

When I do this:

<html>
<body>
<iframe src="start.php"></iframe>
<script>alert(frames[0].location)</script>
</body>
</html>

The alert message says "about:blank"

How can I get the iframe location using 'frames[0].location' passed into a variable that can be read by the parent page without having to use the onclick function?

user3697638
  • 83
  • 1
  • 1
  • 7
  • Possible duplicate of: http://stackoverflow.com/questions/938180/get-current-url-from-iframe – Radu Bogdan Sep 05 '14 at 21:19
  • How do you want to get it then, if not by click? Do you mean alerting the URL periodically? – LcSalazar Sep 05 '14 at 21:30
  • hello Radu, i tried that code and all i get is "about:blank" ... LcSalazar, what I want is to read the URL into a PHP variable $url = iframe_url; and then, in another part of the page, make the appearance of various images conditioned upon which iframe URL is being viewed ... the closest I have come is the above. it gets the right link name, but it doesnt do me much good if i cant read the result into a variable i can based conditional events on ... thanks for the reply – user3697638 Sep 05 '14 at 21:38
  • You say: 'make the appearance of various images conditioned upon which iframe URL is being viewed'. So what triggers the showing of these images? URL change in the iframe or some user action? – jonasnas Sep 05 '14 at 22:44
  • lets say the index page has an iframe that has "music.htm". all the navigation on the site is inside the iframe. so when a link (all on the same local server) is clicked on music.htm it navigates to another page in the iframe. now lets say the user clicked on 'guitar.htm' from the music.htm page. now, there is a pic, to the right of the iframe of musical instruments. but now the user click on 'guitar.htm' so i want the pic to be a guitar (in the index page). maybe have some php code that can pick up the new link and assume it is about guitar, then – user3697638 Sep 06 '14 at 04:34

2 Answers2

0

Try this

alert(frames[0].contentWindow.location.href);
Radu Bogdan
  • 494
  • 2
  • 10
0

You can use directly frames[0].location in the code where you need it. Or wrap it inside the function. It doesn't show in <script> tag directly because iframe is not loaded at that time yet. Whenever you query frames[0].location in your code it should give the location object of the iframe at that exact time

jonasnas
  • 3,540
  • 1
  • 23
  • 32
  • " Whenever you query frames[0].location in your code it should give the location object of the iframe at that exact time " ... so how would I do this? frames['name of frame here].location? thanks for the reply – user3697638 Sep 05 '14 at 21:53
  • I'm not sure I understand the question. How do you want to use this 'location'? You must be doing with it something somewhere? So whenever you want to use the location of the iframe just call frames[0].location. In your example it was blank because at that time when you wanted to use it iframe wasn't loaded yet so it had no value. Let say at some point you want to pass the value of the iframe location you just need to pass frames[0].location – jonasnas Sep 05 '14 at 22:36
  • I mean click works for you because of the different timing. script executes immediately and iframe is not loaded yet and click event takes some time till you point your mouse and at that time iframe is loaded. – jonasnas Sep 05 '14 at 22:40