Web development blog for Html, CSS, javascript, JQuery developers. At Kodecrash you will find best solution for complex problems.

Get URL parameters using JavaScript

Posted in Javascript, javascript interview questions

Sometimes you might need to get URL parameters also called as query string parameters using JavaScript. URL parameters can be one or multiple.

Example:

http://localhost/mypage?id=10

Multiple URL parameters:

http://localhost/mypage?id=10&name=Simy

Use script below to get URL parameters:


function getURLParameter(variable)
{
       var query = window.location.search.substring(1);
       var vars = query.split("&");
       for (var i=0;i<vars.length;i++) {
               var pair = vars[i].split("=");
               if(pair[0] == variable){return pair[1];}
       }
       return(false);
}

Above function returns specified URL parameter based on the argument supplied to that function.

getURLParameter(“id”) –> Returns 10

getURLParameter(“name”) –> Returns Simy

Web development blog for Html, CSS, javascript, JQuery developers. At Kodecrash you will find best solution for complex problems.

Custom select dropdown using CSS

Posted in CSS

We can create customize default HTML select dropdown using CSS. We can either customize this using JS. Refer Create Custom Dropdown using jQuery article to create custom dropdown using JavaScript.

Use CSS below:

<style>
	
  select.custom-dropdown {
    -webkit-appearance: none;
    -moz-appearance: none;
    /* border: 0 !important; */
    /* background-color: #0d98e8; */
    color: #333;
    border-top-left-radius: 5px;
    border-bottom-left-radius: 5px;
    font-size: 14px;
    padding: 10px;
    width: 245px;
    cursor: pointer;
    background: url(drop-down-arrow.png) no-repeat right center;
    background-size: 40px 37px;
    border: 1px solid #CCC;
    background-position: 206px 0;

 }
  select::-ms-expand {
    display: none;
 }
</style>

Use above CSS to customize select dropdown. This CSS is compatible with all browsers including IE 10+, FF, Chrome and MAC Safari.

Download Code

Demo:

Web development blog for Html, CSS, javascript, JQuery developers. At Kodecrash you will find best solution for complex problems.

Search object in an array

Posted in Javascript, JQuery

You can find any element in an array based on index of that element. But what if you want to find an object in an array of objects based on any of the property. You can use filter function of JQuery.

JQuery filter (selection) function returns an element or array of elements that matches specified criteria. If provided selection parameter matches with any of the element in an array it returns that particular element or array of elements.

Example:

  $(selector).filter (selection, function(index){
       // Process elements here
  })
    Selector is a set of DOM elements or array of objects.
    Selection is a filter parameter which is used to match with the elements in array.
    function(index) is a callback function which used to process the elements in filtered array.

Example:

Consider an example of employee’s data. Find list of employee’s based on their age.

    var employees=[
        {name:"George", age:32, retiredate:"March 12, 2014"},
        {name:"Edward", age:17, retiredate:"June 2, 2023"},
        {name:"Christine", age:58, retiredate:"December 20, 2036"},
        {name:"Sarah", age:62, retiredate:"April 30, 2020"}
   ];

   var age=32;
   var employeesFound = employees.filter(function(obj) {
    return obj.age== age;
   });

In above case, employeesFound array contains an employee object with an age of 32. You can find employees with age greater than 32.

    var employeesFound = employees.filter(function( obj) {
     return obj.age> age;
    });

Web development blog for Html, CSS, javascript, JQuery developers. At Kodecrash you will find best solution for complex problems.

HTML5 sessionStorage Safari “QUOTA_EXCEEDED_ERR”

Posted in Html, Javascript

If you have opened a web page in private mode in ios-safari browser and if you are using sessionStorage/localStorage in your code sometimes you will get “QUOTA_EXCEEDED_ERR:”.

Error is highlighted below:

“QUOTA_EXCEEDED_ERR: DOM Exception 22: An attempt was made to add something to storage that exceeded the quota.”

This can be overcome by simply putting sessionStorage code in try catch block.

              if (typeof sessionStorage === 'object') {
		    try {
		        sessionStorage.setItem('sessionStorage', 1);
		        sessionStorage.removeItem('sessionStorage');
		        sessionStorage.setItem("questions", JSON.stringify(data));
		    } catch (e) {
		        Storage.prototype._setItem = Storage.prototype.setItem;
		        Storage.prototype.setItem = function() {};
		    }
		} 

The same code is applicable for localStorage also.

              if (typeof localStorage === 'object') {
		    try {
		        localStorage.setItem('localStorage', 1);
		        localStorage.removeItem('localStorage');
		        localStorage.setItem("questions", JSON.stringify(data));
		    } catch (e) {
		        Storage.prototype._setItem = Storage.prototype.setItem;
		        Storage.prototype.setItem = function() {};
		    }
		} 

Web development blog for Html, CSS, javascript, JQuery developers. At Kodecrash you will find best solution for complex problems.

Debug your website in I-Pad/I-Phone Safari

Posted in Javascript

For responsive web design it is very difficult to adjust and fix anything if you face any issue in I-PAD/I-phone safari browser. You can debug and inspect a website in I-Pad and I-phone. Follow below steps to inspect DOM element in I-Pad and I-Phone safari browser.

Step 1: Enable web inspector option in your device.

web-inspector-ipad

Step 2: Enable Developer Menu option in MAC.
To use the web inspector, connect to Safari on your computer using a USB cable and access your I-Pad from developer menu. If developer menu is not visible, open preferences in your MAC and click Advanced tab and check “show developer menu in menu bar” option.

safari-preferences

developer-option

Now, you can see Develop option in MAC Safari. If everything is correct, you can see your I-Pad/I-Phone in develop toolbar.

safari-developer-menu

Search your IPAD/IPhone name in developer toolbar. In above screenshot, you I-PAD/I-Phone name is “Office iPad”.

Step 3: Inspect DOM element from Device.

Now, you can to inspect elements/ run JS code in developer console.

safari-inspect-element