SitePoint Sponsor

User Tag List

Results 1 to 12 of 12

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2002
    Location
    Birmingham UK
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question 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

  2. #2
    Perl/Mason Guru Flawless_koder's Avatar
    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 |=---

  3. #3
    SitePoint Enthusiast
    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,

  4. #4
    Perl/Mason Guru Flawless_koder's Avatar
    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 |=---

  5. #5
    SitePoint Enthusiast
    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.

  6. #6
    SitePoint Enthusiast
    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

  7. #7
    SitePoint Enthusiast
    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?

  8. #8
    Web-coding NINJA! silver trophy beetle's Avatar
    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
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  9. #9
    SitePoint Enthusiast
    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.

  10. #10
    Web-coding NINJA! silver trophy beetle's Avatar
    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.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  11. #11
    Perl/Mason Guru Flawless_koder's Avatar
    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 |=---

  12. #12
    Web-coding NINJA! silver trophy beetle's Avatar
    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.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev





Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •