SitePoint Sponsor |
|
User Tag List
Results 1 to 12 of 12
Thread: Get the address of an iframe
-
Jul 24, 2002, 11:11 #1
- Join Date
- Jul 2002
- Location
- Birmingham UK
- Posts
- 35
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Get the address of an iframe
I've got an iframe within a document which the user can navigate around the internet with.
How would I use javascript to get the address of what is in the iframe and use it in my master document?
For example, my page loads with an iframe in it which loads up with SitePointForums.com. The user clicks through some links to an article they are interested. They then hit a "SAVE" button on the master page that the iframe is in, which in turn gets the current page in the iframe and does some processing in the master document.
Any ideas greatly welcome.
Cheers,
Si
-
Jul 24, 2002, 11:53 #2
- Join Date
- Feb 2002
- Location
- Gatwick, UK
- Posts
- 1,206
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
use the location property of the frame.
Flawless---=| If you're going to buy a pet - get a Shetland Giraffe |=---
-
Jul 24, 2002, 13:40 #3
- Join Date
- Jul 2002
- Location
- Birmingham UK
- Posts
- 35
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I'm trying that but can't get the syntax right.
When I hit the save button (name = 'btnSave'), I want to put the location of the frame (name = 'fraOutsideWindow') in a hidden value (name = 'hdnWindowLocation').
What is the code for this in JS?
I appreciate any help on this.
Thanks,
-
Jul 25, 2002, 00:21 #4
- Join Date
- Feb 2002
- Location
- Gatwick, UK
- Posts
- 1,206
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
document.forms[index].inputObj.value = top.location;
Flawless---=| If you're going to buy a pet - get a Shetland Giraffe |=---
-
Jul 25, 2002, 06:09 #5
- Join Date
- Apr 2002
- Posts
- 75
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Different browsers have different ways of accessing the location of an iframe.
<iframe src="http://www.google.com"></iframe>
<script>
var IFrameObj=document.getElementsByTagName('iframe')[0];
var IFrameDoc;
if (IFrameObj.contentDocument) {
// For NS6
IFrameDoc = IFrameObj.contentDocument.location;
} else if (IFrameObj.contentWindow) {
// For IE5.5 and IE6
IFrameDoc = IFrameObj.contentWindow.document.location;
} else if (IFrameObj.document) {
// For IE5
IFrameDoc = IFrameObj.document.location;
}
alert(IFrameDoc);
</script>
This script will give you an 'Access denied' error.
You can only access the location object if the domain of the iframe location is the same as the page in which the iframe appears.
-
Jul 30, 2002, 03:28 #6
- Join Date
- Jul 2002
- Location
- Birmingham UK
- Posts
- 35
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I can't get that method working.
Basically, I've got two forms on my page. The first one (frmGetAddress) asks for a web address and has a GO button. This then reloads the whole page with the iframe loading the typed in URL (text field: "gotoURL").
At the bottom of the page is another form (frmProcessAddress). This has a hidden field ("finalURL") and submit button ("btnFinal"). When the submit button is pressed I want to get the iframe location in the hidden field and retrieve that for processing.
As the user may want to browse within the iframe, the address could change so I can't just add the first entered URL.
Can anyone help on this? I'm not the best at JS coding so could do with a hand!
Cheers,
Si
-
Jul 31, 2002, 12:02 #7
- Join Date
- Jul 2002
- Location
- Birmingham UK
- Posts
- 35
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Can anyone else shed some light on this?
My example is available here. Type in a URL in the top box and hit preview. Underneath will appear the website in an iframe. Navigate around the site within the iframe and at any point hit the Get URL button. The current address should appear in the text box next to it. The code I'm using just creates an error.
Any help?
-
Jul 31, 2002, 13:18 #8
- Join Date
- Jul 2002
- Location
- Dallas, TX
- Posts
- 2,900
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I think you're SOL, at least in IE. This article makes a good point about why you can't do this.
http://msdn.microsoft.com/library/de...g_security.asp
-
Jul 31, 2002, 13:34 #9
- Join Date
- Jul 2002
- Location
- Birmingham UK
- Posts
- 35
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Damn...
Can anyone think of another way of getting the same kind of result? Or am I stuck with a simple method of typing the URL in the top box and thats as good as you get?
What about using standard frames (instead of iframes)? What if I open up a popup window which divides the screen in two - bottom frame with a URL box and Go button, top frame with the preview. Bottom frame has a save this page button which then grabs the top frames location and submits it to another form.
-
Jul 31, 2002, 13:41 #10
- Join Date
- Jul 2002
- Location
- Dallas, TX
- Posts
- 2,900
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
According to the article....
You're still stuck, because you are trying to grab the location.href property of a frame (not just iframes). The only time you can do this is when the pages are within the current domain.
-
Aug 1, 2002, 01:01 #11
- Join Date
- Feb 2002
- Location
- Gatwick, UK
- Posts
- 1,206
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I wonder if you told the iframe that your window was it's parent once the content had loaded - if that would make any difference?
Flawless---=| If you're going to buy a pet - get a Shetland Giraffe |=---
-
Aug 1, 2002, 06:21 #12
- Join Date
- Jul 2002
- Location
- Dallas, TX
- Posts
- 2,900
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Seriously doubt it. It seems that any frame, no matter where it's at the document tree, preserves those security settings. It's not something you can switch or or circumvent. The browser simply just won't let you read that property UNLESS the current location.href of the frame is in the same domain as the document that accesses that property. You can write to it just fine.
Bookmarks