Posts

Showing posts with the label JS

Download file from input type file javascript

        <script>             function downloadFile() {                 if (!window.File || !window.FileReader || !window.FileList || !window.Blob) {                     alert('The File APIs are not fully supported in this browser.');                     return;                 }                 input = document.getElementById('fileinput');                 if (!input) {                     alert("Um, couldn't find the fileinput element.");                 }                 else if (!input.files) {           ...

POST an array of objects with $.ajax to C# WebMethod

//Create JS method invoking your C# WebMethod (example: getNames)     function getFstNames() {         var myObj = [             { 'fstName': 'name 1', 'lastName':'last name 1', 'age': 32 }           , { 'fstName': 'name 2', 'lastName':'last name 1', 'age': 33 }         ];         var postData = JSON.stringify({ lst: myObj });         console.log(postData);         $.ajax({             type: "POST",             url: "http://yourUrl.aspx/getNames",             data: postData,             contentType: "application/json; charset=utf-8",             dataType: "json",             success: function (response) {   ...

Work with array of objects in browser local storage

Add methods to work with objects in local storage     $(document).ready(function () {         Storage.prototype.setObj = function (key, obj) {             return this.setItem(key, JSON.stringify(obj))         }         Storage.prototype.getObj = function (key) {             return JSON.parse(this.getItem(key))         }         var myObj = [];         localStorage.setObj('myObj', myObj);     }); Save item in local storage     function SaveItem() {         var myObj = localStorage.getObj('myObj');         var objNome = $('#txtFstName').val();         var objApelido = $('#txtLastName').val();                 var obj = { 'fstName': objNome, 'lastName': objApelid...

JS - function to get URL parameter

var getUrlParameter = function getUrlParameter(sParam) { var sPageURL = decodeURIComponent(window.location.search.substring(1)), sURLVariables = sPageURL.split('&'), sParameterName, i; for (i = 0; i < sURLVariables.length; i++) { sParameterName = sURLVariables[i].split('='); if (sParameterName[0] === sParam) { return sParameterName[1] === undefined ? true : sParameterName[1]; } } };

JS - read data from table to array

var dataArray = new Array; var i = 0; var strAux = {}; $("table[summary='InfoSuporte']").find("td").each(function() { if(($(this).children('div').length) == 0){ i++; //console.log($(this).html()); if(i==1) { strAux["columnA"] = $(this).html(); } if(i==2) { strAux["columnB"] = $(this).html(); } if(i==3) { strAux["columnC"] = $(this).html(); } if(i==4) { strAux["columnD"] = $(this).html(); } if(i==5) { strAux["columnE"] = $(this).html(); } if(i%5 == 0){ dataArray.push(strAux); strAux = {}; i=0; } } });

Sharepoint - Utils in JS

Functions to help to configurations in JS Get user login info    function getWebUserData() {         var context = new SP.ClientContext.get_current();         var web = context.get_web();         var currentUser = web.get_currentUser(); var loginName = currentUser.get_loginName(); console.log(loginName);     }     ExecuteOrDelayUntilScriptLoaded(getWebUserData(), "sp.js"); Get if user has permissions to read SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function () { // function to get if user as permission to edit list function getCurrentUserPermission() {  var web,clientContext,currentUser,oList,perMask; clientContext = new SP.ClientContext.get_current(); web = clientContext.get_web(); currentUser = web.get_currentUser();  oList = web.get_lists().getByTitle('InfoSuporte'); clientContext.load(oList,'EffectiveBasePermissions');...

C#/JS/JQuery - download a file with a loading panel

Easy way to use export a document in C # and maintain the loading functionality. First add the functions of add / get cookies https://utilitiesforprogrammers.blogspot.pt/2018/01/jsjquery-cookies.html Second add the loading panel https://utilitiesforprogrammers.blogspot.pt/2018/01/jsjquery-loading.html Now in your event of export add the cookie to track that the process is ended          protected void btnExport(object sender, EventArgs e)         {             try             {                 byte[] bytes = Export();                 this.Page.Response.AppendCookie(new HttpCookie("fileDownloadToken", "1"));                               this.Page.Response.Clear();               ...

JS/JQuery - Loading

Adicionar um loading à webapplication, a funcionar de forma autónoma. Para isto, bastará clocar o seguinte código na masterpage. <style>     .loading {     position:fixed;     width:100%;     left:0;right:0;top:0;bottom:0;     background-color: rgba(255,255,255,0.7);     z-index:9999;     }         .loading::after {         content:'';         position:absolute;         left:48%;top:40%;         width:70px;         height:70px;         border: 10px dotted #a33038;         border-top-color:transparent;         border-radius:50%;         -webkit-animation: spin .8s linear infinite;         animation: spin .8s linear infinite;         } </style> ...

JS/JQuery - Cookies

Work with cookies the easy way. function setCookieWithTime(key, value, expires) { document .cookie = key + '=' + value + ';expires=' + expires.toGMTString(); } function setCookie(key, value) { var expires = new Date(); expires.setTime(expires.getTime() + (5 * 1000)); document .cookie = key + '=' + value + ';expires=' + expires.toGMTString(); } function getCookie(key) { var keyValue = document .cookie.match('(^|;) ?' + key + '=([^;]*)(;|$)'); return keyValue ? keyValue[2] : null; }

JS/JQuery - Events

List of useful events for JS and JQuery. Listed in the order of execution in the browser. //$(window).bind('beforeunload', function () { });  // in jquery var hasChanges = true; window.onbeforeunload = function () {     if (hasChanges) return "" ;                                     // only asks if hasChanges == true } document.onreadystatechange = function () {     console.log( "2 - document.readyState=" + document.readyState); } $(function () {     console.log( "3 - null" ); }); $(document).ready(function () {     console.log( "4 - ready" ); }); document.onreadystatechange = function () {     console.log( "5 - document.readyState=" + document.readyState); } //$(window).bind("load", function () { });         // other way //$(window).on("load", function () { });      ...