Home >Web Front-end >JS Tutorial >Commonly used js code organization

Commonly used js code organization

一个新手
一个新手Original
2017-09-09 15:14:022310browse

1. Get the address bar parameters

function request(paras) {
    var url = location.search;
    var paraString = url.substring(url.indexOf("?") + 1, url.length).split("&");
    var paraObj = {}
    for (i = 0; j = paraString[i]; i++) {
        paraObj[j.substring(0, j.indexOf("=")).toLowerCase()] = j.substring(j.indexOf("=") + 1, j.length);
    };
    var returnValue = paraObj[paras.toLowerCase()];
    if (typeof(returnValue) == "undefined") {
        return "";
    } else {
        return returnValue;
    };
};

Call the method, var status = request('status'); Get the status parameters in the address bar

2. jq, made with index value Switching between tabs and content

function switch_tab(title, content) {
	title.first().addClass("on");
	content.first().show();
	title.click(function() {
		var a = $(this).index()
		if(content.eq(a).css("display") != "block") {
			content.hide(),
			content.eq(a).show(),
			title.removeClass("on"),
			$(this).addClass("on");
		};
	});
};

Call the method, switch_tab($('.title_box .title'),$('.content_box .box')) to get the status parameter in the address bar

3. js timestamp processing

// 传入时间戳。输出格式为:2017-05-14 00:08:46
common.pattern = function(data) {
    function replace(m) {
        return m < 10 ? &#39;0&#39; + m: m
    }
    var _date = new Date(parseInt(data));
    var re_date = replace(_date.getFullYear()) + "-" + replace(_date.getMonth() + 1) + "-" + replace(_date.getDate()) + " " + replace(_date.getHours()) + ":" + replace(_date.getMinutes()) + &#39;:&#39; + replace(_date.getSeconds());
    return re_date;
};

4. // Determine whether the mobile phone system is Android or ios

var ua = navigator.userAgent.toLowerCase();

if (/iphone|ipad|ipod/.test(ua)) {
    
} else if (/android/.test(ua)) {
   
};

5. Prevent the right-click pop-up to view the code

// 阻止右键
document.body.onselectstart = document.body.oncontextmenu = function() {
    return false;
}

The above is the detailed content of Commonly used js code organization. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn