I have a page with an iframe. In the iframe, when the user clicks a link, instead of the new page appearing in the iframe, surrounded by my graphics, the new page replaces the current page. This is becuase some of the links in the iframe have the target="_top" attribute. How can I prevent the links from clearing out my page without having to remove the target="_top" ?
Who is Participating?
dreamer007Connect With a Mentor Commented:
Ok, all is done - sry again for the delay. Found some space..


The code is on the page. Just remove the space from the asp < % and % > delimiters when you paste it - 2 files test.asp and test2.asp. You should be ok customising this yourself... ; )

Whew! ASP, huh - why use one line in PHP when you can use 20, lol!

Unfortunately it sounds like where you have target="_top" is exactly the code you need to change.  I know you probably don't want to hear that but as your message indicates that you don't want to remove the target="_top" but I think you will probably have to in order to get the functionality you desire.  Along those lines I have two suggestions.  The first (and simplest) is to try changing the target to something other than top.  Maybe try using parent in some places as opposed to top or targeting a specific frame name.  The second alternative is to use a server side scripting language to dynamically change your target.  I would say your environment and experience should determine the server-side language you would want to use.  You can find a number of freeware and shareware tool that do bulk changes across directories and subdirectories to help you make youe changes and test here : http://www.all4you.dk/FreewareWorld/links.php
and information on IFRAMEs/FRAMEs here: http://developer.irt.org/script/frame.htm

Hope that helps somewhat and Good Luck,
yourbudweiserAuthor Commented:
Jeff, thanks for responding. I checked out the websites you suggested... The first one, there are alot of links but I couldn't find one that would help me with my problem. I also looked throught the second link and although it had lots of tips for framed pages, none of them related to iframes that are stored on other servers. If you could suggest some ideas for a server side script that would do what I need, I would love to hear it, Thanks again, YBW
Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to Squarespace.com and use offer code ‘EXPERTS’ to get 10% off your first purchase.

change the target name to the IFrame name
yourbudweiser, is there any a possibility that we can get some code snipits of what you currently have in place, and an explaination of your environment (platform, webserver, etc) as well as a list of the server-side languages you are familiar with?  Something I probably should have mentioned above is that most of the rules that govern frames are the same rules you would apply to iframes (there are some exceptions), but for what you are trying to do it sounds like you simply need to change your target.  Here are a few additional links pertaining to iframes that might prove useful:


Is there a particular reason you need to keep the target='_top' in place in your existing pages other than the fact that it might prove painful to make changes across your entire site?  I'll be watching for your response.

Good Luck,
yourbudweiserAuthor Commented:
Hi Jeff, thanks for responding.

The page in the iframe is actually on another server which I have no control over. In the iframe page are links with the target=_top. When clicked the links open in a new window, outside of the iframe.

I cannot modify that page so I can't remove the target=_top and I can't modify it at all. Therefore, I need some other way to prevent the links target=_top from opening in a new window.

<iframe name="metro" src="http://metrotag.clickprint.com" height="600" width="820" border="0" frameborder="0"></iframe>

The web server is MS and I am familiar with ASP.

I'll be looking at the links you suggested also.

Thanks again,.

This is funny, (well it is if you have PHP on your server).


<iframe width="100%" height="100%" name="wibble" src="demo.php?url=http://metrotag.clickprint.com/"></iframe>


if (eregi("http://metrotag.clickprint.com/", $url))
{ echo "<!-- from the site -->"; }
echo "<BASE href=$url>";
$html = implode ('', file ($url));
$html= str_replace ("target=\"_top\"", " ", $html);
$html= str_replace ("href=\"", "href=\"demo.php?url=", $html);
echo $html

Lol, it breaks on the 'design online' flash detection with an invalid session, but it's fun until then.... If this were specifically for this site, we could get around that.. but, anyway - just for kicks.

If you have PHP on the server and it's just for this site, let me know which items break and we'll correct them. If it's to frame any site, this is probably not the answer..(especially if no PHP)...

Well, it was fun.. ;)

yourbudweiserAuthor Commented:
Hey dreamer, thanks for responding but i'm not into PHP so... can you do something similar in ASP? What is your trying to do there?
I'll have a play with it. I'm all on linux hosting and although theres a versoin of asp, it's just not 100%, so testing is a bit diff...

Here is the version - http://www.thisisyourname.com/demo/test.php

Have a look.

We take in the webpage as a string, parse it and change the target. It's then displayed on our own dynamic page and...well, it works. lol.

Hmm, *thinking of test environment for asp witout messing too much with IIS....*

yourbudweiserAuthor Commented:
dreamer, that works great! now if I can get it to work with ASP i'll be in your debt!

I'll work with it as well and looking foward to seeing your solution in ASP :-)

Election 2004 Battleground State of Florida
No problems - nearly done. I'll be out for an hour, will complete upon return..sry for delay. :)
yourbudweiserAuthor Commented:
Hey dreamer, great job!

This problem was on my mind all day yesterday! With your help, I came up with the following last night:

<iframe name="metro" src="includes/page2.asp?url=http://metrotag.clickprint.com/"></iframe>

Function GetHTML(strURL)
  Dim objXMLHTTP, strReturn
  Set objXMLHTTP = Server.CreateObject("MSXML2.ServerXMLHTTP")
  objXMLHTTP.Open "GET", strURL, False

  strReturn = objXMLHTTP.responseText
  Set objXMLHTTP = Nothing

  GetHTML = strReturn
End Function

newhtml = GetHTML(Request("url"))
newhtml = replace(newhtml, "target=""_top""", "")
newhtml = replace(newhtml, "href=""", "href=""page2.asp?url=")

response.write newhtml

Thanks alot for sticking with this question, you were a great help!

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.