Summary of common JavaScript scripts (2)

Source: Internet
Author: User

Summary of common JavaScript scripts (2)

Common javascript scripts shared in this article include converting the pseudo array in JavaScript into a true array, setting the home page in JavaScript, adding a JavaScript collection, and checking whether the javascript element supports an attribute code, creating and using a name. space, if you need it, come and have a look.

 

 

Convert a pseudo array in JavaScript to a true Array

In JavaScript, The Hidden variable arguments in the function and the Element Set (NodeList) obtained using getElementsByTagName are not real arrays and cannot be pushed, if this is necessary, you can only convert it to a real array first.

For arguments, you can use Array. prototype. slice. call (arguments); to achieve the purpose of conversion, but NodeList does not work, it will report an error in IE8 and below, it can only be said that its JS engine is limited.

Therefore, if you want to convert NodeList to a real array, You need to perform compatibility processing.

 

The Code 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 ret;
}
}

 

Set the Home Page function in JavaScript

 

The Code is as follows:


<! DOCTYPE html>
<Html>
<Head>
<Title> set "set as Homepage" and "add to Favorites" in JavaScript (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, enter about: config in the address bar, and set the value of signed. applets. codebase_principal_support 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 others */
Alert ("your browser does not support automatic homepage setting. Please use the browser menu to manually set it! ");
}
}
</Script>
</Head>
<Body>
<A onclick = "setHomepage ()" title = "set as Homepage" href = "javascript: void (0);"> set as homepage </a>
</Body>
</Html>

 

JavaScript favorites

 

The Code 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 ("use Ctrl + D to add this page to favorites! ");
}
});
};
$ (Function (){
$ ('# Fav'). addFavorite (document. title, location. href );
});
</Script>
</Head>
<Body>
<A href = "javascript:;" title = "add site" id = "fav"> Add site to favorites </a>
</Body>
</Html>

 

Based on JQuery, you can modify it as needed.

Javascript checks whether an element supports an attribute code.

 

The Code is as follows:


Function elementSupportsAttribute (element, attribute ){
Var test = document. createElement (element );
If (attribute in test ){
Return true;
} Else {
Return false;
}
};

 

Usage:

 

The Code is as follows:


If (elementSupportsAttribute ("textarea", "placeholder "){
} Else {
// Fallback
}

 

Create and use namespaces

 

The Code 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];
}
}

 

Usage

 

The Code is as follows:


GLOBAL. namespace ("Lang ");
GLOBAL. Lang. test = function (){
// Todo
}

 

The above is all the content of this article. I hope you will like it.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.