dcsimg
www.webdeveloper.com
Results 1 to 2 of 2

Thread: Handling a javascript variable with Flash

  1. #1
    Join Date
    Dec 2009
    Posts
    17

    Handling a javascript variable with Flash

    The following bit of javascript writes and retrieves a cookie which counts a users visits to the page. Then using swfobject it both embeds main_banner.swf and passes the cookie value "visits" to it via flashvars. The trouble is in the actionscript below; the vallue has been passed (this is certain, because the .swf shows up rather than the alternative content)- but the .swf is stuck on the first frame rather than processing the cookie and sending the user to the appropriate frame label. If anyone has any suggestion on how to write the receiving code, I would definitely appreciate it.



    Thanks, Ryan





    <!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" lang="en" xml:lang="en">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    <script src="http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js"></script>

    <script>

    function createCookie(name,value,days) {
    var expires = "";
    if(days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    expires = "; expires="+date.toGMTString();
    }
    document.cookie = name+"="+value+expires+"; path=/";
    }

    function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0) === ' '){
    c = c.substring(1,c.length);
    }
    if (c.indexOf(nameEQ) === 0){
    return c.substring(nameEQ.length,c.length);
    }
    }
    return null;
    }

    var counter = readCookie("counter");
    var prior_visits = (counter !== null) ? parseInt(counter, 10) : 0;
    var current_visit = prior_visits +1;

    createCookie("counter", current_visit, 365);

    //if(prior_visits === 0){
    // alert("This is your first visit");
    //} else {
    // alert("This is visit #" + current_visit);
    //}

    var flashvars = {
    visits: current_visit
    };

    swfobject.embedSWF("main_banner.swf", "myContent", "189", "151", "9.0.0", false, flashvars);

    </script>


    </head>
    <body>

    <div id="myContent">
    <h1>Alternative content</h1>
    <p><a href="http://www.adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p>
    </div>
    </body>
    </html>



    ACTIONSCRIPT STARTS HERE - This is on the first frame of a 5-frame FLA file. The following 4 frames are labeled (see below) and have a singular function of loading an external .swf

    EX. -- loadMovie("white_papers_banner.swf", "myContainer_mc");
    -- this._lockroot = true;

    -- stop();



    var visits: String ="0,1,2,3";



    if (visits == 0) {
    this.gotoAndPlay("investment_focus");
    }

    if (visits == 1) {
    this.gotoAndPlay("white_papers");
    }

    if (visits == 2) {
    this.gotoAndPlay("portfolio_strategy");
    }

    if (visits == 3) {
    this.gotoAndPlay("third_thursday");
    }

    stop();

  2. #2
    Join Date
    Dec 2009
    Posts
    17

    Problem solved in Flash

    The actionscript was incorrect. Please see below. After several weeks of trial and error, I believe this, in conjunction with my prevous post, is the simplest way to pass a cookie value to Flash on the fly. Thanks to everyone who looked at this. Best, Ryan

    var visits:String;//


    if(visits == "1"){
    this.gotoAndPlay("investment_focus");
    }


    if(visits == "2"){
    this.gotoAndPlay("white_papers");
    }

    if(visits == "3") {
    this.gotoAndPlay("portfolio_strategy");
    }

    if(visits == "4") {
    this.gotoAndPlay("third_thursday");
    }

    stop();

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles