I have an iframe that displays a flash banner ad which has clickable links. Is there anyway to force the links to open in parent window, without putting JS on the page that opens or changing the flash file?
For example, is there something i can do to the iFrame to force links to open in its parent?
This little snippet should do it:
if (top.location != location) {
top.location.href = document.location.href ;
}
No, you absolutely cannot do this. Your ad is likely served from a domain different than the rest of the page outside of the iframe. The only security restriction applied to JavaScript is called same origin execution. A browser will not execute any JavaScript that originates in a domain different than that of the effect page/window. As a result JavaScript is not your answer.
If the links were HTML, yes. But since the links are embedded in the SWF itself, there is no way to change their targt, AFAIK.
Related
Contained within my web page is an iframe that's src is set to load a page from another domain.
This domain has links with target="_top".
What I'm trying to achieve is, when someone clicks one of these links, I'd like it to open in a new window rather than load in the current window.
Is there any way to "catch" this and do what I require?
Thanks!
The domain has probably very good reasons to put target="_top" in their links. They don't want to be ran inside a(n) (i)frame. The correct answer to this is "No, this can't be done." Obvious security reasons prohibit us to do this.
You don't want somebody messing with your page by running it in an iframe. Phising and password retrieval becomes kind of easy when this is allowed. Browsers will not allow this these days.
So, lets say there are two iframes, ifra0 and ifra1. Hyperlinks in ifra0 are fun to click but it would be nice to just have them open in ifra1 instead of a new tab or window. How can I accomplish this using Javascript? I do not have control over the content of these pages.
The page with the iframes is hosted on my computer, the iframes are webpages on the Internet.
EDIT: HTML4 Transitional would be okay.
Frames can communicate using postMessage() in co-operative Javascripts.
https://developer.mozilla.org/en/DOM/window.postMessage
The Javascript must be loaded in every <iframe>
Older browsers may not be supported
http://ajaxian.com/archives/cross-window-messaging-with-html-5-postmessage
Catch click() event in one IFRAME
Convert it to postMessage() message
Post it to the target frame
Target frame parses the message and performs window.location change based on the message payload
Though I am not sure whether the top frame hosting two iframes must be co-operating or not.
I want to allow any page to be loaded inside an iframe. It's for teaching purposes so I want to know if it's possible to force let's say:
<iframe src="http://www.wolframalpha.com/input/?i=5*sin%28x%29" width="400" height="100">
to stay inside the iframe. By default it has some kind of javascript that opens in full page.
UPDATE: What if i use frames? (please don't throw bricks at me) Could they know if the page is inside a frame?
If the page itself wants to break out of being framed with it's own javascript (which apparently this page is doing), it can do so and I know of no way to prevent it other than turning javascript off in your own browser which obviously isn't an option for general viewing.
On some browsers, you can set an attribute on the iframe element that sets a security policy that prevents the iframe from executing JavaScript. I don't remember the attribute name and not sure which browsers support it (I'm sure ie does, not quite sure about the others). If you have problem finding more details, I'll look it up when I get home (on a mobile right now)
edit: found it - security="restricted". Seems to be IE-only.
If you have links outside of this iFrame and want them to load into that iFrame on the same page, you'll have to give it a name, then target the named iFrame within your link's href.
<iframe src="http://google.com" name="myframe" hieght="100" width="100"></iframe>
<br />
Derp.
However, if you're loading a page into your iFrame that's loading links with target="blank", then those will go to a new window; unless you don't have access to those pages, you won't be able to change the links (short of writing JS to dive into your iFrame, etc).
I'm interested in linking to or embedding an external page, scrolling to a specific point. Is this possible, or do cross-browser securities prevent it?
One workaround I've considered is creating the iframe within a div, giving the iframe a negative margin and then overflow:hidden; the container div.
Thanks.
A possible solution could be to use JavaScript to scroll the page. However, if the page you are embedding is on a different domain, you cannot access its content with JavaScript if it is in an iframe (due to the same origin policy). However, if it is on the same domain as the host page, you should be able to access it using JavaScript and then scroll using the window.scrollTo(x,y) method or similar (see this page on MDN).
Also, if the page you are embedding has a named anchor (<a name="blah">) or a block-level element with a specific id (<div id="blah">) at the point you want to scroll to, you can link to it or embed it by using a URL such as http://example.com/page#blah and it will scroll to blah automatically. This is not under the same-origin policy, so you can do something like <iframe src="http://example.com/page#blah"></iframe> and the frame will automatically be scrolled to blah, even if it is not on the same domain as the host.
The "iframe with negative margin" solution you mentioned could work, but that might be hard to implement and would probably cause problems, especially if you want full cross-browser compatibility.
Is it possible to generate dynamic content inside Iframe? if yes , how ? I'm having some problems with IE, thank you
UPDATE :
I'm creating a modal window which plays video, but when I close it it remains playing in IE7 although its hidden but it firefox it stops playing as it should. So I just wanted to try with iframe, thinking maybe that will solve my problem :)
As #Aaron already noted, you can use everything you use for normal pages in your iFrame.
Noteworthy however is that the content in the iframe is an isolated page.
No code from your parent page can access anything in the iframe's page.
This is a security measure that prevents Evil People from showing you trusted pages with custom javascript hooks attached.
An iframe is just like any other HTML window, so yes, you can generate dynamic content.
To create content use the normal syntax:
var div = iframe.document.createElement("div");
Please include a description of what exact problem you face. Otherwise, we can't help much.
[EDIT] Note that the URL of the document in the iframe must contain the same domain or the Same Origin Policy will prevent the access.
As for your problem with the modal window: Are you saying that the window doesn't close? That sounds like a IE bug :/