Click here to Skip to main content
15,893,722 members
Articles / Programming Languages / Javascript

Integrating Silverlight with the Browser's Back Button

Rate me:
Please Sign up or sign in to vote.
4.00/5 (3 votes)
11 May 2009CPOL3 min read 29.1K   15  
Describes how to make Silverlight applications behave like a traditional web application - with navigation states that use the browser's back/forwards navigation buttons.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="SilverlightBrowserNavigation.Web._Default" %>
<%@ Register Assembly="System.Web.Silverlight" Namespace="System.Web.UI.SilverlightControls" TagPrefix="asp" %>

<!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 id="Head1" runat="server">
    <title>Silverlight/Browser Navigation</title>
<style type="text/css">
html, body { height:100%; margin:0px; overflow:hidden }

form#mainForm { height:100%; }

#yui-history-iframe {
  position:absolute;
  top:0; left:0;
  width:1px; height:1px;
  visibility:hidden;
}
</style>
<script type="text/javascript">
    var g_initialState = null, g_controlHasLoaded = false;

    function GetInitialState() {
        g_controlHasLoaded = true;
        return g_initialState;
    }
</script>
</head>
<body>
    <form id="mainForm" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
        <asp:Silverlight ID="silverlightControl" runat="server" Source="~/ClientBin/SilverlightBrowserNavigation.xap" MinimumVersion="2.0.31005.0" Width="100%" Height="100%" />
        <iframe id="yui-history-iframe" src="blank.htm"></iframe>
        <input id="yui-history-field" type="hidden"/>
    </form>
    <script type="text/javascript" src="yui/yahoo-dom-event.js"></script>
    <script type="text/javascript" src="yui/history-min.js"></script>
<script type="text/javascript">
    function LoadContent(state) {
        try {
            YAHOO.util.History.navigate("q", state.toString());
        } catch (e) {
            _LoadContent(state);
        }
    }
    
    function _LoadContent(state) {
        if (g_controlHasLoaded) {
            var ctrl = document.getElementById("silverlightControl");
            ctrl.Content.jsBridge.LoadState(state);
        } else
            g_initialState = state;
    }

    function _Init() {
        var state = YAHOO.util.History.getCurrentState("q");
        if (typeof (state) == "string") {
            if (g_controlHasLoaded)
                _LoadContent(state);
            else
                g_initialState = state;
        }
    }

    YAHOO.util.Event.onDOMReady(new function() {
        var bookmarkedState = YAHOO.util.History.getBookmarkedState("q");
        var queryState = YAHOO.util.History.getQueryStringParameter("q");
        var initialState = bookmarkedState || queryState || "";

        YAHOO.util.History.register("q", initialState, function(state) {
            _LoadContent(state);
        });

        YAHOO.util.History.onReady(function() {
            _Init();
        });

        // Initialize the browser history management library.
        try {
            YAHOO.util.History.initialize("yui-history-field", "yui-history-iframe");
        } catch (e) {
            _Init();
            // web navigation will not be available
        }
    });
</script>
</body>
</html>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder Ice Blue Digital
Australia Australia
I am the founder of Ice Blue Digital - a Sydney based software company in the natural language processing and machine learning space.

Comments and Discussions