SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict
    Join Date
    Apr 2002
    Posts
    395
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Force target="_blank" on an iframed page

    I have an iframed page on my website. The iframed page has links that have set target="_top" and so they open over my website.

    I want all the links in the iframed page to open in a new window. Can I do that?

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,230
    Mentioned
    194 Post(s)
    Tagged
    2 Thread(s)

    target

    Quote Originally Posted by IamAdam
    Can I do that?
    Why not? Any reason you can't simply remove the target attribute from your framed pages?

  3. #3
    SitePoint Addict
    Join Date
    Apr 2002
    Posts
    395
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, the thing is, the page I am framing is not mine - I don't have control over it.

  4. #4
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,230
    Mentioned
    194 Post(s)
    Tagged
    2 Thread(s)

    target attributes

    I see. Then I guess you should explain your problem to the page's author and ask them if they would remove them. I can't think of a reason why they shouldn't as long as you can explain yourself clearly enough.

  5. #5
    <?php while(!sleep()){code();} G.Schuster's Avatar
    Join Date
    Mar 2007
    Location
    Germany
    Posts
    428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Perhaps this may work - I adapted it a little, normally I used this to mak links opening in a new window without the target-attribute hardcoded - instead I used rel="external" and set the target with JS.

    Try it and let me know if itworks

    Code:
    function externalLinks() {
        if(!document.getElementsByTagName) { return false; }
    
        var anchors = document.getElementsByTagName("a");
        for(var i = 0; i < anchors.length; i++) {
            var anchor = anchors[i];
            if (anchor.getAttribute('href') && anchor.getAttribute('target') && anchor.getAttribute('target') == '_top') { anchor.target = '_blank'; }
        }
    }
    window.onload = externalLinks;

  6. #6
    SitePoint Addict
    Join Date
    Apr 2002
    Posts
    395
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the code, and sorry for my belated reply.

    The page in the iframe does not actually have hardcoded target. The iframed page contains javascript links which are set to open in "_top"
    However, there is nothing on the iframed page that has target="_top". Only javascript which contains links that open in "_top"

    To make this look more imaginable, I am framing a javascript adcode.

  7. #7
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,862
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    You can't use javascript to update a web page from another site that is displayed in an iframe. Whatever is in the iframe works the way the owner of that page intends it to. In the case of ads, making any such change would violate the terms of service for your ad account if this were possible.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


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
  •