Results 1 to 4 of 4

Thread: Javascript - Passing object id to function?

  1. #1

    Thread Starter
    Fanatic Member Graff's Avatar
    Join Date
    Jan 2002
    Location
    Calgary
    Posts
    668

    Javascript - Passing object id to function?

    I have a function that loads an html file into an iframe

    Code:
    function LoadIFrame(file) {
    	ContentWindow.innerHTML = "<iframe width=455 height=446 src='" + file + ".htm' frameborder='0'></iframe>";
    }
    in my code I have an area

    Code:
    <div id="ContentWindow"></div>
    And when someone clicks on a image link
    Code:
    <a href="#"><img src="images/News.jpg" id="News" onClick="LoadIFrame('News');window.focus();"></a>

    Now all this works just as I expect. But what I want to is to have more than one IFrame that can be loaded so I want to be able to pass in which area the iframe should be loaded

    Code:
    function LoadIFrame(file,frame) {
    	frame.innerHTML = "<iframe width=455 height=446 src='" + file + ".htm' frameborder='0'></iframe>";
    }
    Code:
    <a href="#"><img src="images/News.jpg" id="News" onClick="LoadIFrame('News','ContentWindow');window.focus();"></a>
    However when I try this it no longer works. I'm fairly new to Javascript, so is there some I can do so I can pass in the ID to make this possible or would I have to make some sort of if else statement (although that would be undesirable since I would like to make this as flexible as possible and work with any site I'm working on)
    If wishes were fishes we'd all cast nets.

  2. #2

    Thread Starter
    Fanatic Member Graff's Avatar
    Join Date
    Jan 2002
    Location
    Calgary
    Posts
    668
    got it

    Code:
    function LoadIFrame(file, frameid) {
    	var divElement = document.getElementById(frameid);
    
    	divElement.innerHTML = "<iframe width=455 height=446 src='" + file + ".htm' frameborder='0'></iframe>";
    }
    If wishes were fishes we'd all cast nets.

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Even better would be rmoving the div, having the iframe all the time and setting the src property via JavaScript.

    Code:
    function LoadIFrame(id, src)
    {
      var frame = document.getElementById(id);
    
      if(frame)
        frame.src = src;  // better DOM would be
         // frame.setAttribute("src", src);
         // but there's insufficient browser support for that.
    }
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4

    Thread Starter
    Fanatic Member Graff's Avatar
    Join Date
    Jan 2002
    Location
    Calgary
    Posts
    668
    thanks for the tip
    If wishes were fishes we'd all cast nets.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width