Converts a pseudo array in JavaScript to a true array
In JavaScript, the hidden variables in a function arguments and the set of elements obtained with getElementsByTagName (nodelist) are not real arrays, and methods such as push are not used, and can only be converted to real arrays when this is needed.
For arguments, Array.prototype.slice.call (arguments) can be used; To achieve the purpose of conversion, but for nodelist is not, its in IE8 and below will be an error, can only say that its JS engine restrictions.
Therefore, if you need to convert nodelist to a real array, you need to do a compatibility process.
Copy Code code 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 ret;
}
}
JavaScript Set Home page features
Copy Code code as follows:
<! DOCTYPE html>
<title>javascript set "Set as Home" and "Favorites page" (Compatible with IE and Firefox) </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 action is rejected by the browser and if you want to enable it, enter About:config in the Address bar and then signed.applets.codebase_principal_support the 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 automatic setting of the home page, please manually set it using the browser menu!");
}
}
</script>
<body>
<a onclick= "setHomePage ()" title= "Set as homepage" href= "javascript:void (0);" > Homepage </a>
</body>
JavaScript collection feature
Copy Code code as follows:
<! DOCTYPE html>
<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 '). Addfavorite (DOCUMENT.TITLE,LOCATION.HREF);
});
</script>
<body>
<a href= "javascript:;" title= "Favorites site" id= "fav" > Favorites site </a>
</body>
Based on jquery, you can modify it according to your own needs.
JavaScript detects whether an element supports a property code
Copy Code code as follows:
function Elementsupportsattribute (element, attribute) {
var test = document.createelement (element);
if (attribute in test) {
return true;
} else {
return false;
}
};
Usage:
Copy Code code as follows:
if (Elementsupportsattribute ("textarea", "placeholder") {
} else {
Fallback
}
Creating and Using namespaces
Copy Code code 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
Copy Code code as follows:
Global.namespace ("Lang");
GLOBAL. Lang.test = function () {
Todo
}
The above is the entire content of this article, I hope you can enjoy.