JS snippets

This post will be my container for js snippets…

Last updated: 2015-09-16

Ensure code called onload, even if onload already happened

<script type="text/javascript" src="/_layouts/MyIf_Customization/jquery-1.6.3.min.js"></script>
<script type="text/javascript">
    function moreOnload() {
	//Do something
        $("#mydiv").hide();
    }
    //Call moreOnload() earliest after ready
    if (document.readyState === 'complete') {
        moreOnload();
    } else {
        $(document).ready(moreOnload);
    }
</script>

Namespacing and onload

// top-level namespace
var MYAPPLICATION = MYAPPLICATION || {
    calculateVat: function (base) {
        return base * 1.21;
    },
    product: function (price) {
        this.price = price;
        this.getPrice = function () {
            return this.price;
        };
    },
    doCalculations: function () {
        var p = new MYAPPLICATION.product(100);
        alert(this.calculateVat(p.getPrice()));
    }
}
//Onload Public
var p = new MYAPPLICATION.product(150);
alert(p.getPrice());

//Onload Private - not publishing vars
(function () {
    var p = new MYAPPLICATION.product(150);
    alert(p.getPrice());
})();

Thanks to

Ongoing ….

The End

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: