Can I get ifame links openin a new window?
February 24, 2008 5:59 PM   RSS feed for this thread Subscribe

External web page being displayed in an Iframe. How can I make the links in the displayed web page open in a new window OR make it such that clicking anywhere on the iframe opens the displayed external page in a new window. Could get a tad NSFW inside.

1) I do not have access to the source code of the external pages.

2) I am authorized to display them anywhere and in any manner I choose.

There is a special code in the links of the pages that when someone clicks a link in one of these pages, it is tracked by the external pages owner and I get credit if the user purchases something.

I have cobbled together a way to display, via iframe and javascript code, a random page (of several thousand which changes upon every reload) from the websites of which I am affiliate at the bottom of my own website's pages.

The problem is that when a link is clicked it opens within the iframe itself, and that makes it extremely difficult to navigate the external site, and in some browsers, the clicked link wont display at all in the iframe.

What i need is that when a user clicks that link, the link OR the page the link lives on, opens in a new window. Then all problems are solved.

One consideration: I cant have MY links to MY content which are on the page open in new windows. Just whats in the iframe.

Thoughts, hivemind?

Thanks in advance!!!

P.S. I'll be on for an hour or so if I can provide more info I will, but i think i covered it pretty well :-)
posted by sandra_s to computers & internet (8 comments total)
I'm not really sure if it's possible, there are some restrictions on interaction between an iframe and the rest of an HTML document in order to reduce cross site scripting attacks in most browsers.

Two ideas:

1) periodically poll the src attribute in javascript to see if it changes. If it does, launch that source in a new window and reset the source to the origional location. That would be pretty irritating for users.

2) Write a pass through script that reads the page off the remote server, adds target="_new" attributes to all of the hrefs in the page, then gives sends it on to the user. So they would go to a URL like http://mysite/redir.php?url=http://theirsite/wherever.

Both would be a lot of work. #1 might be easier then #2, but I'm not sure if it would actually do what you want.

That's off the top of my head, there may be a better way to do this.
posted by delmoi at 6:17 PM on February 24


#2 sounds iffy, and like you would be modifying their source code.

#1 sounds like it might work. The only thing you really have access to is the properties of the iframe itself, not anything inside it. so, like delmoi said, if you had a script running on an interval every second or even half-second, it would be a slightly awkward user experience but it would work, and I don't think it would affect browser performance very much- it wouldnt be doing anything most of the time.

the ideal way to do this would be with a setter that was called when the frame src changed, but that sounds like it's a nightmare to pull off in javascript. (as is just about anything involving serious programming)
posted by drjimmy11 at 6:46 PM on February 24


or, there are scripts to open all links in a new window/tab. You could turn that on on mouseOver the iframe, and off on mouseOut.

However, not sure if the script would cascade down and affect the links in the iframe. I'm guessing probably not.
posted by drjimmy11 at 6:50 PM on February 24


ok, this is bizarre, but I'm amusing mysel pondering this, so bear with me:

create not one, but two(2) identical iframes with the original link in them. They sit in the exact same place, but make one invisible via css.

now, onClick of anything in the visible frame, hide frame 1 and frame 2 appears. Then, have a js look at the source of frame one (now invisible), and open that in a new window.

one problem I can think of is if they clicked something that wasn't a link, it might get weird.
posted by drjimmy11 at 7:05 PM on February 24


I figured you'd be able to do it with Javascript, but browser security prevents frames communicating across domains.

My only other suggestion is a PHP script or something that reads the remote page modifies it locally to add target attributes, and then outputs the modified source.
posted by sycophant at 12:55 AM on February 25


periodically poll the src attribute in javascript

Tested this -- addressing the iframe by id only allows you access to the 'src' element, which doesn't change with navigation within the frame. Addressing it as a frame, by name doesn't allow access to the location.href property.

I'm sure there is some way, but Javascript seems not to be that way.
posted by sycophant at 1:14 AM on February 25


Well, Im astounded that this seems to be an impossible thing to do, so lets look at it differently. Is there some other way I can display at the bottom of my page, a random (from a finite list) page, and make it or its link open in a new window? Without an Iframe?

Ancillary: Is there a way to automatically take screenshots of a list of webpages? If i could automatically take a screenshot of the pages, i could just link the screenshot. But it is not practical to do manually take the thousand or so screenshots.


Thanks!
posted by sandra_s at 6:28 AM on February 25


There are services to take screenshots, but the free ones are few and far between, I've never had any luck with that.

The best way I can think is to proxy the remote request through your site and modify the content there.
posted by sycophant at 2:58 AM on February 26


« Older Can anyone point me to a full-...   |   What's the best way to fix up ... Newer »

You are not logged in, either login or create an account to post comments



Related Questions
What's the best external HD? April 20, 2008
External, wireless, RAID-capable storage (and a... February 12, 2008
Help me find reliable external storage October 24, 2007
External RAID 5 September 7, 2007
Internal hard drive to external? September 30, 2006