Using JavaScript to change <object> tag attributes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • etabetapi
    New Member
    • Aug 2008
    • 2

    Using JavaScript to change <object> tag attributes

    I'm trying to build a simple gallery using some thumbnail images to load larger images and SWF files by swapping out the source hrefs. It works okay with images, but I'm having trouble doing the same thing with SWFs (Note: Both galleries exist on separate pages so there is no confusion.) Here is the function that works on the image gallery page:

    Code:
    function showPic (whichpic) 
    {
     if (document.getElementById) 
    	{
    		  document.getElementById('placeholder').src = whichpic.href;
            }
    return false;
    }
    I tried adapting it for the Flash gallery page like this (Note: In preview post, I'm seeing a big space on line 6 between the 'mo' and 'vie' in after setAttribute. This is a weird formatting error that caused by the forum, and doesn't exist inside my code.):

    Code:
    function showFlash (whichFlash)
    {
    	if(getElementByID)
    	{
    		document.getElementByID('placeholder').data = whichFlash.href;
    		document.getElementById('swfsrc').setAttribute('movie', whichFlash.href);
    	}
    return false;
    }
    I'm implementing my SWFs using the Flash Satay method which strips out the embed tag because it's not W3C compliant.

    Code:
    <object
            id="placeholder"
    	type="application/x-shockwave-flash" 
            data="../media/dresscat.swf"
    	width="700" height="300">
            <param name="movie" value="../media/dresscat.swf" id="swfsrc" />
    </object>
    Thumbnail link that calls the function:
    Code:
    <a onclick="return showFlash(this)" href="../media/mia_flash_gallery.swf">image</a>
    What I want to happen:

    When the thumbnail is clicked, the SWF with the ID 'placeholder' should be replaced with the SWF referenced in the thumbnail link, so the old SWF file is "swapped" for the new one.

    What actually happens:

    When the thumbnail is clicked, a page containing only the SWF is loaded in full frame.

    Here's an example of the working image gallery on my site.
    Thanks in advance.
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Add a return false in the showFlash function otherwise the link href URL will be followed.

    Comment

    • etabetapi
      New Member
      • Aug 2008
      • 2

      #3
      Originally posted by acoder
      Add a return false in the showFlash function otherwise the link href URL will be followed.
      Thanks, I actually already had that in there, but for some reason I omitted it when I posted.

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Code:
        if (getElementByID)
        should be [code=javascript]if (document.getEl ementById)[/code]and JavaScript is case-sensitive so it's a small 'd', not ID.

        Comment

        Working...