JavaScript easy to handle the quick message function with just one line of code _javascript tips

Source: Internet
Author: User
Don't worry, the method is very simple, or listen to me first tell the origin of it ...

At the beginning, the function of the express message is traced back to the publication [Ajaxcontroltoolkittests Automatic test Framework complete parsing one: Architecture Article] This article, when it was purely a by-product of the article, was not too concerned about, I would like to just give the same lazy people as I prepared a shortcut , and later, because there is no hint of this function will be directly automatically submitted messages, but also caused a few students dissatisfied, and then in the eye-catching position plus a hint: [Hint: Quick link will directly recommend and submit message information. ], only to quell the situation;

Later, some students may be the mouse is too advanced, combo speed particularly fast reason, every use of my quick message function will be five (I want to top, passing, look forward to the next, too good, I want to recommend) shortcut links are all the point of the fun, originally, I put so many quick links is to give you more than a few options to provide, The intention is not to want you to each point again, the result is originally for lazy people to provide lazy tools, but let people suddenly become hard-working, it is a sin. Everyone is so hard-working, I can not afford, the message is like a chat room brush screen, the fundamental, or the original program is too simple to write, did not consider the details, so now only left two quick links, and added restrictions on the program, so that will not be able to brush the screen.

Then again, in recent days, I found that the function of Express message is more and more people added to their blog, yesterday saw this article (here) is almost unchanged white box copy of my shortcut message function code. These applications let me rethink the shortcut message This small function positioning, objectively speaking, this function is actually on the blog park existing message function A very good extension! So, instead of letting people reinvent the wheel repeatedly, why not provide a public extension for direct use?
Based on the above historical reasons, after several hours of efforts, the new version of the General Express message function was born! This version of the goal is to do a simple general, other blog Park bloggers need to simply quote can be directly in their blog to add a quick message function! At present, the function is set to v1.0bate version, there are some features to be perfected, future releases will provide more user-defined settings, and new ideas will be added incrementally, so if you want to be able to automatically upgrade the new version in the future, just refer to my file directly, and all users who use this script will synchronize the updates simultaneously.
In fact, the usage is really very simple, only in the [admin]->[set]->[page first HTML code] text box, fill in the following sentence on the line.
Copy Code code as follows:

In a text box [background admin]->[set]->[page first HTML code]
<script language= "javascript" type= "Text/javascript" src= "Http://files.cnblogs.com/justinw/jMsg.js" ></ Script>

As shown in the following illustration:

Here is the entire General Express message function of the source code, the time is very short, the code did not undergo a rigorous test, so please use the actual test first, you are welcome to criticize correct! The code is very simple, I also added a lot of comments, here is no longer tired, there are questions direct message on the line.
Copy Code code as follows:

Author: Justin
Copyright: Reprint, please keep the source.
Version: V1.0 Beta
Last Updated: 201003300330
Note: If there is a problem, you can give priority to http://www.cnblogs.com/justinw/archive/2010/03/30/1700190.html message questions.

--begin--jmsg---
function jmsg () {}

Initialization action
JMsg.prototype.setup = function () {
Jmsg.loadevent (This.combat);
Jmsg.loadevent (JMSG.MSGDIV);
Jmsg.msgdiv ();
}

Submit Message
JMsg.prototype.post = function (msg) {
This is done by judging Haveup flag bits to prevent repeated submissions of the same message.
if (window.top.HaveUp) {
Alert (' You've already topped it! Thank you friend:);
Return
}

var txt = document.getElementById (' tbcommentbody ');
if (txt!= null) {
Txt.style.backgroundColor = "#E4F5FF";
var date = new Date ();
Txt.value = msg;
Txt.focus ();
Haveup is set to true if a quick message is submitted
Each time you re-enter the page, the Haveup is initialized.
Window.top.HaveUp = true;
This is the blog to submit a message in the way, through here is the submission of messages.
If you call this method directly on the client without logging on, the interior throws a mistake. (Blog yard code problem, no null sentence)
Postcomment ();
}
else {
At present, the function of the blog park only after login to submit a message.
If the tbcommentbody element is not found, you can assume that the current user is not logged on.
Alert ("Use Blog Park message function need to log in first!") ");
}
}

Recommended
JMsg.prototype.recommend = function () {
var diggit = jmsg.getelementsbyclassname (' diggit ');
if ((diggit) && (Diggit[0])) {
Diggit[0].onclick ();
}
else {
Alert ("Debug: ClassName the recommended button is renamed!") ");
}
}

Against
JMsg.prototype.combat = function () {
var buryitmsg = function () {
At present, Blog Park recommendations and objections are anonymous, is opposed to the course want to know why.
This function can only do to prevent a gentleman from the villain, just give a hint.
var txt = document.getElementById (' tbcommentbody ');
if (txt!= null) {
Alert (' This classmate, very much hope to hear your valuable advice, please enlighten ... ');
Txt.style.backgroundColor = "#fe9ab3";
Txt.focus ();
}
}
var buryit = jmsg.getelementsbyclassname (' Buryit ');
if ((Buryit) && (Buryit[0])) {
Jmsg.addevent (Buryit[0], "click", Buryitmsg);
}
}

Submit a message while recommending
JMsg.prototype.superPost = function (msg) {
This.post (msg);
This.recommend ();
}

Additional onload Event
Jmsg.loadevent = function (fn) {
var oldonload = window.onload;
if (typeof window.onload!= ' function ') {
Window.onload = fn;
} else {
Window.onload = function () {
Oldonload ();
FN ();
}
}
}

Attached events
jmsg.addevent = function (obj, type, fn) {
if (Obj.addeventlistener)
Obj.addeventlistener (Type, FN, true);
else if (obj.attachevent) {
Obj.attachevent ("On" + Type, function () {
FN ();
});
}
}

Finding DOM elements by classname
Jmsg.getelementsbyclassname = function (n) {
var el = [],
_el = document.getElementsByTagName (' * ');
for (var i = 0; i < _el.length; i++) {
if (_el[i].classname = = N) {
El[el.length] = _el[i];
}
}
Return el;
}

Floating navigation bar for express messages
Jmsg.msgdiv = function () {
Whether a floating bar appears.
if (!) ( Window.location.href.indexOf (". html") >-1) return;

Dynamically calculate the position of a floating scroll bar
lastscrolly = 0;
var beat = function () {
var Diffy;
if (document.documentelement && document.documentElement.scrollTop)
Diffy = Document.documentElement.scrollTop;
else if (document.body)
Diffy = Document.body.scrollTop
Else
{/*netscape stuff*/}

Percent =. 1 * (diffy-lastscrolly);
If (Percent > 0) percent = Math.ceil (percent);
else percent = Math.floor (percent);
document.getElementById ("Msgdiv"). Style.top = parseint (document.getElementById ("Msgdiv"). Style.top) + percent + "px" ;

lastscrolly = lastscrolly + percent;
}

Msgdivcode = "<div id=\" msgdiv\ "style= ' z-index:2010"; right:2px; top:400px; Position:absolute; ' ><strong><span style=\ "color:red; Font-size:10pt;\ ">[Express message: Click on the link below will be directly submitted message information. ]&LT;/SPAN&GT;&LT;/STRONG&GT;&LT;BR/><a onclick=\ "$JMSG. Superpost (' Good stickers! I have you! '); return false;\ "href=\" ###\ "><span style=\" font-size:30pt;\ "> I want the top! </span></a> <a onclick=\ "$JMSG. Superpost (' Good stickers! '); return false;\ "href=\" ###\ "><span style=\" font-size:18pt;\ "> Pass </span></a></div>";
document.write (Msgdivcode);

Window.setinterval (beat, 120);
}
--end--jmsg---
TODO: Provides an interface for setting whether to display a floating bar
TODO: interfaces that provide custom floating bar pictures
TODO: interfaces that provide full custom floating bar content
TODO: provides an interactive mode interface other than a floating bar
var $jMsg = new Jmsg ();
$JMSG. Setup ();

Update:201003301250-> for the maintenance of blog Park recommended ranking mechanism of fair and effective and world peace, the special Superpost method is modified to not automatically recommend the article, but pop-up prompt box to ask the user "whether to recommend this article", the original [passing by] Link to use post method to submit, only express message information, will not automatically recommend:

Copy Code code as follows:

JMsg.prototype.superPost = function (msg) {
This.post (msg);
The query function is added here, the automatic direct recommendation is not supported
if (confirm), do you want to recommend the article at the same time? ")) {
This.recommend ();
}
}

Author: Justin
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.