Forum Moderators: open

Message Too Old, No Replies

Dynamically show iframe URL in text field

         

Manic Monday

7:48 am on Oct 13, 2005 (gmt 0)

10+ Year Member



Hello everyone, this is driving me CRAZY please help :)

I was reading an older post (http://www.webmasterworld.com/forum91/2042.htm) and I can't get my modification to work.

I'm trying to dynamically show the iframe url in the text field below it, as the person browsers around in the URL. With no luck but :\

------------------------------------------------------------
<html><body>

<iframe id="myframe" name="myframe" src="http://www.google.com"
style="height: 90%; width:100%; margin: 0 0 0 0; border: 0px;">
</iframe><br>

<script type="text/javascript" language="JavaScript">
document.write('<input type="text" style="width:100%; " ');
document.write(' value="' + document.getElementById('myframe').document.URL + '">');
</script>

</body></html>
------------------------------------------------------------

Many thanks in advance for your suggestion and assistance,
Manic_Monday

Scally_Ally

9:25 am on Oct 13, 2005 (gmt 0)

10+ Year Member



you just need to change one bit within the code,

from this
document.getElementById('myframe').document.URL

to this
document.getElementById('myframe').src

this ensures that you are getting the ifames source value and not the documents url

<html><body>

<iframe id="myframe" name="myframe" src="http://www.google.com"
style="height: 90%; width:100%; margin: 0 0 0 0; border: 0px;">
</iframe><br>

<script type="text/javascript" language="JavaScript">
document.write('<input type="text" style="width:100%; " ');
document.write(' value="' + document.getElementById('myframe').src+ '">');
</script>

</body></html>

Manic Monday

11:52 pm on Oct 13, 2005 (gmt 0)

10+ Year Member



Thanks Scally,

But unfortuantely src only gets the source value and not the actual iframes URL.
And for example, since people will navigate away from google.com in the iframe what I need is the URL they are visiting.

I need the text field "myfield" to display the iframes URL just like the Address Bar does for the window.

I've placed a button next to the text field so that onclick get URL of iframe (currently it writes to an alert and I'm having all sorts of trouble getting it to write to the textfield "myfield")

------------------------------------------------------------
<html><body>

<iframe id="myframe" name="myframe" src="http://www.google.com"
style="height: 90%; width:100%; margin: 0 0 0 0; border: 0px;">
</iframe><br>

<input type="text" style="width:92%;" name="myfield" id="myfield">

<button sty style="" onclick="alert(document.getElementById('myframe').url)">get url</button>

</body></html>
------------------------------------------------------------

Let me know what you think,
Cheers Manic Monday

Rambo Tribble

2:48 am on Oct 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You might wish to look into innerHTML or the W3C's node manipulation methods to replace document.write(), as it could cause complications. Remember, only URLs from your domain will be accessible. The Same Origin Policy prevents URLs from other domains from being available to a page from your domain.

ajkimoto

2:55 pm on Oct 14, 2005 (gmt 0)

10+ Year Member



Hi Manic_Monday,

As RamboTribble mentioned, you will not be able to read the href of the document contained in the iframe if the document is located on a different domain than yours even though you can change the href. Kinda reminds me of a friend of mine that invented Write-Only-Memory (WOM). It never really took off, though.

ajkimoto

Rambo Tribble

10:39 pm on Oct 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oh, yeah, WOM. I think I had shares in that start-up.

Bernard Marx

12:12 am on Oct 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Myself, I work best on the Read-Only Forgetfulness system.

Manic Monday

1:45 am on Oct 15, 2005 (gmt 0)

10+ Year Member



*laugh*

Thanks guys for all your help & quick responces :D

You've all definately made my first post :)