Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How do i make a .swf play only once during a user's session?

Posted on 2007-12-04
9
Medium Priority
?
532 Views
Last Modified: 2008-02-17
I have about 5 or 6 .swfs each on their own page. The client wants the swf to play only once during a user's session. So if they refresh - no play- if they surf around and come back - no play(there is a control bar, so if they want to see it again, they can). The swfs contain video of a woman talking about the content of the page.

How would i set that up? In the actionscript, javascript, cookies? If you need more info let me know.

I Really don't know.

Thanks
0
Comment
Question by:vinceq
  • 4
  • 4
9 Comments
 
LVL 36

Expert Comment

by:Zyloch
ID: 20407508
You would set this up with a Javascript cookie to remember whether the user has visited the page or not. Do you have a Flash movie that automatically runs the first time, and you want it to not run every other time but still display itself?
0
 

Author Comment

by:vinceq
ID: 20407589
Yes, that is correct. The client just wants the video to sit there until the play button is pushed.

Now on the home page, there is currently a cookie to see if they've visited before, if so, it directs them to a duplicate homepage but with a different swf. Would I just add to that cookie or write a new one. (I don't know cookies, so I obviously did not write the current cookie, but found it with a google search). I've attached the code

thanks.
<script type="text/javascript">
var mycookies = document.cookie;
var p = mycookies.indexOf("seenthis=");
var seenthis = (p!= -1)? mycookies.substring(p + 9, p + 10) : 0;
if (seenthis > 1 ) {
location.href = "welcomeback.html";
} else {
var farfuture = new Date();
farfuture.setFullYear(farfuture.getFullYear() + 10);
seenthis++;
document.cookie = "seenthis=" + seenthis + "; expires=" + farfuture.toGMTString();
}
</script>

Open in new window

0
 
LVL 36

Accepted Solution

by:
Zyloch earned 2000 total points
ID: 20407637
That is fine. Try using this code, where you should replace FLASH_MOVIE_ID with the correct id.
window.onload = function() {
 
    // COOKIE CODE
    var mycookies = document.cookie;
    var p = mycookies.indexOf("seenthis=");
    var seenthis = (p!= -1)? mycookies.substring(p + 9, p + 10) : 0;
    if (seenthis < 1) {
        // START FLASH MOVIE
        document.getElementById("FLASH_MOVIE_ID").Play();
 
        // SET NEW COOKIE
        var farfuture = new Date();
        farfuture.setFullYear(farfuture.getFullYear() + 10);
        seenthis++;
        document.cookie = "seenthis=" + seenthis + "; expires=" + farfuture.toGMTString();
    }
 
 
};

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 36

Expert Comment

by:Zyloch
ID: 20407641
Oh! You will need to disable the movie from playing automatically, so use the page that has the nonplaying flash movie.
0
 

Author Comment

by:vinceq
ID: 20407955
Zyloch thank you for your comments and quick response.

I plopped in your cookie code, but then realized:
don't i need to tell the new swf (welcomeback) where to play, or mark the current div containing the (welcome) swf? Or will this automatically swap out the swf.

I didn't specify that earlier. sorry.
0
 

Author Comment

by:vinceq
ID: 20408148
I stripped out everything except the flash detection and div where the swf resides. For context.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
 
<!--cookie to call welcomeback-->
<script type="text/javascript">
window.onload = function() {
 
    // COOKIE CODE
    var mycookies = document.cookie;
    var p = mycookies.indexOf("seenthis=");
    var seenthis = (p!= -1)? mycookies.substring(p + 9, p + 10) : 0;
    if (seenthis < 1) {
        // START FLASH MOVIE
        document.getElementById("welcomeback.swf").Play();
 
        // SET NEW COOKIE
        var farfuture = new Date();
        farfuture.setFullYear(farfuture.getFullYear() + 10);
        seenthis++;
        document.cookie = "seenthis=" + seenthis + "; expires=" + farfuture.toGMTString();
    }
 
 
};
</script>
<!--end cookie welcomeback-->
 
 
<script language="JavaScript" type="text/javascript">
<!--
// -----------------------------------------------------------------------------
// Globals
// Major version of Flash required
var requiredMajorVersion = 8;
// Minor version of Flash required
var requiredMinorVersion = 0;
// Revision of Flash required
var requiredRevision = 0;
// the version of javascript supported
var jsVersion = 1.0;
// -----------------------------------------------------------------------------
// -->
</script>
<script language="VBScript" type="text/vbscript">
<!-- // Visual basic helper required to detect Flash Player ActiveX control version information
Function VBGetSwfVer(i)
  on error resume next
  Dim swControl, swVersion
  swVersion = 0
  
  set swControl = CreateObject("ShockwaveFlash.ShockwaveFlash." + CStr(i))
  if (IsObject(swControl)) then
    swVersion = swControl.GetVariable("$version")
  end if
  VBGetSwfVer = swVersion
End Function
// -->
</script>
<script language="JavaScript1.1" type="text/javascript">
<!-- // Detect Client Browser type
var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
var isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;
jsVersion = 1.1;
// JavaScript helper required to detect Flash Player PlugIn version information
function JSGetSwfVer(i){
	// NS/Opera version >= 3 check for Flash plugin in plugin array
	if (navigator.plugins != null && navigator.plugins.length > 0) {
		if (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]) {
			var swVer2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
      		var flashDescription = navigator.plugins["Shockwave Flash" + swVer2].description;
			descArray = flashDescription.split(" ");
			tempArrayMajor = descArray[2].split(".");
			versionMajor = tempArrayMajor[0];
			versionMinor = tempArrayMajor[1];
			if ( descArray[3] != "" ) {
				tempArrayMinor = descArray[3].split("r");
			} else {
				tempArrayMinor = descArray[4].split("r");
			}
      		versionRevision = tempArrayMinor[1] > 0 ? tempArrayMinor[1] : 0;
            flashVer = versionMajor + "." + versionMinor + "." + versionRevision;
      	} else {
			flashVer = -1;
		}
	}
	// MSN/WebTV 2.6 supports Flash 4
	else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.6") != -1) flashVer = 4;
	// WebTV 2.5 supports Flash 3
	else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.5") != -1) flashVer = 3;
	// older WebTV supports Flash 2
	else if (navigator.userAgent.toLowerCase().indexOf("webtv") != -1) flashVer = 2;
	// Can't detect in all other cases
	else {
		
		flashVer = -1;
	}
	return flashVer;
} 
// If called with no parameters this function returns a floating point value 
// which should be the version of the Flash Player or 0.0 
// ex: Flash Player 7r14 returns 7.14
// If called with reqMajorVer, reqMinorVer, reqRevision returns true if that version or greater is available
function DetectFlashVer(reqMajorVer, reqMinorVer, reqRevision) 
{
 	reqVer = parseFloat(reqMajorVer + "." + reqRevision);
   	// loop backwards through the versions until we find the newest version	
	for (i=25;i>0;i--) {	
		if (isIE && isWin && !isOpera) {
			versionStr = VBGetSwfVer(i);
		} else {
			versionStr = JSGetSwfVer(i);		
		}
		if (versionStr == -1 ) { 
			return false;
		} else if (versionStr != 0) {
			if(isIE && isWin && !isOpera) {
				tempArray         = versionStr.split(" ");
				tempString        = tempArray[1];
				versionArray      = tempString .split(",");				
			} else {
				versionArray      = versionStr.split(".");
			}
			versionMajor      = versionArray[0];
			versionMinor      = versionArray[1];
			versionRevision   = versionArray[2];
			
			versionString     = versionMajor + "." + versionRevision;   // 7.0r24 == 7.24
			versionNum        = parseFloat(versionString);
        	// is the major.revision >= requested major.revision AND the minor version >= requested minor
			if ( (versionMajor > reqMajorVer) && (versionNum >= reqVer) ) {
				return true;
			} else {
				return ((versionNum >= reqVer && versionMinor >= reqMinorVer) ? true : false );	
			}
		}
	}	
	return (reqVer ? false : 0.0);
}
// -->
</script>
 
 
</head>
 
<body>
 
 
 
<div id="homepageflash">
 
<script language="JavaScript" type="text/javascript">
<!-- 
var hasRightVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);
if(hasRightVersion) {  // if we've detected an acceptable version
    var oeTags = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"'
    + 'width="250" height="240"'
    + 'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab">'
    + '<param name="movie" value="welcome_a.swf" /><param name="play" value="true"<param name="loop" value="false" /><param name="quality" value="high" /><param name="wmode" value="transparent" /><param name="bgcolor" value="#ffffff" />'
    + '<embed src="welcome_a.swf" loop="false" quality="high" wmode="transparent" bgcolor="#ffffff" '
    + 'width="250" height="240" name="welcome_a" align="middle"'
    + 'play="true"'
    + 'loop="false"'
    + 'quality="high"'
    + 'allowScriptAccess="sameDomain"'
    + 'type="application/x-shockwave-flash"'
    + 'pluginspage="http://www.macromedia.com/go/getflashplayer">'
    + '<\/embed>'
    + '<\/object>';
    document.write(oeTags);   // embed the flash movie
  } else {  // flash is too old or we can't detect the plugin
    var alternateContent = 'OOPS! You need the most current version of Macromedia Flash Player to view this.'
     	+ '<a href=http://www.macromedia.com/go/getflash/>Get Flash</a>';
    document.write(alternateContent);  // insert non-flash content
  }
// -->
</script>
<noscript>
	// OOPS! You need the most current version of Macromedia Flash Player to view this.'
     
  	<a href="http://www.macromedia.com/go/getflash/">Get Flash</a>  	
</noscript><script type="text/javascript" src="js/ieupdate.js"></script></div><!--end hompageflash -->
 
 
</body>
</html>

Open in new window

0
 
LVL 36

Expert Comment

by:Zyloch
ID: 20408683
In this case, you shouldn't need more than just one flash movie that defaults to not playing. Then, you need to change document.getElementById("welcomeback.swf").Play(), change the welcomeback.swf to and ID name. This is an id attribute you put in your <object> or <embed> tag.
0
 

Author Comment

by:vinceq
ID: 20413346
Zyloch, thanks again for your responsiveness. I am unclear about your last comment.
I think i was misleading, so i will try to clarify my statements.

Firstly on this index.html page, there is a 'welcome' movie that plays, the cookie sees if they have seen it before, if so they are directed to welcomeback.html, an identical home page, but with a 'welcome back' movie that plays. My question is two-fold i guess: The cookie..if they've seen the 'welcome' movie before, how do i load the 'welcome back' movie, without having to create another page.  Second, how do i get the swf to play only once during the user's visit, no matter how many times they jump back through the pages, the swf should only play once.

I hope this isn't too confusing.  thanks!
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question