Window SessionStorage Client Versus Server Ajax Tutorial
โœ‚๐Ÿƒ๐Ÿพโ€โ™€๏ธ๐Ÿƒ๐Ÿผโ€โ™‚๏ธ
๐Ÿ“–

Window SessionStorage Client Versus Server Ajax Tutorial

Window SessionStorage Client Versus Server Ajax Tutorial

We have a few โ€œclientside chestnutsโ€ to use with our current Capital City Find Matching Country Report web application project today, those being โ€ฆ

  • Ajax functionality, kicked off by an โ€œonclickโ€ event set of logic, allowing mobile platforms to also have a look in (the look in that they miss when the event logic is off the โ€œonmouseoverโ€ event)
  • iframe and its โ€ฆ
    1. srcdoc attribute (โ€œcontentโ€ alternative to src โ€œurlโ€ attribute) โ€ฆ along with, and crucially needing (because srcdoc ignores its own document.body onload goings on, that we need the โ€œIframe Client Pre-Emptiveโ€ methods below to circumvent) the โ€ฆ
    2. onload event opportunity of an iframe element (we group into โ€œIframe Client Pre-Emptiveโ€ methods, here)

โ€ฆ adding onto yesterdayโ€™s Window SessionStorage Client Versus Server Canvas Tutorial.

Itโ€™s not that involved with the Ajax work today, given that there are no cross-domain issues, though there are cross-protocol (SSL https: versus non-SSL http:) issues to be careful about. Those can be addressed because the web application is recalled to present its โ€œCountry Reportโ€ and that is the opportunity to check on protocol navigation requirements.

Along the way, we also make this happen for the user on โ€ฆ

  • click/touching a table row โ€ฆ it sets off new โ€œtrโ€ (table row) element logic calling our (inhouse) Timezone and Wikipedia Place Information helper (HTML) via Ajax (so not leaving the webpage) โ€ฆ and because of place name oddities we allow for โ€ฆ
  • โ€œtdโ€ (table cell) element user amendments by setting their contenteditable attributes to โ€œtrueโ€ (since fixed, but we found the Timezone Europe/Tirane pointing at Tirane in Albania used to be spelt โ€œTiranaโ€)

โ€ฆ that latter methodology normally a technique we apply to โ€œdivโ€ elements (so, there you are!)

Also used are โ€œoverlayโ€ techniques, two of the โ€œusual suspectsโ€ here coming into play, to present to the โ€œAjax content to srcdoc iframe arrangementsโ€ โ€ฆ

Yet again, see thechanged wls_vs_phpโšซhtmโ€˜s Capitalโœ‚City Find Matching Country Report live run linkโ€™s new โ€œAjaxโ€ functionality.



Previous relevant Window SessionStorage Client Versus Server Canvas Tutorial is shown below.

Window SessionStorage Client Versus Server Canvas Tutorial

Window SessionStorage Client Versus Server Canvas Tutorial

Yesterdayโ€™s Window SessionStorage Client Versus Server Share Tutorial dealt with ascii text clipboard copy assisted sharing options with our current Capital City Find Matching Country Report web application project. This suited both Email and SMS share options we coded for, but todayโ€™s extension of functionality from โ€œascii textโ€ data to โ€œgraphical dataโ€ only suits Email sharing. The other caveat with our work is that no serverside (for us, PHP) help is allowed, so no PHP mail here.

What comes into play with a โ€œgraphical dataโ€ clientside (only) sharing approach? It will not surprise many readers that, for us, it involves โ€ฆ

  • canvas element โ€ฆ converting HTML table outerHTML โ€œascii textโ€ data โ€ฆ via โ€ฆ
  • canvas drawing methods โ€œ[canvasContext].strokeRect()โ€ and โ€œ[canvasContext].strokeText()โ€ via โ€œ[cellElement].getBoundingClientRect()โ€ โ€ฆ to convert that canvas element content via โ€ฆ
  • [canvasElement].toDataURL() โ€ฆ to an โ€ฆ
  • img element nested in a div contenteditable=true element โ€ฆ so as to hook in with todayโ€™s very useful helper link, thanks โ€ฆ use โ€ฆ


  • function tabletoclipboard(canvas) { // thanks to https://stackoverflow.com/questions/27863617/is-it-possible-to-copy-a-canvas-image-to-the-clipboard

    var img = document.createElement('img');

    img.src = canvas.toDataURL();



    var div = document.createElement('div');

    div.contentEditable = true;

    div.appendChild(img);

    document.body.appendChild(div);



    // do copy

    SelectText(div);

    document.execCommand('Copy');

    document.body.removeChild(div);

    }



    function SelectText(element) { // thanks to https://stackoverflow.com/questions/27863617/is-it-possible-to-copy-a-canvas-image-to-the-clipboard

    var doc = document;

    if (doc.body.createTextRange) {

    var range = document.body.createTextRange();

    range.moveToElementText(element);

    range.select();

    } else if (window.getSelection) {

    var selection = window.getSelection();

    var range = document.createRange();

    range.selectNodeContents(element);

    selection.removeAllRanges();

    selection.addRange(range);

    }

    }

  • to leave the userโ€™s deviceโ€™s clipboard containing a useful table (with linework) โ€ฆ ready to โ€ฆ
  • paste into an email body section

โ€ฆ sharing off to an emailee collaborator.

Again, see thechanged wls_vs_phpโšซhtmโ€˜s Capitalโœ‚City Find Matching Country Report live run linkโ€™s new โ€œEmail Tableโ€ button functionality.


Previous relevant Window SessionStorage Client Versus Server Share Tutorial is shown below.

Window SessionStorage Client Versus Server Share Tutorial

Window SessionStorage Client Versus Server Share Tutorial

Yesterdayโ€™s Window SessionStorage Client Versus Server Tutorial has been amended today for two new sharing and collaboration options, those being โ€ฆ

  • email
  • SMS

โ€ฆ but you may well be familiar with the restrictions on email and SMS client (program) approaches to this, coming from HTML โ€œaโ€ link โ€œmailto:โ€ and โ€œsms:โ€ href property prefixes respectively. Weโ€™re going to need help with the 800 odd character (length) restrictions with the (resultant) web address (bar) URL, but what? How about working off the great advice of this wonderful link, thanks, to copy what weโ€™d have assembled into an ascii text Report into the characters contained by the userโ€™s deviceโ€™s clipboard?



function copytoclipboard(str) { // thanks to https://hackernoon.com/copying-text-to-clipboard-with-javascript-df4d4988697f

var el = document.createElement('textarea');

el.value = str;

el.setAttribute('readonly', '');

el.style.position = 'absolute';

el.style.left = '-9999px';

document.body.appendChild(el);

el.select();

document.execCommand('copy');

document.body.removeChild(el);

}

An issue that springs up here using such clipboard ascii text content, whenever you get the Font choice given to you, pick a monospaced Font like Courier New or โ€œFixed Widthโ€.

See thechanged wls_vs_phpโšซhtmโ€˜s Capitalโœ‚City Find Matching Country Report live run linkโ€™s new sharing functionality.


Previous relevant Window SessionStorage Client Versus Server Tutorial is shown below.

Window SessionStorage Client Versus Server Tutorial

Window SessionStorage Client Versus Server Tutorial

Sometimes itโ€™s the case at this blog that weโ€™d like to introduce a new topic, but do not do so, because we cannot show any real world (or real application) use of that concept. So it has been, up until now, with the concept of (web browser) window (object) sessionStorage property. But yesterdayโ€™s Window LocalStorage Client Versus Server Primer Tutorial represented an opportunity akin to when Haleyโ€™s Comet gets at its closest to the Earth โ€ฆ while you see a chance, take it โ€ฆ chance because of that nuance whereby we were not trying to store data for any other purpose than passing data onto โ€ฆ

  1. a known entity โ€ฆ ie. same web application โ€ฆ at โ€ฆ
  2. a known time โ€ฆ ie. immediately

โ€ฆ two conditions that make the code design โ€œmarginallyโ€ more ideal for the window object property concept of sessionStorage rather than localStorage, in that any โ€ฆ



localStorage.removeItem([knownLocalStorageName]);

โ€ฆ becomes superfluous as with sessionStorage data will disappear between web browser sessions, anyway.

We offer this new concept as a non-default option of a select (dropdown) element replacement to the h1 element hardcoding โ€œlocalStorageโ€ with thechanged wls_vs_phpโšซhtm Capitalโœ‚City Find Matching Country Report live run. The other nuance of difference with sessionStorage usage is that in the document.body onload event logic, we may as well (as part of other changes) pre-emptively look for, and if there, respond to, any found sessionStorage data points, even without the user having flagged it specifically โ€ฆ



var datamode='localStorage';



function checkforreport() {

var divcont='';

var dcaps, dctys, idis;

if (getcapitals == 'localStorage') {

if (window.localStorage) {

getcapitals=decodeURIComponent(localStorage.getItem('wls_vs_php_capitals')).replace(/\+/g,' ');

localStorage.removeItem('wls_vs_php_capitals');

} else {

getcapitals='';

}

} else if (getcapitals == 'sessionStorage') {

document.getElementById('smode').value=getcapitals;

datamode=getcapitals;

if (window.sessionStorage) {

getcapitals=decodeURIComponent(sessionStorage.getItem('wls_vs_php_capitals')).replace(/\+/g,' ');

} else {

getcapitals='';

}

}
else if (getcapitals == '' && window.sessionStorage) {

getcapitals=decodeURIComponent(('' + sessionStorage.getItem('wls_vs_php_capitals')).replace(/^null$/g,'')).replace(/\+/g,' ');

if (getcapitals != '') {

document.getElementById('smode').value='sessionStorage';

datamode='sessionStorage';

}

}


if (getcountries == 'localStorage') {

if (window.localStorage) {

getcountries=decodeURIComponent(localStorage.getItem('wls_vs_php_countries')).replace(/\+/g,' ');

if (getcapitals.replace('localStorage','') != '' && getcountries.replace('localStorage','') != '') { document.getElementById('myh1').innerHTML+=' <font size=1>... yes, it was needed</font>'; }

localStorage.removeItem('wls_vs_php_countries');

} else {

getcountries='';

}

} else if (getcountries == 'sessionStorage') {

if (window.sessionStorage) {

getcountries=decodeURIComponent(sessionStorage.getItem('wls_vs_php_countries')).replace(/\+/g,' ');

if (getcapitals.replace('sessionStorage','') != '' && getcountries.replace('sessionStorage','') != '') { document.getElementById('myh1').innerHTML+=' <font size=1>... yes, it was needed</font>'; }

} else {

getcountries='';

}

}
else if (getcountries == '' && document.getElementById('smode').value == 'sessionStorage' && window.sessionStorage) {

getcountries=decodeURIComponent(('' + sessionStorage.getItem('wls_vs_php_countries')).replace(/^null$/g,'')).replace(/\+/g,' ');

if (getcountries != '') {

document.getElementById('smode').value='sessionStorage';

datamode='sessionStorage';

}

}


if (getcapitals != '' && getcountries != '') {

divcont='<table border=5 style="width:95%;vertical-align:top;background-color:white;"><tr style=background-color:#f0f0f0;"><th>Capital</th><th>Country</th></tr></table>';

dcaps=getcapitals.split('|');

dctys=getcountries.split('|');

for (idis=0; idis<dcaps.length; idis++) {

divcont=divcont.replace('</table>', '<tr><td>' + dcaps[idis] + '</td><td>' + dctys[idis] + '</td></tr></table>');

}

document.getElementById('dreport').innerHTML=divcont;

}

document.getElementById('smode').value=datamode;

}

Which beggars the question โ€œWhat are the differences between sessionStorage and localStorage?โ€ A quick reading might surmise that โ€œthe latter has an expiration dateโ€. We leave you with an open ended Google search so that you may extend your readings on this.


Previous relevant Window LocalStorage Client Versus Server Primer Tutorial is shown below.

Window LocalStorage Client Versus Server Primer Tutorial

Window LocalStorage Client Versus Server Primer Tutorial

Even though we rave on a lot about serverside PHP and its $_POST method=POST (versus HTML/Javascript recipient via ? and & argument $_GET method=GET scenario) data length advantages as the recipient of an HTML form method=POST set of data that could be sizeable, weโ€™ve just realized that there is a client Javascript and window.localStorage methodology that may help alleviate the need to involve PHP (and any other serverside intervention) on occasions.

Hint: Yes, weโ€™ve raved on about this too?! Does the blog posting title give it away? Okay, yes, it should read โ€œlocalStorageโ€, but thought weโ€™d gone past such juvenile finickiness since the Whac-A-Mole controversy of 1st December 2019 (or even The Great Tea Trolley Disaster of โ€™67, we daresay).

It can even use a โ€œself-destructโ€ approach to the use of this โ€œlocalStorageโ€ on having used it because โ€ฆ

  • the web application knows who is using it (localStorage) โ€ฆ and on having accessed and read it โ€ฆ
  • the web application knows it (localStorage) is of no use to any other user (in this web applicationโ€™s case, at least)

โ€ฆ which is very pleasing for a Land Surveyor who likes to leave cow paddocks as theyโ€™ve seen them so to speak. Except itโ€™s like having a ten tonne truck worth of data access in amongst the cow pats when having access to โ€œlocalStorageโ€ (or PHP), rather than a little piddle of calf wee (wee Metcalfes know a thing or two about these things!) data access of ? and & HTML/Javascript URL arguments (or even if we were to use HTTP Cookies).

Itโ€™s not as if we all have access to serverside language usage, though we do, because we really like PHP and MAMP and Apache/PHP/MySql web servers (and have arranged our development environment accordingly), but what if you are starting out in web development, and still want to allow for sizeable chunks of data with your web applications? Huh? Huh?! See the possibilities? Try our proof of concept Capitalโœ‚City Find Matching Country Report live run, and highlight a whole swathe of (multiple mode) dropdown option Capital Cities holding down the shift key before pressing the yellow โ€œReportโ€ button. If the URL ends up as โ€ฆ

https://rjmprogramming.com.au/HTMLCSS/wls_vs_php.html?capitals=localStorage&countries=localStorage

โ€ฆ thatโ€™s because the web applicationโ€™s โ€ฆ



function analyze() {

var purl=document.URL.split('#')[0].split('?')[0] + '?capitals=' + encodeURIComponent(document.getElementById('capitals').value) + '&countries=' + encodeURIComponent(document.getElementById('countries').value);

if (purl.length > 800) {

if (phpexists) {

document.getElementById('myform').method='POST';

document.getElementById('myform').action='./wls_vs_php.php';

} else if (window.localStorage) {

localStorage.setItem('wls_vs_php_countries', encodeURIComponent(document.getElementById('countries').value));

localStorage.setItem('wls_vs_php_capitals', encodeURIComponent(document.getElementById('capitals').value));

document.getElementById('capitals').value='localStorage';

document.getElementById('countries').value='localStorage';

location.href=document.URL.split('#')[0].split('?')[0] + '?capitals=' + encodeURIComponent(document.getElementById('capitals').value) + '&countries=' + encodeURIComponent(document.getElementById('countries').value);


return false;

}

}

return true;

}

โ€ฆ HTML form onsubmit event logic โ€ฆ

  1. discovered no PHP web application existant (via Client Pre-emptive Iframe techniques) โ€ฆ and โ€ฆ
  2. discovered (in a sanity check feeling way) that to go down the proposed HTML form method=GET approach was risking a โ€ฆ


    HTTP 414 "Request URI too long"


    โ€ฆ web browser error โ€ฆ and that โ€ฆ
  3. localStorage was a known web browser piece of functionality
  4. โ€ฆ and so as per our localStorage logic we โ€ฆ

  5. back out of the default HTML form method=GET navigation setup of the web application in favour of โ€ฆ
    • storing that data into localStorage
    • substituting into the URL ? and & arguments the hardcoding โ€œlocalStorageโ€ (and in so doing, getting back under the HTTP 414 โ€œRequest URI too longโ€ limitation, piecing together (what amounts to) โ€ฆ
      location.href=document.URL.split(โ€˜#โ€™)[0].split(โ€˜?โ€™)[0] + โ€˜?capitals=localStorage&countries=localStorageโ€™;)
      โ€ฆ that on a recall to this same web application a โ€ฆ
    • document.body onload event piece of Javascript logic checks the localStorage for its incoming Capital City Country Report data, as per โ€ฆ


      var phpexists=false;

      var getcapitals=location.search.split('capitals=')[1] ? decodeURIComponent(location.search.split('capitals=')[1].split('&')[0]).replace(/\+/g,' ') : '';

      var getcountries=location.search.split('countries=')[1] ? decodeURIComponent(location.search.split('countries=')[1].split('&')[0]).replace(/\+/g,' ') : '';



      function checkforreport() {

      var divcont='';

      var dcaps, dctys, idis;

      if (getcapitals == 'localStorage') {

      if (window.localStorage) {

      getcapitals=decodeURIComponent(localStorage.getItem('wls_vs_php_capitals')).replace(/\+/g,' ');

      localStorage.removeItem('wls_vs_php_capitals');

      } else {

      getcapitals='';

      }

      }

      if (getcountries == 'localStorage') {

      if (window.localStorage) {

      getcountries=decodeURIComponent(localStorage.getItem('wls_vs_php_countries')).replace(/\+/g,' ');

      if (getcapitals.replace('localStorage','') != '' && getcountries.replace('localStorage','') != '') { document.getElementById('myh1').innerHTML+=' <font size=1>... yes, it was needed</font>'; }

      localStorage.removeItem('wls_vs_php_countries');

      } else {

      getcountries='';

      }

      }


      if (getcapitals != '' && getcountries != '') {

      divcont='<table border=5 style="width:95%;vertical-align:top;background-color:white;"><tr style=background-color:#f0f0f0;"><th>Capital</th><th>Country</th></tr></table>';

      dcaps=getcapitals.split('|');

      dctys=getcountries.split('|');

      for (idis=0; idis<dcaps.length; idis++) {

      divcont=divcont.replace('</table>', '<tr><td>' + dcaps[idis] + '</td><td>' + dctys[idis] + '</td></tr></table>');

      }

      document.getElementById('dreport').innerHTML=divcont;

      }

      }


      โ€ฆ the localStorage.removeItem() representing that โ€œself-destructโ€ nuance we were talking about before

We may well use this methodology in future projects, and hope it has been of some little interest to you as well?!

If this was interesting you may be interested in this too.


If this was interesting you may be interested in this too.


If this was interesting you may be interested in this too.


If this was interesting you may be interested in this too.


If this was interesting you may be interested in this too.

This entry was posted in Ajax, eLearning, Event-Driven Programming, Tutorials and tagged , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *