Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm working in a custom Session class for the ASP.Net framework (both WebForms and MVC flavors).</p> <p>Although very old, this question is very pertinent and I could not find any other addressing the problem given.</p> <p>Hence, I share the solution I coded to ensure a unique and persistent GUID for each window and/or tab, kept static and secured no matter how hard one refreshes, navigates outside the site and returns, cleans the cache, etc.</p> <p>The magic involves <code>window.name</code> and is implemented by the JavaScript code below. The bootstrap is based on jQuery, but easily portable to jQuery-less solutions.</p> <p>Notice that this GUID is automatically appended to any existing <code>form</code>'s providing server-side references.</p> <hr> <p><strong>UIGUID.js</strong></p> <hr> <p><strong>Edited:</strong> the original version presented an error at <code>windowLoadSetGUIDOnForms</code> for <code>formList.length == 1</code> </p> <pre><code>//------------------------------------------------------------------------------ //-- guarantees that window.name is a GUID, and that it would //-- be preserved whilst window life cicle //-- //-- for frames and iframes, the outermost window determines the GUID //-- //-- for every form it will be appended a hidden element of id //-- "this.window.GUID" for server-side references //------------------------------------------------------------------------------ //-- window.name will be set to "GUID-&lt;A_GUID&gt;" //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ //-- Retrieves window GUID, initializing it if necessary ----------------------- //------------------------------------------------------------------------------ function getWindowGUID() { //---------------------------------- var windowGUID = function () { //---------- var S4 = function () { return ( Math.floor( Math.random() * 0x10000 /* 65536 */ ).toString(16) ); }; //---------- return ( S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4() ); }; //---------------------------------- //-- traverses up in the hierarchy for the outermost window ---------------- var topMostWindow = window; while (topMostWindow != topMostWindow.parent) { topMostWindow = topMostWindow.parent; } //-- initialize GUID if needed --------------------------------------------- if (!topMostWindow.name.match(/^GUID-/)) { topMostWindow.name = "GUID-" + windowGUID(); } //-- return GUID ----------------------------------------------------------- return topMostWindow.name; } //-- getWindowGUID ----------------------------------------------------------- //------------------------------------------------------------------------------ //-- Append via jQuery handlers for windowLoad --------------------------------- //------------------------------------------------------------------------------ $(window).load( function () { windowLoadSetGUID(); windowLoadSetGUIDOnForms(); } ) //---------------------------------------------------------------------------- function windowLoadSetGUID() { var dummy = getWindowGUID(); } //-- windowLoadSetGUID ------------------------------------------------------- function windowLoadSetGUIDOnForms() { var formList = $('form'); var hidGUID = document.createElement("input"); hidGUID.setAttribute("type", "hidden"); hidGUID.setAttribute("name", "this.window.GUID"); hidGUID.setAttribute("value", getWindowGUID()); if (formList.length == 1) { formList.append(hidGUID); } else { for (var i = 0; i &lt; formList.length; ++i) { formList[i].append(hidGUID); } } } //-- windowLoadSetGUIDOnForms ------------------------------------------------ </code></pre> <p>As POC, I devised two HTML scripts demonstrating the uniqueness even at child elements in frames or iFrames</p> <hr> <p><strong>GUIDTest.html</strong></p> <hr> <pre><code>&lt;html&gt; &lt;head&gt; &lt;script language="javascript" type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"&gt;&lt;/script&gt; &lt;script language="javascript" type="text/javascript" src="UIGUID.js"&gt;&lt;/script&gt; &lt;/head&gt; &lt;body onLoad="alert('Main document: ' + getWindowGUID());"&gt; &lt;iframe id="frame001" src="GUIDFrame.html"&gt;&lt;/iframe&gt; &lt;iframe id="frame002" src="GUIDFrame.html"&gt;&lt;/iframe&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <hr> <p><strong>GUIDFrame.html</strong></p> <hr> <pre><code>&lt;html&gt; &lt;head&gt; &lt;script language="javascript" type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"&gt;&lt;/script&gt; &lt;script language="javascript" type="text/javascript" src="UIGUID.js"&gt;&lt;/script&gt; &lt;/head&gt; &lt;body onLoad="alert('iFrame: ' + getWindowGUID());" /&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload