calling javascript function in iframe from window.opener - javascript

I have a function in an iFrame (same domain) that I want to call from a popup.
So basically I want to do something like window.opener.document.getElementById('topFrame').contentWindow.setActive('1');
In the window.opener of the popup, i have an iframe with the id "topFrame" (and name "topFrame" in case that's a better solution) in which the function setActive('1') must be executed.
However the code stated above does not work and google isn't really helping in finding solutions for this specific case.
Anyone has an idea?
Thx

Your line should actually work and the problem probably a timing issue: most likely your iframe hasn't finished loading at the time you are trying to call its function. Placing your call after load or DomReady should do the trick.
(You can also experiment by wrapping your call in a timeout and see if it works at a later point).

if you are opening the pop up from the iframe window then you should use
window.opener.setActive('1');
because window.opener will itself return you the iframe window .it might be the case because i was also committing this mistake. hope your problem is solved :)

Related

Open link in popup window

I might be tired but I just can't figure out what the problem is. What I'm trying to do is open a link in a popup window. I got this code below working before but I removed it.
About
However, it stopped working now when I put it back. I even got it working on jsFiddle so I'm at lost on what to do. I'm assuming something must be blocking it from running?
The code is short and simple so I figured someone here might have an idea what could cause this.
EDIT: Sorry I should have thought of it. I guess I should sleep. Anyway here's a demo-website where I reproduced the problem http://testmycode.tumblr.com/ The problem is the "About" link, pressing it returns nothing.
OK, it seems like somewhere in your code you have changed the window variable to a custom function. When you try to call window.open (more specifically, document.window.open), the method open simply doesn't exist in the function window, which causes it to throw an error.
Check this out:
You somewhere changed it to a function by doing document.window = ....
It's MooTools 1.2.4 which changed it:
To fix it, simply using an EventListener and problem solved! (Inline codes are bad practice anyway.)
<a class="about">About</a>
$(".about")attr("href", "#").click(function(e){
window.open(...);
e.preventDefault();
});
The snippet you shared works when I append it to the page we are at, in Google Chrome. Which makes me wonder which browser you are having the trouble in. So I would encourage you to try the snippet you shared in Google Chrome, and if it works there then you will know it is a browser specific kind of bug, in which case I would try adding a semicolon after return false.

Iframe has fancybox inside it that pops up onload. I want that to be closed automatically. [duplicate]

I know this is possible and can be done, or at least used to be like that.
Now I can't find the code anywhere.
I want to close the fancybox within the iframe on document ready from the parent page.
How to do this? Any help is appreciated!
After careful examination and researching everywhere from here to all over Google, seems like this is not possible at all due to security issues rising from it if the browsers did allow such a thing.

Inserting <script> into iframe with jQuery

I'm attempting to insert a snippet into an iframe through jQuery with the following:
var snippet = "<script>document.writeln('test')</script>";
jQuery('<iframe />').appendTo('body').append(snippet);
instead of writing "test" in the iframe, it overwrites the parent window.
How can I make it so that the parent window gets the iframe with "test" inserted in it?
You should set the source of new windows and iframes to 'about:blank' if you want control over them. Then reference it by the iframe's ID!
You also want to use iframe.contentDocument || iframe.contentWindow.document
And it might be a good idea to 'open()' the document before you 'write()' to it.
Update: forgot this: If you open the window about:blank, it needs time to load..
So you cannot write to it right away!!
So either set timeout of about 50ms (usually) and then write to the new window/iframe.
OR check if it is loaded (onload), then have it write the source (I prefer this).
If you need to reuse the iframe, set it to about:blank first (again) and wait or check onload again before writing to the frame again. All this is due to xss security.
I also strongly advise to use the traditional event-model (so no addEvent things, they don't work as intended crossbrowser and lead to memoryleaks in IE).
So: create iframe with src set to about:blank, add a onload function that checks a var containing your html-string: if it is empty, else: write it to the iframe and empty the string.
To update the iframe: set content to the var containing the html-string, followed by setting the source of the iframe to about:blank.
Understand the loop?
This baby even works in IE6...
Good luck!!
UPDATE: you did not escape your snippet properly: should be: <script>document.writeln('test')\<\/script>
See: jsfiddle
UPDATE2: argl.. I normally never give up, but since I don't care for jQuery, I'm not going through the manual for jQuery for something something so simple in something as difficult as jQuery (sorry). For modern (crappy) security reasons you need an about:blank. Period.
See this updated fiddle for the 'plain jane' basics at work, then see how to do it in jquery and make a choice: onload or setTimeout. I'm working on my own crossbrowser html-editor and this subject took over a week to figure out.

Calling javascript function from iframe

I have an iframe on the page and I am trying to call a function on the parent page form the iframe. I noticed that when i use:
window.top
that works fine in Chrome. However, when i use:
window.parent
that works for IE.
How can i write a method that checks both and uses the correct method? Also i am using jquery and was trying to find a way use that to do this. Any help is appreciated.
So i doing something wrong for some reason but parent.function() works fine in all browser. Sorry for confusion

How do I get JavaScript created with document.write() to execute?

I have a multi-frame layout. One of the frames contains a form, which I am submitting through XMLHttpRequest. Now when I use document.write() to rewrite the frame with the form, and the new page I am adding contains any javascript then the javascript is not exectuted in IE6?
For example:
document.write("<html><head><script>alert(1);</script></head><body>test</body></html>");
In the above case the page content is replaced with test but the alert() isn't executed. This works fine in Firefox.
What is a workaround to the above problem?
Instead of having the JS code out in the open, enclose it in a function (let's call it "doIt"). Your frame window (let's say it's name is "formFrame") has a parent window (even if it's not visible) in which you can execute JS code. Do the actual frame rewrite operation in that scope:
window.parent.rewriteFormFrame(theHtml);
Where rewriteFormFrame function in the parent window looks something like this:
function rewriteFormFrame(html) {
formFrame.document.body.innerHTML = html;
formFrame.doIt();
}
Workaround is to programmatically add <script> blocks to head DOM element in JavaScript at Callback function or call eval() method. It's only way you can make this work in IE 6.
Another possible alternative is to use JSON, dynamically adding scripts references which will be automatically processed by browser.
Cheers.
In short: You can't really do that. However JavaScript libraries such as jQuery provide functionality to do exactly that. If you depend on that, give jQuery a try.
Eval and/or executing scripts dynamically is bad practice. Very bad practice. Very, very, very bad practice. I can't stress enough, how bad practice it is.
AKA.: Sounds like bad design. What problem are you trying to solve again?
You could use an onload attribute in the body tag (<body onload="jsWrittenLoaded()">).

Categories