Page 1 of 2 12 LastLast
Results 1 to 10 of 17

Thread: Script to force a page into its parent iframe

  1. #1
    Join Date
    Mar 2006
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need Help w/Script to force a page into its parent iframe

    I've not been able to find a good script** to use to force a content page to always open in its parent iframe so when Yahoo or Google lists the content page and a user clicks on it, I can make it open up in the iframe that it was made for. Scenario:

    Index.html has an iframe with src=contentpage1.htm

    contentpage2.htm is another page that should always be opened in the Index.html iframe.

    User clicks on a search page external link www.mywebsite.com/contentpage2.htm but page is forced to open inside Index.html iframe.

    ** it will be 2 scripts, 1 for the to be used in all the content pages and one for the index.html page.

    thanks for the help
    Last edited by jlorenz; 03-30-2006 at 06:40 PM.

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    I'm slightly confused, but isn't this less javascript and more target frame stuff with the links? target=self, target=blank, target=framename? I haven't done this myself and I can't recall specifically from when I learned it. I think its easier than what you're thinking, though.

  3. #3
    Join Date
    Mar 2006
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Target= comes into play only when you are linking from within the iframe or the parent index.html page itself.

    My problem is when a user is trying to open up the content page "externally" - usually from an external link like Yahoo or Google. Also if you are in another page in the website and want to link to that content page and want it only to open inside its iframe, you need Javascript to force it. One script in the content page to direct it to open in the parent page and another script in the parent page to then open the content page in the iframe.

  4. #4
    Join Date
    Sep 2005
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I didnt make this script, but i found it and it is rather useful if you dont want your users to view pages outside the frames/iframes. just paste this code into the head section of the pages you dont want users accessing outside the frameset and it will automatically redirect their browsers to the page with the iframe.

    Code:
    <SCRIPT LANGUAGE='javascript'>try { if (top == self) {top.location.href='index.html'; } } catch(er) {  } </SCRIPT>

  5. #5
    Join Date
    Mar 2006
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks for the input.

    I've tried this type of script and it will open the parent index.html page but it doesn't automatically load the original content page into the iframe. So the user gets redirected to the parent page but now doesn't have the page in the iframe they wanted.

    I need a script like this to redirect to the Parent iframe page and a script for the Parent page to load the content page into the iframe.

    Does anyone have these two scripts?

  6. #6
    Join Date
    Sep 2005
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    thats where links come in... either place the links to other pages inside or outside of the iframe. if the link is placed outside of the iframe use
    Code:
    <a href="contentpage2.html" target="iframename">
    (dont forget to name your iframe or this wont quite work) in the iframe tag put in
    Code:
    name="(whatever you want to name your iframe)"
    of course putting links to other pages inside the iframe though doesnt require the target method.

  7. #7
    Join Date
    Mar 2006
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Josh,

    Please read my reply above. The problem to solve is from external links NOT internal page links.

  8. #8
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Why not use target="blank"? That'll open a new, clean window, and it'll leave your site in the background, so they'll return to it after viewing the new page. Might be you some more traffic than otherwise.

  9. #9
    Join Date
    Mar 2006
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    OK, Let me restate my javascript need for those who don't want to read the top question:

    I need help with a script so when a user clicks on a link, to one of my content (iframe) pages ,that's on on an "EXTERNAL" page to my website and I want that content page to only open in its parent page iframe!

    < a href="" target="iframe"> does not come into play here. It only works when the link is on the same page as the iframe.

    An example is where I have a link on Google to one of my content (iframe) pages that I only want to have it open in the iframe of the parent index.html.

  10. #10
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    With a little modification, this should work for iframes:

    http://www.dynamicdrive.com/forums/s...36&postcount=4

    You can use the same script as suggested for the 'orphan' page as at the above link. Then on the top page use this:

    Code:
    <script type="text/javascript">
    var text = window.location.href;
    function delineate(str) {
    theleft = str.indexOf("=") + 1;
    theright = str.indexOf("&");
    return(str.substring(theleft, str.length));
    }
    url=delineate(text)
    
    function refreshFrame() {
    if(iframeName.location!=url&&url!=location.href)
    iframeName.location.replace(url);
    }
    </script>
    and since it isn't a frameset page, run the refreshFrame() onload:

    HTML Code:
    <body onload="refreshFrame();">
    Last edited by jscheuer1; 04-01-2006 at 08:49 AM. Reason: correct code
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

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
  •