Convert pseudo-arrays in JavaScript to true arrays
In JavaScript, the hidden variable arguments in the function and the element set (NodeList) obtained with getElementsByTagName are not real arrays, and methods such as push cannot be used. When there is such a need, they can only be converted to real arrays first.
For arguments, you can use Array.prototype.slice.call(arguments); to achieve the purpose of conversion, but for NodeList, it will not work. It will report an error in IE8 and below. It can only be said that its JS engine has some restrictions.
Therefore, if you need to convert NodeList to a real array, you need to do a downward compatibility process.
The code copy is as follows:
function realArray(c) {
try {
return Array.prototype.slice.call(c);
} catch (e) {
var ret = [], i = 0, len = c.length;
for (; i < len; i++) {
ret[i] = (c[i]);
}
return return return;
}
}
JavaScript setting home page function
The code copy is as follows:
<!DOCTYPE html>
<html>
<head>
<title>JavaScript settings "Set as Home" and "Favorites" (compatible with IE and Firefox browsers)</title>
<meta charset="utf-8">
<script type="text/javascript">
function setHomepage() {
if (document.all) {
/*IE*/
document.body.style.behavior = 'url(#default#homepage)';
document.body.setHomePage(window.location.href);
} else if (window.sidebar) {
/*FF*/
if (window.netscape) {
try {
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
} catch (e) {
alert("This operation is rejected by the browser. If you want to enable this function, please enter about:config in the address bar, and then enter the item signed.applets.codebase_principal_support value to true");
}
}
var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
prefs.setCharPref('browser.startup.homepage', window.location.href);
} else {
/*chrome or other*/
alert("Your browser does not support automatic setting of home page, please use the browser menu to set it manually!");
}
}
</script>
</head>
<body>
<a onclick="setHomepage()" href="javascript:void(0);">Set as homepage</a>
</body>
</html>
JavaScript collection function
The code copy is as follows:
<!DOCTYPE html>
<html>
<head>
<title>demo</title>
<meta charset="utf-8">
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script type="text/javascript">
jQuery.fn.addFavorite = function(l, h) {
return this.click(function() {
var t = jQuery(this);
if(jQuery.browser.msie) {
window.external.addFavorite(h, l);
} else if (jQuery.browser.mozilla || jQuery.browser.opera) {
t.attr("rel", "sidebar");
t.attr("title", l);
t.attr("href", h);
} else {
alert("Please use Ctrl+D to add this page to your favorites!");
}
});
};
$(function(){
$('#fav').addFavite(document.title,location.href);
});
</script>
</head>
<body>
<a href="javascript:;" id="fav">Save this site</a>
</body>
</html>
Based on JQuery, you can modify it according to your needs.
JavaScript detects whether an element supports a certain attribute code
The code copy is as follows:
function elementSupportsAttribute(element, attribute) {
var test = document.createElement(element);
if (attribute in test) {
return true;
} else {
return false;
}
};
usage:
The code copy is as follows:
if (elementSupportsAttribute("textarea", "placeholder") {
} else {
// fallback
}
Create and use namespaces
The code copy is as follows:
var GLOBAL = {};
GLOBAL.namespace = function(str){
var arr = str.split('.'),o = GLOBAL;
for(k=(arr[0]=="GLOBAL")?1:0;k<arr.length;k++){
o[arr[k]]=o[arr[k]]||{};
o=o[arr[k]];
}
}
How to use
The code copy is as follows:
GLOBAL.namespace("Lang");
GLOBAL.Lang.test = function(){
//todo
}
The above is all about this article, I hope you like it.