Home > Web Front-end > JS Tutorial > js imitates Weibo to implement statistical characters and local storage functions_javascript skills

js imitates Weibo to implement statistical characters and local storage functions_javascript skills

WBOY
Release: 2016-05-16 15:24:06
Original
1335 people have browsed it

With the popularization of mobile devices and web applications, in order to better facilitate users to use, the user experience requirements for web pages or applications are getting higher and higher. This is indeed the case. As users, they prefer to choose the one with the best user experience. websites or applications, so as developers we need to develop more human-like applications.

I believe many people have experience using Weibo. For a social platform like Weibo, a good user experience has become particularly important.

For example: when we post on Weibo, the text box will prompt us in real time the number of remaining characters. This humanized prompt makes it easier for users to know the word limit of Weibo, and also limits the number of words that the user can input.

There is a saying that we should keep in mind: every input must have restrictions; every input must be verified.

In the next article, we will introduce how to implement the real-time prompt function of input characters and local storage (localStorage) technology.

1. jQuery character statistics plug-in
Now, we take Sina Weibo's Weibo input box as an example to introduce the use of jQuery to prompt the user for the number of remaining characters in real time.

Sina Weibo has a limit of 140 Chinese characters (280 English characters). Of course, there are also various other character spaces. Before the user input reaches the limit or reaches it, a good user experience should prompt the user to approach or reach the limit. , of course we can prompt users by using different colors or bold fonts.

Figure 1 Sina Weibo user input restrictions

The character count statistics plug-in will create a sibling element span after the input box, which is used to display the current number of remaining characters. When the keyup, keydown and change events of the input box are triggered, the number of remaining characters in the span will be modified in real time. If The number of remaining characters is close to "warning" (close to zero). Modify the CSS style to prompt the user that the input limit is approaching.

When the remaining characters reach "warning", add the corresponding style class in the span element. When the remaining characters are equal to or greater than the input limit, add the corresponding style class to prompt the user that the character limit has been exceeded.

We use the character count plug-in to dynamically insert the following code into the page:

<!-- adds element dynamic -->
<span class="counter">140</span>
Copy after login

By default, the character limit is 140. When the number of input characters is less than or equal to 25, the user is prompted. When the number of input characters is less than or equal to 0, the user is prompted that the number of characters exceeds the limit. Below we define the condition object by default:

// The default limitation.
var defaults = {
 allowed: 140,
 warning: 25,
 css: 'counter',
 counterElement: 'span',
 cssWarning: 'warning',
 cssExceeded: 'exceeded',
 counterText: ''
};
Copy after login

Above, we defined the defaults object, which contains attributes such as allowed, warning, css, cssWarning and cssExceeded. By modifying the defaults object attributes, we can easily modify the character statistics plug-in.

  • Allowed: The number of characters allowed to be entered.
  • Warning: Prompts the user that the number of remaining characters is close to zero.
  • Css: CSS style class name added to the counter element.
  • cssWarning: warning prompt style.
  • cssExceeded: Character limit exceeded prompt style.

Next, we define the method calculate() in the character statistics plug-in, which calculates the current number of remaining characters. If it reaches the warning range, it adds the style class "warning" to the page. When the number of remaining characters is less than or equal to zero, Add the style "exceeded" to the page.

/***
* Calculates the char
* @param obj
*/
function calculate(obj) {

 // Get the count.
 var count = getLength($(obj).val());
 var available = options.allowed - count;

 if (available <= options.warning && available >= 0) {
  $(obj).next().addClass(options.cssWarning);
 }
 else {
  $(obj).next().removeClass(options.cssWarning);
 }
 if (available < 0) {
  $(obj).next().addClass(options.cssExceeded);
 }
 else {
  $(obj).next().removeClass(options.cssExceeded);
 }
 $(obj).next().html(options.counterText + available);
}

Copy after login

We also define the method getLength(). When the input character is Chinese, totLen increases by 1. If it is an English character or number, totLen increases by 0.5 (default allows 140 Chinese characters to be entered).

/**
* Get the length of char.
* @param str
* @return {Number}
*/
function getLength(str) {
 var totLen = 0;
 for (var i = 0; i < str.length; i++) {
  // If the char is Chinese.
  if (str.charCodeAt(i) > 256) {
   totLen += 1;
  }
  else {
   totLen += 0.5;
  }
 }
 return Math.floor(totLen);
}
Copy after login

Next, we bind the keyup(), keydown() and change() event methods in the control. When the page object triggers the keyup(), keydown() or change() event method, call the calculate() method Calculate the current number of remaining characters and add the corresponding CSS style to the page.

// Binds text area keyup, keydown and change event.
this.each(function() {
 $(this).after('<' + options.counterElement + ' class="' + options.css + '">' + options.counterText + '</' +
    options.counterElement + '>');
 calculate(this);
 $(this).keyup(function() { calculate(this), storeWeibo(this) });
 $(this).keydown(function() { calculate(this), storeWeibo(this) });
 $(this).change(function() { calculatea(this) });
});
Copy after login

2、Web Storage
现在,我们基本实现了jQuery字符数统计插件功能了,相信许多人都注意到,如果我们在发微博时,没有发送出去的微博下次打开页面,发送框依然保存着我们未发送的微博,即使关闭浏览器重新打开页面,我们没发送的信息依然存在。

其实,要实现这一个功能方法是多种多样的,例如我们可以使用:Cookies,Session等技术。

随着HTML5规范的制定,与此同时W3C制定了网络存储(Web Storage)的规范,它提供将数据存储在客户端中,直到Session过期(会话存储)或超出本地容量(本地存储),它比传统的Cookies存储功能更强大、更容易实现和容量更大(大部分浏览器支持5M的本地存储)。

会话存储
会话存储:它将数据保存在会话中,一旦我们关闭浏览器选项卡时,会话中的数据将失效。

本地存储
本地存储:当数据需要持久地保存在客户端中,这时我们可以使用本地存储(Local Storage),它是以key/value 的形式来存储数据的,如果关闭了页面或浏览器后,重新打开页面数据依然存在,它提供了数据的持久保存。一个简单的应用是:记录用户访问页面的次数。

图2存储空间的对比

接下来,我们将介绍如何使用本地存储保存用户数据。

由于,localStorage提供了setItem(),getItem(),removeItem(),key()和clear() 5个方法,和一个属性length,具体定义如下:

// Storage definition.
interface Storage {
 readonly attribute unsigned long length;
 DOMString key(in unsigned long index);
 getter any getItem(in DOMString key);
 setter creator void setItem(in DOMString key, in any value);
 deleter void removeItem(in DOMString key);
 void clear();
};
Copy after login

在现代浏览器中使用本地存储是非常的简单,我们只需在Javascript代码中直接调用localStorage对象的方法或属性就OK了。

// stores the username 'jkrush',
// then get the username.
localStorage.setItem('username', 'jkrush');
var userName = localStorage.getItem('username');
Copy after login

上面,我们通过调用localStorage的setItem()和getItem()方法实现数据的存储和获取,由于localStorage是以Key/Value形式存储数据的,所以我们在存储时需要提供Key/Value值,然后调用getItem()方法获取存储在Key中的值。

由于本地存储是以Key/Value的形式进行存储的,那么我们可以很容易存储字符串类型的数据,如果我们需要存储对象类型,那么本地存储就显得捉襟见肘了。

假设,我们把一个student对象存储到localStorage中,具体代码如下:

// Defines a student object.
var student = {
 name: 'JK_Rush',
 age: '26',
 sex: 'male'
};

// Prints student object
console.log(student);

// Stores student object.
// Gets student object again.
localStorage.setItem('student', student);
console.log(localStorage.getItem('student'));

Copy after login

图3 localStorage存储对象

通过上面示例,我们注意到在Firebug的控制台中输出的并不是真正的student对象,而是student对象的信息而已。

那么我们该如何把对象存储到localStorage中呢?其实,我们可以把对象序列化为JSON数据进行存储,最后通过反序列化把JSON数据转换为对象。具体实现如下:

// Defines a student object.
var student = {
 name: 'JK_Rush',
 age: '26',
 sex: 'male'
};

console.log(student);

// Serializes the object to json string.
localStorage.setItem('student', JSON.stringify(student));

// Deserializes the json string to object.
console.log(JSON.parse(localStorage.getItem('student')));

Copy after login

上面示例中,在存储student对象之前,我们使用JSON的stringify()方法序列化对象为JSON字符串,然后存储到localStorage中;如果我们要获取student对象,只需使用JSON的parse()方法反序列化字符串为对象。

图4 localStorage存储对象

上面,我们实现了student对象转换为JSON格式字符串存储到localStorage中,接下来,我们在前面的例子中添加localStorage功能,具体代码如下:

/**
* Store user data into local storage.
* @param obj
*/
function storeWeibo(obj) {

 // Checks the browser supports local storage or not.
 if (window.localStorage) {
  localStorage.setItem('publisherTop_word', $(obj).val());
 }
 else {

  // For instance, ie 6 and 7 do not support local storage,
  // so we need to provider other way.
  window.localStorage = {
   getItem: function(sKey) {
    if (!sKey || !this.hasOwnProperty(sKey)) { return null; }
    return unescape(document.cookie.replace(new RegExp("(&#63;:^|.*;\\s*)" + escape(sKey).replace(/[\-\.\+\*]/g,
       "\\$&") + "\\s*\\=\\s*((&#63;:[^;](&#63;!;))*[^;]&#63;).*"), "$1"));
   },
   key: function(nKeyId) {
    return unescape(document.cookie.replace(/\s*\=(&#63;:.(&#63;!;))*$/, "").split(/\s*\=(&#63;:[^;](&#63;!;))*[^;]&#63;;\s*/)[nKeyId]);
   },
   setItem: function(sKey, sValue) {
    if (!sKey) { return; }
    document.cookie = escape(sKey) + "=" + escape(sValue) + "; expires=Tue, 19 Jan 2038 03:14:07 GMT; path=/";
    this.length = document.cookie.match(/\=/g).length;
   },
   length: 0,
   removeItem: function(sKey) {
    if (!sKey || !this.hasOwnProperty(sKey)) { return; }
    document.cookie = escape(sKey) + "=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/";
    this.length--;
   },
   hasOwnProperty: function(sKey) {
    return (new RegExp("(&#63;:^|;\\s*)" + escape(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=")).test(document.cookie);
   }
  };
  window.localStorage.length = (document.cookie.match(/\=/g) || window.localStorage).length;
 }
}

Copy after login

现在我们在自定义字符统计插件(jquery.charcount.js)中,添加方法storeWeibo(),首先我们判断当前浏览器是否支持localStorage,主流的浏览器如:Chrome、Firefox、Opera、Safari以及IE 8都支持本地存储(localStorage)和会话存储(sessionStorage)。

如果浏览器支持本地存储,那么我们可以直接调用localStorage的setItem()方法,将textarea中的数据存储起来;当我们再次打开页面或浏览器,首先检查localStorage是否存储了相应的数据,如果有数据存储,那么我们再次把数据取出显示到textarea中。

但由于一些用户可能使用旧版的浏览器(如:IE6和IE7),考虑到兼容我们必须提供支持旧版浏览器的方案。

我们知道旧版浏览器(如:IE6和IE7),它们支持Cookies的持久化存储方式,所以我们使用Cookies实现getItem(), setItem()和removeItem()等方法。

图5 主流浏览器支持Web Storage

现在,我们已经完成了字符统计插件jquery.charcount.js,由于时间的关系我们已经把发送框的界面设计好了,具体的HTML代码如下:

<!-- From design-->
<body>
 <form id="form" method="post">
 <h2>
  有什么新鲜事想告诉大家?</h2>
 <div>
  <label class="mali_oglas_kategorija" for="message">
   有什么新鲜事想告诉大家?<b></b></label>
  <textarea id="weiboMsg" placeholder="请Fun享"></textarea>
  <span class="counter"></span>
  <input onclick="SaveCache()" type="submit" value="发布">
 </div>
 </form>
</body>
Copy after login

图6 发送框界面设计

接下来,我们在页面代码中引用jQuery库和自定义字符统计插件jquery.charcount.js,具体代码如下:

<!-- Adds Javascript reference -->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script type="text/javascript" src="./js/jquery.charcount.js"></script>
Copy after login

上面,我们直接引用Google提供的jQuery库,当然我们也把jQuery库下载到本地,然后引入到项目中,接下来我们在HTML页面中添加调用字符统计插件的代码,具体代码如下:

 <!-- When document ready invokes charCount function-->
<script type="text/javascript">
 $(document).ready(function () {
  // Uses default setting.
  $("#weiboMsg").charCount();
 });
</script>
Copy after login

上面,我们完成了在页面代码中调用字符统计插件,每当我们在文本框中输入字符时,都会实时地显示剩余的字符数,而且我们在文本框中输入的字符都会保存到localStorage中。

接下来,我们分别在Chrome和Firefox中查看保存在localStorage中的数据。

首先,我们打开Chrome的“开发者工具”(Ctr+Shift+I),然后我们选择“Resources”选项,这时我们就可以看到保存在localStorage中的数据了。

图7 Chrome的本地存储

同样,我们打开Firefox的“Firebug”(F12),然后我们选择“DOM”选项,这时我们需要查找window的对象localStorage,这样就可以看到保存在localStorage中的数据了。

图8 Firefox的本地存储

我们知道IE8也是支持localStorage对象的,但是我做测试时候发现IE8中一直提示localStorage对象未定义,后来我上Stackoverflow查看了一下,有人说在IE8中,localStorage对象是依赖于域名的,所以需要运行在Web服务器中才可以成功保存数据到localStorage中。

我们注意到微博通过本地存储技术,保存用户在发送框中的数据,一旦数据发送了就清空本地存储,反之保存用户的输入。

本文通过微博发送框例子介绍了如何定义jQuery字符统计插件和本地存储技术,首先,我们知道限制用户输入是必须的,但如何有效而且人性化提示用户输入限制呢?这里我们通过定义一个jQuery插件,动态地统计剩余字符数,希望对大家学习javascript程序设计有所启发。

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template