Others have encountered a similar error, usually because they tried to access a DOM object before a web page had completely loaded. I am doing something different, but I may also have a timing problem.
I have a Javascript application that communicates with CGI scripts with forms and iframes. The first time the user clicks a submit button, the JS application creates an invisible iframe and sends the form's data to the server. The server is supposed to respond by sending data to the iframe which the JS application reads and interprets.
Here is a very simplified version of the code:
var iframe = document.createElement("iframe");
iframe.name = "myIframe";
document.body.appendChild(iframe);
var iframeDocument = 'contentDocument' in iframe? iframe.contentDocument : iframe.contentWindow.document;
iframeDocument.body.innerHTML = "";
The full error is: TypeError: Unable to set value of the property 'innerHTML': object is null or undefined
The entire page loaded long ago--when the code above executes, the user has entered some data and hit a submit button. However, the iframe is new.
The code works fine on my computer--I cannot reproduce the problem. I can see from a log file on the server that the user agent is 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)'.
Perhaps naively, I thought that the iframe had been created and I could set its document's innerHTML property. Could this be a timing problem? Do I have to wait before setting innerHTML, or is something else going wrong?
If the error is indeed caused by a timing problem, I can probably fix it by creating the iframe when the page first loads. If the user then clicks a cancel button, the iframe will never be used--I thought it was better to create the iframe only when it is needed. To really fix this error, I have to understand it--that is why I am asking my question.
Maybe it's a timing issue, you can try :
var iframe = document.createElement("iframe"),
iframeDocument;
iframe.name = "myIframe";
iframe.onload = function(){
iframeDocument = iframe.contentDocument || iframe.contentWindow.document;
iframeDocument.body.innerHTML = "";
};
document.body.appendChild(iframe);
That way you'll be sure that your iframe will be ready
Related
I have a web page embedded as a form within an iFrame.
The web page posts back using the following javascript code triggered from an anchor tag
function GoToPdf(link) {
var form = document.forms[0];
form.action = link;
form.target = "_blank";
form.method = "Post";
form.submit();
}
The link passed is an .aspx page that retrieves JSON data passed in the request body and creates a PDF for download.
The whole thing works fine on Chrome and Firefox but not on IE 11. If the web page is outside of an iFrame it also works on IE 11.
In the iframe on IE 11 the page posts back to the .aspx page but the request body contains no data.
Any ideas.
Thanks
Tim
This sounds like a security issue. Try adding the domain you are trying to access to your IE trusted zone.
I'm trying to access the inside of a dynamically inserted iframe (inserted from a script that was loaded from a different domain than the host page) in ie9 on twitter.com so that i can write content into it. I tried setting the src to both '' and about:blank. Accessing the contentDocument throws an Access denied error, and accessing the contentWindow returns nothing. Why is this and is there a way around it? It works in other browsers, but for some reason is seems that Twitter might be doing something to prevent this and I'm curious as to what it is.
I use the following and I believe it works on all browsers that support iframe.
var iframe = document.createElement("iframe");
document.body.appendChild(iframe);
var iframeDoc = iframe.contentWindow.document;
iframeDoc.open();
iframeDoc.write(html);
iframeDoc.close();
I don't set #src at all.
I have seen many questions like this but have not found anything that seems to help with my specific situation so I apologize if this question seems repetitive.
I have a site www.foo.com and have an iframe in it. When information I click on an a tag in foo.com a javascript function is called that passes a new image to the iframe to show the user. The communication between iframe and its "parent" seems to work fine on all browsers EXCEPT RANDOM IE8 PAGES. I get the following error message "access is denied" and the browser points to the function that has been activated. Following is a piece of code from the site to see how it works.
the iframe:
<iframe scrolling="no" src="foo.com/bar" id="ifram" name="ifram"></iframe>
the a tag:
The javascript:
if($(this).val() == '242'){
document.getElementById('ifram').style.border='0px';
document.getElementById('ifram').style.background = "url('../product_images/uploaded_images/Flag.jpg')";
document.frames.ifram.document.body.style.backgroundColor="transparent";
This is just a snippet of a code and does not include the whole process of the ajax call to get the image but was not sure if the ajax is part of the issue. I get an undefined error in firefox but the function still fires . I am assuming I would just need to use window.frames for firefox.
Overall, any help on how to resolve this issue would be appreciated. I am wondering if there is a security issue that has to do with browser settings or if its part of how I coded.
Thanks in advance
It's important to note that if your iframe tag is on a page located at http://www.foo.com but the iframe points to http://foo.com it's considered a sandboxing violation and you'll get access denied. Make sure you're pointing to the same domain as your page is on. You can use relative URLs in the iFrame src tag, too, so you can change it to src="bar.php" (without any domain information).
You dont need to use document.frames before document.frames.ifram.document.body.style.backgroundColor = "#ccc";. Just ifram.document.body.style.backgroundColor = "#ccc"; will do.
I have some JavaScript code that dynamically injects an iframe in a given HTML page. Unfortunately, in Firefox, and only in Firefox, although the iframe is created from time to time the relevant URL isn't loaded into it.
I know it wasn't loaded because the relevant URL doesn't appear in the Firebug Net tab, and when I inspect the iframe I don't see any expected HTML code in there (when the iframe is on the same domain as the outlying page). I don't see any JavaScript or network errors either.
Here's a code snippet, I've checked all the relevant variables are correct:
var iframe = document.createElement("iframe");
iframe.width = options["w"];
iframe.height = options["h"];
iframe.scrolling = "no";
iframe.marginWidth = 0;
iframe.marginHeight = 0;
iframe.frameBorder = 0;
iframe.style.borderWidth = 0;
if (node.childNodes.length > 0)
node.insertBefore(iframe, node.childNodes[0]);
else
node.appendChild(iframe);
iframe.contentWindow.location = iframeSrc + "?" + querystring;
Here's an example URL that is set for the iframe (the issue also recreates when the URL points to an external server, had to omit the 'http://' at the beginning otherwise I couldn't post the question):
127.0.0.1:8000/widget/iframe/index.html?style=slide-top-to-bottom&culture_code=en_us&c=26&sc=1324&title=Top%20News&caption=Top%20Stories&order=relevance&count=20&w=250&h=300×tamp=true&scrollbar=false&theme=ui-lightness&className=8815455464592103&referrer=http%3A%2F%2F127.0.0.1%3A8000%2Fwidget%2Fbuilder%2Findex.html
Doing some research on the web, I found this unfixed Firefox bug which seems related to this issue:
https://bugzilla.mozilla.org/show_bug.cgi?id=279048
After reading the bug, I tried several solutions none of which solved the issue:
Setting iframe.src instead of iframe.contentWindow.location
Adding a random parameter to the querystring
Adding the '#' symbol with a random number at the end of the URL
Giving the iframe a random name
Does anyone have a workaround for this annoying Firefox bug? Or is the issue I'm describing unrelated to the bug and has a different solution?
What happens if you add this to the bottom of your script?
iframe.contentWindow.location.reload(true);
Perhaps it will stop the need to reload in FF.
EDIT
Fixed the example
Solved the issue, I was looking in the wrong place. The HTML file where this dynamic iframe was loaded had an empty iframe tag that was removed from the DOM, after which the dynamic iframe was injected instead.
Apparently Firefox cached the last URL for this iframe, and loaded it immediately as the external page loaded. I know because I saw the relevant HTML file being loaded twice in the Firebug Net tab rather than once upon the injection.
After I got rid of this empty iframe tag and relied only on the injected iframe, everything started to work well and the issue didn't reproduce anymore. I guess Firefox didn't like handling this scenario, some kind of bug maybe?
Thanks anyway for helping me out, it gave me the inspiration for the right solution :)
I tried to run a line of code as the following:
document.getElementById('frame0').contentDocument.location.reload(true); to force iframe to refresh or reload but I got the error like "permission denied" in firefox. Does anyone know why? and help to offer a solution? Thanks!
It is probably because of crossdomain issues - looks like your iframes content is from another domain as your mainframe (from which you run your js code). FF is very restrictive concerning crossdomains.
You can't reload a document that comes from a different hostname, due to the Same origin policy, which applies in all browsers.
You would have to remove the iframe from the page and replace it with a new one:
var iframe= document.getElementById('frame0');
var newiframe= document.createElement('iframe');
newiframe.src= iframe.src;
iframe.parentNode.replaceChild(newiframe, iframe);
However this will load the original src of the <iframe>, which won't be the same as the current location if the user has navigated the page since.