HTML5 supports Web Storage API that allows browsers to store key/value pairs, in a much more better way than using cookies. Web Storage API provides two mechanisms of storing data locally:


  • window.localStorage stores data with no expiration date.
  • window.sessionStorage> stores data for one session (data is lost when the browser tab is closed).


DotNetBrowser provides API that allows working with both Local and Session storages:


  • Browser.GetLocalWebStorage() returns Local web storage instance.
  • Browser.GetSessionWebStorage() returns Session web storage instance. Local and Session storages implement same IWebStorage interface that provides methods for working with storage.


The following sample demonstrates how to access Local web storage of the loaded web page, read and set key/value pairs:

C#

using DotNetBrowser;
using DotNetBrowser.WPF;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
namespace WebStorageSample
{
    /// <summary>
    /// The sample demonstrates how to access WebStorage on
    /// the loaded web page using DotNetBrowser API.
    /// </summary>
    class Program
    {
       public class WindowMain : Window
       {
           private WPFBrowserView browserView;
           public WindowMain()
           {
               BrowserContext browserContext = BrowserContext.DefaultContext;
               Browser browser = BrowserFactory.Create(browserContext);
               browser.DocumentLoadedInMainFrameEvent += delegate
               {
                   IWebStorage webStorage = browser.GetLocalWebStorage();
                   // Read and display the 'myKey' storage value.
                   Console.Out.WriteLine("The myKey value: " + webStorage["myKey"]);
                   // Modify the 'myKey' storage value.
                   webStorage.Set("myKey", "Hello from Local Storage");
               };
               browserView = new WPFBrowserView(browser);
               Content = browserView;
               Width = 1024;
               Height = 768;
               this.Loaded += WindowMain_Loaded;
           }
           void WindowMain_Loaded(object sender, RoutedEventArgs e)
           {
               browserView.Browser.LoadHTML(new LoadHTMLParams(
                      "<html><body><button onclick=\"myFunction()\">Modify 'myKey' value</button>" +
                              "<script>localStorage.myKey = \"Initial Value\";" +
                              "function myFunction(){alert(localStorage.myKey);}" +
                              "</script></body></html>",
                      "UTF-8",
                      "http://teamdev.com"));
           }
           [STAThread]
           public static void Main()
           {
               Application app = new Application();
               WindowMain wnd = new WindowMain();
               app.Run(wnd);
               var browser = wnd.browserView.Browser;
               wnd.browserView.Dispose();
               browser.Dispose();
           }
       }
    }
}

VB.NET

Imports DotNetBrowser
Imports DotNetBrowser.WPF
Imports System.Windows

Module WebStorageSample
    Public Class WindowMain
        Inherits Window

        Private browserView As WPFBrowserView

        Public Sub New()
            Dim browserContext As BrowserContext = BrowserContext.DefaultContext
            Dim browser As Browser = BrowserFactory.Create(browserContext)
            AddHandler browser.DocumentLoadedInMainFrameEvent, sub()
                Dim webStorage As IWebStorage = browser.GetLocalWebStorage()
                Console.Out.WriteLine("The myKey value: " & webStorage("myKey"))
                webStorage.Set("myKey", "Hello from Local Storage")
            End sub

            browserView = New WPFBrowserView(browser)
            Content = browserView
            Width = 1024
            Height = 768
            AddHandler Me.Loaded, AddressOf WindowMain_Loaded
        End Sub

        Private Sub WindowMain_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
            browserView.Browser.LoadHTML(
                New LoadHTMLParams(
                    "<html><body><button onclick=""myFunction()"">Modify 'myKey' value</button>" &
                    "<script>localStorage.myKey = ""Initial Value"";" &
                    "function myFunction(){alert(localStorage.myKey);}" & "</script></body></html>", "UTF-8",
                    "http://teamdev.com"))
        End Sub

        <STAThread>
        Public Shared Sub Main()
            Dim app As Application = New Application()
            Dim wnd As WindowMain = New WindowMain()
            app.Run(wnd)
            Dim browser = wnd.browserView.Browser
            wnd.browserView.Dispose()
            browser.Dispose()
        End Sub
    End Class
End Module