Showing posts with label Jquery. Show all posts
Showing posts with label Jquery. Show all posts

Wednesday, June 10, 2020

How to Create a back to top button for website




Wednesday, April 15, 2020

Click event count on page using jquery


$(document).click(function(e)
  {
    if(e.currentTarget.activeElement.attributes[0])
    {
      var idClicked = e.currentTarget.activeElement.attributes[0].value;
    }
    if (e.currentTarget.activeElement.attributes[1])
    {
      var idClicked1 = e.currentTarget.activeElement.attributes[1].value ;
    }
    if (idClicked == 'loginbutton' || idClicked1 == 'loginModal' || idClicked1 == 'close')
    {
    }
    else
    {
        var auth_token = $.session.get("auth_token");
        var user_id = $.session.get("user_id");
        var pathname = window.location.pathname;
        var patharray = pathname.split("/");
        var pageurl = patharray[patharray.length - 1];
        if (!(auth_token && user_id) && pageurl != "register.php")
        {
          var clickcount = $.session.get("clickcount");
          if (clickcount) {
            clickcount++;
          } else {
            clickcount = 1;
          }
          $.session.set("clickcount"clickcount);
          var newclickcount = $.session.get("clickcount");
          if (newclickcount > 10)
          {
            $('.modal').modal('hide');
            $("#loginModal").modal("show");
            $.session.set("clickcount"0);
          }
        }
    }


  });

Tuesday, April 14, 2020

Get URL parameters using jQuery

jQuery code snippet to get the dynamic variables stored in the url as parameters and store them as JavaScript variables ready for use with your scripts. Used differently to Hash Url’s as the world turns to dynamic web apps. Thus things like Decoding URL Strings will be ever-popular for years to come.

$.urlParam = function(name){
 var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
 return results[1] || 0;
}

// example.com?param1=name&param2=&id=6
$.urlParam('param1'); // name
$.urlParam('id');        // 6
$.urlParam('param2');   // null

//example params with spaces
http://www.jquery4u.com?city=Gold Coast
console.log($.urlParam('city'));  
//output: Gold%20Coast

console.log(decodeURIComponent($.urlParam('city')));  
//output: Gold Coast
This could be used for example to set the default value of a text input field:
$('#city').val(decodeURIComponent($.urlParam('city')));
Thanks to bjverde for improvements to this function:
$.urlParam = function(name){
    var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
    if (results==null){
       return null;
    }
    else{
       return results[1] || 0;
    }
}