iframe InnerHTML Editing with JS - javascript

Is there any way to edit the innerHTML of an iframe after you have used .location?

If the new page is in your domain, then after it has loaded¹ you can access its .contentDocument property, which is a separate document that refers to the page that you just loaded. If the new page isn't in your domain then you can't of course because that would be a cross-site scripting violation.
¹Typically by waiting for its load event, but your new page can also call a function in your parent page by using the window.parent property.

Related

Javascript call from swf in iframe

I have a javascript function is called from AS3 when all are in same html document, it's all succesfull.
But when i put the swf in iframe, I get an error like ;
Unsafe JavaScript attempt to access frame with URL http://example.com/ from frame with URL http://example2.com. Domains, protocols and ports must match.
Iframe and parent documents have diffent domains. How can I fix it? In this case one of requirements is it should work with also in iframe. So putting all of them in same frame is not solution.
As Philipp has commented, this is being blocked by the browser's cross-scripting protection (the SWF element in this is merely a distraction - any javascript in the child iFrame will be blocked from calling the parent frame, if they do not share the same domain).
Your parent frame should have access to the child iframe, however, so you could set a parameter in the child iframe from your swf and poll for that from the parent frame.
Otherwise it's trivial to create a server-side script in PHP or .NET or whatever you're comfortable with, and bounce a value off that.

Update URL from inside an iFrame

is it possible for javascript inside an iFrame to update the URL (hash) of the parent page (and retrieve it)
Does it have any permissions?
To further explain, I have no hosting for this domain, I can only set up an Iframe. I also cannot use a DNS config to get that page to display because of limitations of my hoster.
I also cannot transfer the domain to them to make that work because my clients wants to keep control of the domain.
Thank you for your help!
If the <iframe> page is within the same domain, probably yes. Otherwise you don't get access to the parent page due to cross-domain restrictions.
You can change the URL of the parent page though:
top.location.href = 'http://www.example.com';
due to security constraints you will not be able to access properties of the parent window IF the domain,port or protocol is different than the one in the iframe.
To be short, the answer is NO.
Your script works only inside the context of that iframe.
If you try for example,
var loc = document.location;
you will see what I mean.
One solution is that when you give the other side your iframe, you should add a script in witch you can do whatever you want, because it runs on their domain.
Maybe dynamically create the source of your iframe and stuff.

How to use $(window).delegate('selector','resize',function handler) when selector is into an iFrame?

I need to use as a selector an element from DOM inside an iFrame.
Thanx in advance.
You can not access anything in an iframe with JavaScript running outside this frame.
Browsers has a same origin policy to prevent security problems like phishing
(If you host the content of this iframe your self you may have a look at jQuery load() to load HTML chunks into your page)

Call javascript method defined in parent from iframe

After some research (even at stackoverflow) I still can't figure out how to do this. parent.method() won't do the trick, nor some other solutions I've tried.
Situation: I have a index.html on the client side (mobile phone in this case) which has an iframe loading server-side page. What I need to do is call a javascript method defined in the index.html (client side) from the iframe content (server-side).
As an example (I'm not using android in the question described above), Android apps have addJavascriptInterface which, when defined, allows one to call methods defined client-side from server-side pages just invoking window.CustomObject.MethodToCall().
Any hint?
Thanks!
window.top.foo
for the top level window
window.parent.foo
for the direct parent
I realize I am only a year late to this party but there was no real answer.
So, in order to do this both files must be on the same domain. Since you have the index.html on the phones localhost and load a page on your site it will not work (locahost to example.com). You could load the index.html off your site as well and that would fix this problem (example.com to example.com). Then you could reference the parent frame in the normal window.top.function.
In certain situation there could be a neccessity of calling a javascript function inside an iframe from the parent document, and vice versa ie;
calling a javascript function in parent document from the iframe.
For example; the parent document have an iframe with id attribute ‘iFrameId‘, and the function ‘functionInIframe()‘ is defined in that iframe document.
Following code can call that iframe function from the parent document itself.
document.getElementById('iFrameId').contentWindow.functionInIframe();
And following code can call the function defined in parent document(functionInParent()) from the iframe itself.
parent.functionInParent();
This way javascript can interact between parent document and iframe.
This is the original post.

Can I access contents of an iframe from a subdomain if I use www. on the main domain?

I'm trying to access and edit the contents of an iframe. The iframe points to a page on a subdomain on my own domain. This is the javascript code I'm using, although it doesn't really matter.
$('iframe').load(function(){
$('div.code textarea.html').val($(this).contents()[0].html());
});
When I run it it says I don't have permission to access example.domain.com from www.domain.com. So does it matter if the domain I try to access from has the www? Because my host does not allow me to not use www.
Same-Origin-Policy requires the exact same hostname by default.
To tell it not to, set:
document.domain= 'domain.com';
from script in both the parent (www.) document and the iframe (example.) document.
Note that setting an onload of a statically-written iframe (or image) from script is unreliable, as it is conceivable that the iframe might get completely loaded in between the time the parser reads the iframe tag and when it reads the script tag that sets the onload.
To avoid this, either include the event handler as an inline ‘<iframe onload="doSomething()"’ attribute (one of the few places where inline event handling has some purpose), or, if acceptable for accessibility, create the iframe element itself from script, setting the onload before writing the src and adding it to the page.

Categories