PDA

View Full Version : change iframe inner A tag urls



vinny.benson
07-22-2010, 09:54 PM
Hi,

I was wondering if there is a way the change the URL links in any page loaded into an iframe on my homepage

e.g. if i load google.co.uk into the frame
and did a search for "javascript code", i want the links to open in a new tab but within my homepage

here is one of the results i got

Dynamic Drive DHTML(dynamic html) & JavaScript code library (www.dynamicdrive.com)
Click here for Dynamicdrive.com, a comprehensive DHTML site featuring DHTML scripts, tutorials, and more.
www.dynamicdrive.com/ - Cached - Similar

the title links to "http://www.dynamicdrive.com/", but i want to change it so it links to "http://mysite.com/?url=http://www.dynamicdrive.com/

which will load the site in a new tab in my home frame

is this possible??

if so how do i go about doing this?

djr33
07-24-2010, 02:06 AM
First, this question borders on a legal gray area. The effect will be either that you are altering external websites and effectively lying/tricking users, or that you will be stealing the content of external sites for use in your own-- the difference depends on how integrated it all feels. For example, if you are planning to "borrow" google as a search engine on your website, then this would be effectively stealing their site/code and using it as your own. It's complex...

Now toward a more direct answer for your question: this is not possible using simple methods. An iframe's content cannot be controlled from the parent page and security may disallow any access.
It may be possible loop through every A tag in the iframe using Javascript, but that borders on the legal gray area mentioned above. As a purely hypothetical idea, it might be interesting to see if this code works. Note that there are different security restrictions (based on browsers and users' settings) for pages on the same site and external pages (other sites). But it wouldn't be easy, regardless.
The easiest method using Javascript is to modify the page in the iframe, if you do have access to it. If you don't, then that's a case where you probably shouldn't be modifying it at all.


In order to really accomplish what you are asking, you would need to build what is called a "proxy", though for a somewhat different goal. A proxy is an intermediate server that gets content from some other server, then forwards it to the final user. So if I ask a proxy for "google.com", it the proxy server first downloads it from google's server, then I download it from the proxy server.
This can get complex (though in the basic form it isn't very difficult, using a language like PHP).

The issue here, though, is that you then need to parse the entire page and modify components. Or you could just add some code to force it to open in the parent window or whatever you like.

The reason this works with proxies is that they only modify the content so that it works on their proxy server. For example, if you use google translate to view a page in another language, it converts all links on that page to more "google translate" links, so you continue viewing the page in English (or your native language). This method could be applied here, but it is not easy to program, and your situation is different: it sounds like you are trying to either mislead the user or "borrow" from another site.



By the way, if your example of google is your final goal, there are custom google search tools available so that you can add a google search to your site.