View Full Version : How to PREVENT opening in new window?

04-22-2008, 09:10 PM
Here's a problem I'm having. I got my HTML page. In this page I have an iframe. The iframe is linked up to a page that I don't have control over, but what I want is that when someone clicks on a link in this iframe, that it replaces the what's in the iframe instead of opening a new window.

Is this even possible considering that I don't have control over the code that's contained within the iframe? If I do a view source on the page, I can see that they're doing a "target = _blank" to have this link open in a new window. Is there anyway to prevent this from happening? Thanks.

04-22-2008, 09:23 PM
i suppose it comes down to the domain that the displayed content inside the iframe originates from. if it's on the same domain, then you can gain programmatic access to the iframe, locate the appropriate anchor and set the target via javascript.

04-22-2008, 10:04 PM
Your window cannnot use a script to read anything in the window in the iframe, if it is from a different origin.

Script in an iframe can call a new window, or even window.top.replace your window-
so pick your remote content with some care.

04-22-2008, 10:22 PM
it is possible that this user is working in an environment where there are multiple servers comprising a single web domain (using virtual mappings etc) and that he/she does not have access to the code (or is not allowed to modify the code) of the page in question, in these circumstances, you can read/modify the contents of the iframe.

in the case that you've just opened a site on a different domain in your iframe, then mrhoo is right. there is a way around this, however. if you implement an http proxy through server side logic on the server you do have access to, you can request the remote page from a local server side page and have your iframe render that content, followed by using javascript in your main page to modify the contents of the proxy-requested remote page. the hangup with this is that if the remote page was using relative paths to its resources, they would no longer be available, all the resources would have to be absolutely pathed in order for them to find their true origins, which you could also correct via javascript (but that would take some work).

04-22-2008, 10:57 PM
Nope. It's not. The page is from another domain and changes depending on the link prior to arriving at the page. Basically, we have a job listing site. Some job listings are in-house listings, in which case we just take them to our job listing page.

However, other listings that come up are pulled from outside listing sites. When showing the listing, we show the listing stuff inside an iframe. However, when a person clicks on a link to actually apply for that job, the whole "target = _blank" opens up a new window for that link. What we want to be able to do is that when a person clicks on that link to apply for the job that the new window just replaces the contents of our iframe.

Is that even possible? Looks like I'm stuck, huh?

04-22-2008, 11:07 PM
if you have the capabilities to run asp or php, you can request the page from the server side.
check out listing 3, gives an example of the syntax you'd need in your asp page to be able to fetch the page.

you would then set the source of your iframe to the page you've made to pull in the data from the other site. then you can access the contents of your iframe programmatically because they were last processed by a server on the same domain as your present page.

04-23-2008, 03:43 PM
Cool. That sounds like it should work. I'll give it a try. Thanks again!