views:

37

answers:

2

Hi i have a asp.net page which inherits form a master page where i load javascripts.I like to place a iframe withing the page which will point to a html(which contains the link to javascript) .Will my java scrips work ?

A: 

Javascript files imported by a page in an <iframe> will "work" just fine, provided you understand their execution context. The <iframe> provides a separate page context from the parent page, with its own DOM. The <iframe> DOM is linked to the parent DOM, and the parent DOM is linked down to the <iframe>, if the pages come from the same domain.

What that means is that from the <iframe>, if your code assumes it's in it's very own window, it will probably just work fine (or have whatever bugs it has :-) However, it will not be able to use something like document.getElementById("foo") to find elements in the parent page. To cross page boundaries the code will have to explicitly use the window.parent link to get to the containing page.

I guess what I'm saying is that you must understand that <iframe> is not like a "import" or "#include" facility for the parent page. It's a way of creating a complete "sub-page".

Pointy
A: 

Yes, JavaScript can run inside an iframe. However, realize that you're working with different document/window objects, and the window objects are different, so to communicate from iframe to parent, you'd have to use window.parent to get the parent window's window object.

You are working with different contexts. Javascript code running in an iframe can be thought of as javascript code running isolated, if you ignore window.parent.

Seems like a wordy answer, I can't really explain better.

ItzWarty