jQuery Tips and Tricks

Mosharrf Hossain
Mh Mohon
Published in
1 min readDec 16, 2019

Some useful jQuery Tips & tricks are here.

How to get URL prams in jQuery:

//URLSearchParams() JQuery built in function.
// e.g. http://127.0.0.1:8000/account?option=user_registration
//window.location.search will get search keyword. (?option=user_registration)
let searchParams = new URLSearchParams(window.location.search);

if(searchParams.has('option'))
{
let param = searchParams.get('option');
if(param == 'user_registration')
{
if($("#edit").length != 0)
{
$('#edit').click();
}
}
}

Other

http://www.refulz.com:8082/index.php#tab2?foo=789

Property Result
------------------------------------------
host www.refulz.com:8082
hostname www.refulz.com
port 8082
protocol http:
pathname index.php
href http://www.refulz.com:8082/index.php#tab2
hash #tab2
search ?foo=789

var x = $(location).attr('<property>');
function getAbsolutePath(type){var loc = window.location;var pathName = loc.pathname.substring(0, loc.pathname.lastIndexOf('/') + 1);if(type == 'pathname'){return pathName;}else{return loc.pathname.substring(0, loc.href.length - ((loc.pathname + loc.search + loc.hash).length - pathName.length));}}

Reference here.

Tips-2: Always bind any JavaScript event on document.

Or it will give this type errors on 2nd page of data-table or any other j query table.

you have to change in the code:

$(document).on('click', '.delete-mail-template', function(e){    e.preventDefault();)};

--

--