SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    May 2004
    Location
    Quebec, Canada
    Posts
    101
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Bad javascript detection of Flash in IE?

    I'm working on a Flash site with a routine to detect the Flash version installed (not mine, I inherited the site to complete it). Some people get "[object]" when loading the first page on IE (and I don't, it kinda drives me crazy).

    I think it has something to do with this part of the Flash validation routine :

    Code:
    var tempObj = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
    var arrVersion = tempObj.GetVariable("$version").split(" ")[1].split(",");
    installedVersion = parseInt(arrVersion[0]) || 0;
    It seems the script stalls after creating the ActiveX object... On my IE 6, it seems to always fail the test (not detecting any Flash plugin) and on other computers with IE 6, it seems to popup a Javascript "[object]" message (though I'm not sure it's because of this part of the code, I could post the whole script if need be).

    Anyone know what this is about? Could there be something about the new IE restrictions on ActiveX objects insertion that prevents the object from being used in the Flash version test? Thanks!

  2. #2
    SitePoint Zealot
    Join Date
    May 2004
    Location
    Quebec, Canada
    Posts
    101
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, here's the full code to the Flash detection routine. If anyone sees something that would cause the "[Object]" message in IE, please let me know. Also, this is called with :
    showFlash("main.swf", "100% : 900", "100% : 570", 8, "r : upgrade");

    Code:
    ////////////////////////////////////////////////////////////////////////////
    // Flash Detect 0.9.1
    // Flash embed/object printout and detect
    /*
    USAGE
    showFlash(movie url, movie width, movie height, required flash player version, action if player requirements are not met)
    showFlash("test.swf", "100%", "200", 8, "d : flash_upgrade");
    showFlash("test.swf", "100%", "200");  //will only display the swf without any version check
    showFlash("test.swf", "100% : 800", "100% : 600", 8, "d : flash_upgrade");  //will bypass h and w if the size of the window is too small to allow scrollbars
    
    
    ACTIONS
    
    r : upgrade.html
    redirect to this page. the redirect page will have a querystring containing the required and installed
    versions of flash as follows:  upgrade.html?r=8&i=7 . They can be read back with: querystring["r"] if 
    you include this .js again in the redirect page.
    
    i : image.jpg : http://www.google.com
    show an image
    	i : image.jpg  (<a> tag link to the default macromedia upgrade page
    	i : image.jpg : none  (no <a> tag)
    	i : image.jpg : http://www.google.com  (<a> tag link to the specified URL)
    
    d : flash_upgrade
    shows the hidden div tag of the provided id
    ex:   <div id="flash_upgrade" style="display:none">This is the upgrade div tag</div>
    
    h : <i>this is some crazy HTML message</i>
    HTML output. ATTN: do not use  " : " in the HTML block, or it will truncate your message.
    
    
    no action
    do nothing even if the player doesn't pass the check
    
    
    NOTES
     - Always put the upgrade div before the showFlash() call
     - You can use the querystring param:  index.html?bypassFlashCheck=true   to bypass all flash checks in a page
     - div tag output name: fd_div_1, fd_div_2, fd_div_3, etc.  Number is attributed in order of appearance.
     - swf name : fd_swf_1, fd_swf_2, fd_swf_3, etc.  Number is attributed in order of appearance. (Same as it's parent DIV)
     
    */
    ////////////////////////////////////////////////////////////////////////////
    function showFlash(movie, w, h, required_version, action){
    	
    	//constants
    	var codebase = "http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version="+required_version+",0,0,0";
    	var player_download = "http://www.macromedia.com/go/getflashplayer";
    
    	//parse action string
    	(action != null) ? action = action.split(" : ") : action = "";
    	
    	//flash versions
    	if (required_version == null) required_version = 0;
    	var installed_version = getFlashVersion();
    
    	//--check flash version
    	if (installed_version >= required_version || get_param("bypassFlashCheck") == "true"){
    		//Flash player OK or Bypass == true, printout the swf tag
    		var output = "<div id='fd_div_"+flash_detect_counter+"' style='width:"+ calculateSize(w, "w") +"; height:"+ calculateSize(h, "h") +"'>";
    		output += "<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='"+codebase+"' id='fd_swf_"+flash_detect_counter+"' width='100%' height='100%' align='top' hspace='0' vspace='0'>";
    		output += "<param name='movie' value='"+movie+"'><param name='quality' value='high'><param name='menu' value='false'> <param name='swLiveConnect' value='true'>";
    		output += "<embed src='"+movie+"' width='100%' height='100%' align='top' hspace='0' vspace='0' quality='high' swLiveConnect='true' pluginspage='"+player_download+"' type='application/x-shockwave-flash' menu='false' name='fd_swf_"+flash_detect_counter+"'></embed></object></div>";
    		document.write(output);
    
    		//little hack to refresh content, fixes a bug in IE when used inside a table
    		document.getElementById("fd_div_"+flash_detect_counter).innerHTML = document.getElementById("fd_div_"+flash_detect_counter).innerHTML;
    		
    		//remember the initial w/h settings
    		document.getElementById('fd_div_'+flash_detect_counter).flash_detect_w = w;
    		document.getElementById('fd_div_'+flash_detect_counter).flash_detect_h = h;
    
    	}else if(action[0] == "r"){
    		//redirect to upgrade page
    		location.href = action[1] + "?r=" + required_version + "&i=" + installed_version;
    
    	}else if(action[0] == "i"){
    		//show image
    		if (action[2] == "none"){
    			//print out image with no <a> tag
    			document.write("<img src='"+action[1]+"' border=0 />");
    		}else{
    			//if no URL specified, use the default macromedia download
    			//else, use the one specified
    			if (action[2] == null) action[2] = player_download;
    			document.write("<a href='"+action[2]+"' target='_blank'><img src='"+action[1]+"' border=0 /></a>");
    		}
    	}else if (action[0] == "d"){
    		//show hidden div content
    		document.getElementById(action[1]).style.display="block";
    	}else if (action[0] == "h"){
    		//output raw HTML
    		document.write(action[1]);
    	}else{
    		//the player is not passing the test but no action has been defined: do nothing
    	}
    
    	//increment flash counter
    	flash_detect_counter++;
    
    }
    
    //////////////////////////////////////////////////////////////////////////////
    // Flash detection
    function getFlashVersion(){
    	try{
    		var installedVersion = 0;
    		var b = navigator.userAgent.toLowerCase();
    
    		if ( (b.indexOf('msie') != -1) && (b.indexOf('win') != -1) && (b.indexOf('opera') == -1) ) {
    			//IE
    			var tempObj = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
    			var arrVersion = tempObj.GetVariable("$version").split(" ")[1].split(",");
    			installedVersion = parseInt(arrVersion[0]) || 0;
    		} else {
    			//everything else
    			if (navigator.plugins["Shockwave Flash"]) {
    				var pluginDesc = navigator.plugins["Shockwave Flash"].description;
    				installedVersion = parseInt( pluginDesc.charAt( pluginDesc.indexOf(".")-1 ) );
    			}
    			
    			//webTV
    			if(b.indexOf("webtv/2.6") != -1) installedVersion = 4;  
    			if(b.indexOf("webtv/2.5") != -1) installedVersion = 3;  
    			if(b.indexOf("webtv") != -1) installedVersion = 2;  
    		}
    	}catch(e){
    		//alert(e.name + "\n" + e.message);
    	}finally{
    		return installedVersion;
    	}
    }
    
    //////////////////////////////////////////////////////////////////////////////
    // Window Resize Check
    function recheckSize(){
    	//batch-check all movies in the page
    	for (var i=1; i<flash_detect_counter; i++){
    		//change the DIV tag's property if this object exists
    		if (document.getElementById('fd_div_'+i)){
    			//get size to use
    			var w = calculateSize(document.getElementById('fd_div_'+i).flash_detect_w , "w");
    			var h = calculateSize(document.getElementById('fd_div_'+i).flash_detect_h , "h");
    
    			document.getElementById('fd_div_'+i).style.width = w;
    			document.getElementById('fd_div_'+i).style.height = h;
    		}
    	}
    }
    
    //////////////////////////////////////////////////////////////////////////////
    // Returns good value to use with height and width
    function calculateSize(val, type){
    	//type can be "w" or "h"
    	(type=="w") ? type="clientWidth" : type="clientHeight" ;
    
    	//parse and check minimum display size
    	var val = val.split(" : ");
    
    	//if the first value is a percent value, use it to modify comparison
    	var t_val = val[0].split("%");
    	(t_val[0] != "" && t_val[1] != undefined) ? t_val = (t_val[0] / 100) : t_val = 1;
    		
    	//compare window
    	(val[1] > (document.body[type] * t_val) || val[1] == "") ? val=val[1] : val=val[0];
    	
    	return val;
    }
    
    ////////////////////////////////////////////////////////////////////////////
    // Test swLiveConnect
    function swLiveConnectTest(flashObj, msg){
    	//window.document.fd_swf_2.Play();
    	window.document[flashObj].SetVariable("myTxt.text", msg);
    }
    
    ////////////////////////////////////////////////////////////////////////////
    // Querystring
    function get_querystring(){
    	var params = window.location.search.substring(1).split("&");
    	var querystring=[];
    
    	for (var i in params) {
    		var pos = params[i].indexOf("=");
    		if (pos > 0) querystring[params[i].substring(0,pos)] = params[i].substring(pos+1);
    	}
    	
    	return querystring;
    }
    
    function get_param(param){
    	//get querystring and return param to compare
    	return get_querystring()[param];
    }
    
    
    //////////////////////////////////////////////////////////////////////////////
    // MAIN
    
    window.onresize = recheckSize; //init resize handler
    var flash_detect_counter = 1; //init ID counter flash DIV and Flash object unique names
    Thanks for any help on this!

  3. #3
    SitePoint Zealot
    Join Date
    May 2004
    Location
    Quebec, Canada
    Posts
    101
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, since this isn't going anywhere (), how about a good Flash detection routine? One that detects Flash with it's version (I need Flash Player 8) and if not there or not right version, redirects to a "downloads" page. The script must take into consideration the latest security issues with IE 6+ and ActiveX objects. Thanks!

  4. #4
    Take The Blue Pill neilmerton's Avatar
    Join Date
    Jul 2004
    Location
    Leicester, UK
    Posts
    871
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    swfObject does quite a good job:

    http://blog.deconcept.com/swfobject/

  5. #5
    SitePoint Zealot
    Join Date
    May 2004
    Location
    Quebec, Canada
    Posts
    101
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Looks mighty fine, thanks!


Bookmarks

Posting Permissions

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