I'm currently using an IFrame to sandbox user generated content on a website. This eliminates any styling issues with our main stylesheets.
However, when a user generates a link using our rich text editor, we would like the link to open in the parent and not just open the link in the IFrame. I realize you can set a target to the parent, but we do not have control of the user and what they enter in their content.
Is there any way to hijack the HREFs inside the IFrame so they all target parent without modifying them? Or use a bit of Javascript that could be injected universally so I do not need to scrape through all of the content and replace the target programatically?
Ideally a simple script in one spot would be the best solution.
Thoughts?
END SOLUTION
I used a variation of the answer I selected... It got me in the right direction.
<script>
Event.observe(window, 'load', function() {
$$('a').each(function(e) {
e.writeAttribute('target', '_parent');
});
});
</script>
That's inside the IFrame with the content. It ended up being the most simple solution for the task.
Same domain in the iframe? Yes.
Different domain in the iframe? No.
yields a "Permission denied to get property HTMLDocument.getElementsByTagName".
There may be ways around, but at least with simple JavaScript their are some protections against iframes mucking with sites (imagine a malicious frame around a bank's website and you can understand why).
Simplest answer:
I got around the cross domain issue via ajax..
Since the parent frame cant be accessed, I replaced the body via ajax.
Use this to create it and you'll have access to any parts with the $body variable:
So you can then do something like this
Found here: http://groups.google.com/group/jquery-en/browse_thread/thread/fb646741a6192540