0

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 ?

Pointy
415k62 gold badges600 silver badges631 bronze badges
asked Jul 27, 2010 at 9:27

2 Answers 2

2

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".

answered Jul 27, 2010 at 9:38
Sign up to request clarification or add additional context in comments.

Comments

1

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.

answered Jul 27, 2010 at 9:40

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.