Showing posts with label JQuery. Show all posts
Showing posts with label JQuery. Show all posts

Wednesday, March 19, 2014

Removing an empty div using Jquery

$('.selector:empty').remove();

Another Way

$.expr[':'].blank = function(obj){ return obj.innerHTML.trim().length === 0; }; $('.selector:empty').remove();

Friday, February 7, 2014

Capitalize First character in a Javascript String

var capitalized = capitalizeMe.substr(0, 1).toUpperCase() + capitalizeMe.substr(1);

var capitalized = capitalizeMe[0].toUpperCase() + capitalizeMe.substring(1);

var capitalized = capitalizeMe.charAt(0).toUpperCase() + capitalizeMe.substring(1);

var capitalized = capitalizeMe.slice(0,1).toUpperCase() + capitalizeMe.slice(1, capitalizeMe.length);

var capitalized = capitalizeMe.substring(0, 1).toUpperCase() + capitalizeMe.substring(1);

Thursday, February 6, 2014

JQuery URL parser function

// Parse URL Queries Method
(function($){
    $.getQuery = function( query ) {
        query = query.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
        var expr = "[\\?&]"+query+"=([^&#]*)";
        var regex = new RegExp( expr );
        var results = regex.exec( window.location.href );
        if( results !== null ) {
            return results[1];
            return decodeURIComponent(results[1].replace(/\+/g, " "));
        } else {
            return false;
        }
    };
})(jQuery);

// Document load
$(function(){
    var test_query = $.getQuery('test');
    alert(test_query); // For the URL /?test=yes, the value would be "yes"
});
Reference:

http://www.kevinleary.net/jquery-parse-url/

Change URL parameter value of a variable in a Javascript Regular Expression (RegEx)

function changeParamByName(href, paramName, newVal) {
var tmpRegex = new RegExp("(" + paramName + "=)[a-z]+", 'ig');
return href.replace(tmpRegex, '$1'+newVal);
}
 
var href = changeParamByName("http://domain.com?var=thisIsOld&bca=something&b=2&c=3","bca","theNewValue");
console.log(href);



Reference:

http://blog.adrianlawley.com/jquery-change-url-parameter-value/

Changing the URL of parent

In some cases to make the parent window to load a different URL ( from IFRAME and pop etc.,), use

window.top.location.href = "http://www.site.com"; 
To just refresh the parent window use

parent.location.reload();

To get the parent URL as a string (in cases to modify parent URL), use this,

parentlocation = document.referrer;


Javascript function to get parent URL

This is usefull in case of finding parent URL from iframe or a popup etc.,

function getParentUrl() {
    var isInIframe = (parent !== window),
        parentUrl = null;

    if (isInIframe) {
        parentUrl = document.referrer;
    }

    return parentUrl;
}

Thursday, December 5, 2013

Use JQuery Squeeze box for modal display

To use modal pop up in joomla, there is an in-built javascript plugin Squeeze box based on the mootools library.

JHtml::_('behavior.modal');

This will add the squeeze box modal library.

And then add a class "modal" to the anchor.

Example
<a href="http://www.example.com/somepage.html" 
class="modal" rel="{size: {x: 700, y: 500}, handler:'iframe'}" 
id="modalLink1">
Click here to see this interesting page</a>

The above will load a iframe in modal box.

Reference
http://www.spiralscripts.co.uk/Joomla-Tips/using-modal-windows-with-joomla.html

Monday, December 2, 2013

Check if a var available in jQuery

This functions returns value 1 if var has some value and 0 when var is not present

jQuery.fn.exists = function(){return this.length>0;}

if ($(selector).exists()) {
    // Do something
}

Wednesday, October 9, 2013

Disable Native tooltip in HTML with help of JQuery

This will remove the title attribute on mouseover and then adds the title back on mouse out.

$(document).ready(function() {
    $('[title]').mouseover(function () {
        $this = $(this);
        $this.data('title', $this.attr('title'));
        // Using null here wouldn't work in IE, but empty string will work just fine.
        $this.attr('title', '');
    }).mouseout(function () {
        $this = $(this);
        $this.attr('title', $this.data('title'));
    });
});