0

I have an iframe issue ( little bit strange for me ) . The issue is that i have an iframe in my document, and there are several functions are operating different task on that iframe and for accessing the contents of iframe we use :

$("iframe").contents();

So instead of writing this long statement i used a global variable :

var i = $("iframe").contents();

But this is not working well, like

alert( i.find("someelement") );    

=> undefined

alert($("iframe").contents().find("someelement")    

=> [object]

Whats the problem here?

1
  • paste your complete code like where have you declared i Commented Jun 8, 2012 at 7:10

3 Answers 3

1

Replace

var i = $("iframe").contents();

with

window.i = $("iframe").contents();

As i has a lesser scope than window, which is used to declare global variables in JavaScript.

But one more thing is, you cannot modify any items inside the iframe, if it is from a different domain. Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

0

You are probably overwriting i somewhere, or it's not global. To ensure it's global, don't use var. Instead, define window.i = ...; and always refer to window.i. This avoids possible confusion with a local variable (which can happen quite often given that i is almost always used as an iterator variable). Also make extra sure you always declare i as local if you're using it for anything but the iframe (such as the aforementioned iterator).

Comments

0

Is the content of your iframe on the same domain as the page where it is interted ?

More: http://en.wikipedia.org/wiki/Same_origin_policy

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.