Javascript Study Notes

Some useful tips like:
Format JavaScript Date to yyyy-mm-dd

Format JavaScript Date to yyyy-mm-dd

1
2
3
4
5
6
7

function formatDate(date) { //Format JavaScript Date to yyyy-mm-dd
var d = new Date(date),month = '' + (d.getMonth() + 1),day = '' + d.getDate(),year = d.getFullYear();
if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;
return [year, month, day].join('-');
}

How to run jQuery directly on any page in the browser?

sometimes, we need to debug in browser directly.

1
2
3
4
5
6

var body = document.getElementsByTagName("body")[0];
var script = document.createElement('script');
script.type = "text/javascript";
script.src = "http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js";
body.appendChild(script);