Self-written javascript front-end waiting controls, javascript front-end

Source: Internet
Author: User

Self-written javascript front-end waiting controls, javascript front-end

Wait for the controls to be searched on the Internet, but they are complicated and not necessarily easy to use. Besides, my project is caused by bootstrap, and I am afraid to use third-party controls easily, for fear of incompatibility, so I wrote a waiting control by myself. Its technical points include dynamic loading of CSS and javascript namespaces, so let's record it.

The waiting control displays a message indicating "loading data. Please wait ...", After the operation is successful, the callback function will prompt to disappear. The principle is that this wait control is completely added by JS dynamically, including CSS, and there is no preset in the page.

So how can I dynamically load this CSS? In the wait control, the style uses class. If we write these classes in the style file in advance, the calling page must reference the relevant JS files and CSS files; even if the css file is dynamically loaded in the js file, think about it. A simple control contains two files.

I dynamically pieced together and loaded CSS in JS.

The Code is as follows:

Var FTabPages = function () {var tabKeeper = null; // e.g. // tabKeeper = {// container: "" //, isErase: true //, url: "" //, params: {} //, callback: null //}; var wrap = $ (document. body); function initTab (tabJson) {tabKeeper = tabJson;} function onTab (tabJson) {// switch the tab if (tabKeeper! = Null) {var divPrev = $ (tabKeeper. container); if (tabKeeper. isErase) {divPrev. empty ();} divPrev.css ("display", "none");} tabKeeper = tabJson; var div =$ (tabJson. container); div.css ("display", ""); if ().trim(div.html ()). length =) {// loadwaiting (); getViewRequest (tabJson. url, tabJson. params, function (data) {div.empty().html (data); docallback (tabJson. callback); removeloading ();}, Function (data) {alert ("data retrieval timeout or failure! "); Removeloading ();} else {// hide but not clear docallback (tabJson. callback) ;}} function getViewRequest (url, params, onsuccess, onerror) {$. ajax ({type: 'get', url: url, data: params, contentType: "text/html; charset = utf-", timeout:, success: function (data) {if (onsuccess! = Undefined & onsuccess! = Null) {onsuccess (data) ;}, error: function (data) {if (onerror! = Undefined & onerror! = Null) {onerror (data) ;}}) ;}function docallback (callback) {if (typeof callback! = 'Undefined' & callback instanceof Function) {callback () ;}} function resetTab () {// refresh the current tab loadwaiting (); var div =$ (tabKeeper. container); getViewRequest (tabKeeper. url, tabKeeper. params, function (data) {div.empty().html (data); div.css ("display", ""); docallback (tabKeeper. callback); removeloading () ;}) ;} function loadwaiting () {// display the wait Information $ ("<div class = \" datagrid-mask \ "> </div>" detail .css ({display: "block", width: wrap. width (), height: wrap. height ()}). appendTo (wrap); $ ("<div class = \" datagrid-mask-msg \ "> </div>" loading .html ("loading data... "Maid ({display:" block ", left: (wrap. width ()-$ ("div. datagrid-mask-msg ", wrap ). outerWidth ()/, top: ($ (window ). height ()-$ ("div. datagrid-mask-msg ", wrap ). outerHeight ()/});} function removeloading () {// hide the wait information wrap. find ("div. datagrid-mask-msg "). remove (); wrap. find ("div. datagrid-mask "). remove ();} function initloading () {// sets the wait control style var css = ". datagrid-mask {"; css + =" position: absolute; "; css + =" left:; "; css + =" top:; "; css + =" width: %; "; css + =" height: %; "; css + =" opacity :.; "; css + =" filter: alpha (opacity =); "; css + =" display: none; "; css + ="} "; css + = ". datagrid-mask-msg {"; css + =" position: absolute; "; css + =" top: %; "; css + =" margin-top:-px; "; css + =" padding: px; "; css + =" width: auto; "; css + =" height: px ;"; css + = "border-width: px;"; css + = "border-style: solid;"; css + = "display: none ;"; css + = "}"; // dynamic loading of CSS if (document. all) {window. style = css; document. createStyleSheet ("javascript: style");} else {var style = document. createElement ('style'); style. type = 'text/css '; style. innerHTML = css; document. getElementsByTagName ('head '). item (). appendChild (style) ;}} initloading (); return {// the method onTab: function (tabJson) {onTab (tabJson) ;}, resetTab: function () {resetTab () ;}, init: function (tabJson) {initTab (tabJson );}};}();

How can we call the external interface? In this way:

FTabPages.init({  container: "#div_BasicInfo"  , isErase: true  , url: "http://blog.csdn.net/leftfist"  , params: {}  , callback: function () {     alert("Hello World!");  }  });

The above is the implementation process of the javascript front-end wait control. I hope this article will help you.

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.