Share via


Windows Media Player 11 SDK Maintaining Session Information 

Windows Media Player SDK banner art

Previous Next

Maintaining Session Information

How the Sample Stores Information

The sample Web page maintains data by using cookies. Cookies are simply persistent name/value pairs that the Web browser can store for you.

When the Web page closes, the Shutdown function runs. Shutdown calls the function SetPendingCookies. This function loops through the download collection list, stored in the drop-down list box named selDLC. If a download collection contains incomplete items, the function calls the method SetCookie, passing a name and a value. The name string is determined by appending an integer value to a constant value, WMPDLC, which is stored in the variable named g_sPreCookie. For instance, for the third pending download collection, the cookie name would be WMPDLC2, because the counting mechanism is zero-based. As each cookie is created, the function increments the global variable g_Pending to keep a count of pending downloads. The code is reproduced here for your convenience:

// Write cookies for pending downloads.
function SetPendingCookies()
{
    g_Pending = 0; // Init the pending count.
    
    for(var i = 0; i < selDLC.length; i++)
    {
        var sCookieName = g_sPreCookie + i.toString(10);
        var sCookieVal = selDLC.options[i].text;
    
        // Write a cookie for each pending download.    
        if(IsDLCComplete(sCookieVal.valueOf()) == false)
        {      
            SetCookie(sCookieName, sCookieVal);
            g_Pending++;
        }        
    }
}

IsDLCComplete is a helper function that simply loops through each download item in the download collection to determine whether any item is pending. If there is a pending item, the function returns false.

SetCookie is the function that creates the cookie.

When SetPendingCookies returns, Shutdown creates the count cookie, which stores the value from the variable g_Pending. This value is important because the Web page needs a way to determine how many cookies to retrieve when it reloads. The count cookie name is stored in the variable named g_sCountCookie.

How the Sample Retrieves Information

When the Web page loads, the Init function runs. First, the function calls GetPendingDlCount to retrieve the count cookie. Next, it stores this value in the variable named g_Pending. Then it calls the PopulateDLList function to retrieve each download session cookie and add its identifier to the drop-down list box. Here is the code for that function:

// Fill the drop-down list with pending download collection IDs.
function PopulateDlList(iCount)
{
    ClearList(selDLC);
     
    // For each cookie, add the value to the SELECT element.
    // The value represents a download collection ID.  
    for (var j = 0; j < iCount; j++)
    {
        var sCookieName = g_sPreCookie + j.toString(10);        
        var sCookieVal = GetCookie(sCookieName);
        DelCookie(sCookieName); // Don't leave the cookies lying around. 
  
        if(sCookieVal != null)
        {      
            var oOption = document.createElement("OPTION");
            oOption.text = sCookieVal;
            oOption.value = j;
            selDLC.add(oOption); 
        }          
    }
}

The function ClearList removes all items from the list box.

The function then enters a loop. Each cookie name is built as a string, and then the cookie is retrieved by calling the helper function GetCookie. Once retrieved, the cookie is deleted by calling DelCookie. If the cookie has a value, the value is added to the list box. This action initiates the onChange event for the selDLC list, which in turn calls the handler function named OnSelectDLC. This is the same function that executes when the user selects a download collection; it is the code that fills the download item list box.

See Also

Previous Next