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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Revathi Balakrishnan
    New Member
    • Jun 2008
    • 40

    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



    Code:
    <script lanquage="Javascript">
    
    
    if(window.document.getElementById('1').title=="rightnav")
    alert("got it");
    
    
    
    
    </script>
    
    
    <iframe id=1 title="rightnav"src="http://www.google.com"></iframe>
    Last edited by acoder; Aug 9 '08, 06:03 PM. Reason: Added [code] tags
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    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:
    [code=javascript]if (document.getEl ementById("ifra meID")) {
    ...[/code]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.

    Comment

    • Revathi Balakrishnan
      New Member
      • Jun 2008
      • 40

      #3
      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.re load is null or not an object error in Ie, in firefox i am getting nam.location has no properties error.

      Code:
      
      <script  type="text/JavaScript">
      
      function refreshme()
      {
      
      
      for(i=0; (a = document.getElementsByTagName("iframe")[i]); i++) 
      {
        
      nam=a.getAttribute('name');
      
      alert(nam);
      
      if(nam)
      
       nam.location.reload();
      
      }
      
      }
      </script>
      	
      		
      		<a  href="#" onClick="javascript:refreshme();"> refresh</a>
      		<iframe src="http://www.google.com" name="doc"></iframe>
      
                <iframe src="http://www.w3schools.com" name="doc1"></iframe>


      i could get an alert, the alert properly gives me the frame name, and using doc.location.re load(); 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.
      Last edited by acoder; Aug 11 '08, 03:22 PM. Reason: fixed code tags

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        nam is the name of the frame, not the frame itself. That's what you've set as variable a. So use a.location.relo ad() instead.

        PS. for code tags use square brackets [], not the angle brackets, <>.

        Comment

        • Revathi Balakrishnan
          New Member
          • Jun 2008
          • 40

          #5
          "a.location.rel oad" does not work,
          but a.contentWindow .location.reloa d();
          works fine.

          Thanks for the help. really happy to a memeber of this forum.

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Originally posted by Revathi Balakrishnan
            "a.location.rel oad" does not work,
            but a.contentWindow .location.reloa d();
            works fine.
            That would be for IE. If you want cross-browser code in one line, use window.frames[frameName] syntax.

            Originally posted by Revathi
            Thanks for the help. really happy to a memeber of this forum.
            Glad to hear it!

            Comment

            Working...