469,329 Members | 1,453 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,329 developers. It's quick & easy.

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

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
5 1090
acoder
16,027 Expert Mod 8TB
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
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
16,027 Expert Mod 8TB
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
"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
16,027 Expert Mod 8TB
"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.

Similar topics

3 posts views Thread by Hugh G. Johnson | last post: by
1 post views Thread by Andy Roger | last post: by
5 posts views Thread by VB Programmer | last post: by
3 posts views Thread by GSL | last post: by
2 posts views Thread by jordan.weberflink | last post: by
5 posts views Thread by pbd22 | last post: by
reply views Thread by Purva khokhar | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.