By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,605 Members | 2,147 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,605 IT Pros & Developers. It's quick & easy.

trying to identify whether a particular iframe is existing in a page

P: 40
i have used the below code to find out , whether the below named "rightnav" iframe is existing. But when i use this code it throws object required error. Expecting your help on this



Expand|Select|Wrap|Line Numbers
  1. <script lanquage="Javascript">
  2.  
  3.  
  4. if(window.document.getElementById('1').title=="rightnav")
  5. alert("got it");
  6.  
  7.  
  8.  
  9.  
  10. </script>
  11.  
  12.  
  13. <iframe id=1 title="rightnav"src="http://www.google.com"></iframe>
Aug 9 '08 #1
Share this Question
Share on Google+
5 Replies


acoder
Expert Mod 15k+
P: 16,027
There are two problems. One is that you're testing before the iframe even appears in the source. Secondly, by using a property to test, you're assuming the object already exists.

To test the existence of an element/object, use something like:
Expand|Select|Wrap|Line Numbers
  1. if (document.getElementById("iframeID")) {
  2. ...
onload, i.e. after all elements have loaded.

There are two other problems with your code. One is that the script language attribute is deprecated. Use the type attribute instead: type="text/javascript". Secondly, IDs should not begin with a number.

Finally, please use code tags when posting code. See How to Ask a Question. Thanks.
Aug 9 '08 #2

P: 40
hi i have understood my problem in the previous question, i have modified my my code but still i am getting "variable has no properties error", hope you would give me your valuable advice.


To automatically refresh all the iframes in my web page


i have tried to get my iframes's name in a variable called nam, and using that variable i am trying to refresh it, but i am getting nam.location.reload is null or not an object error in Ie, in firefox i am getting nam.location has no properties error.

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. <script  type="text/JavaScript">
  4.  
  5. function refreshme()
  6. {
  7.  
  8.  
  9. for(i=0; (a = document.getElementsByTagName("iframe")[i]); i++) 
  10. {
  11.  
  12. nam=a.getAttribute('name');
  13.  
  14. alert(nam);
  15.  
  16. if(nam)
  17.  
  18.  nam.location.reload();
  19.  
  20. }
  21.  
  22. }
  23. </script>
  24.  
  25.  
  26.         <a  href="#" onClick="javascript:refreshme();"> refresh</a>
  27.         <iframe src="http://www.google.com" name="doc"></iframe>
  28.  
  29.           <iframe src="http://www.w3schools.com" name="doc1"></iframe>
  30.  
  31.  
  32.  
  33.  
  34.  


i could get an alert, the alert properly gives me the frame name, and using doc.location.reload(); is also working for me, but i am not able to make use of the variable which has the frame name to refresh the the same frame.
Aug 11 '08 #3

acoder
Expert Mod 15k+
P: 16,027
nam is the name of the frame, not the frame itself. That's what you've set as variable a. So use a.location.reload() instead.

PS. for code tags use square brackets [], not the angle brackets, <>.
Aug 11 '08 #4

P: 40
"a.location.reload" does not work,
but a.contentWindow.location.reload();
works fine.

Thanks for the help. really happy to a memeber of this forum.
Aug 12 '08 #5

acoder
Expert Mod 15k+
P: 16,027
"a.location.reload" does not work,
but a.contentWindow.location.reload();
works fine.
That would be for IE. If you want cross-browser code in one line, use window.frames[frameName] syntax.

Thanks for the help. really happy to a memeber of this forum.
Glad to hear it!
Aug 12 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.