A few useful JavaScript scripts worth collecting

Source: Internet
Author: User
Tags format count return tagname
javascript| Script

Unicode encoding and encoding in Chinese with JavaScript

<script>
function Unicode (s) {
var len=s.length;
var rs= "";
for (Var i=0;i<len;i++) {
var k=s.substring (i,i+1);
rs+= "&#" +s.charcodeat (i) + ";";
}
Return RS;
}

function Runicode (s) {
var k=s.split (";");
var rs= "";
for (i=0;i<k.length;i++) {
var m=k.replace (/&#/, "");
Rs+=string.fromcharcode (m);
}
Return RS;
}
Alert (Unicode ("I Am a Pig"));//I'm a pig.
Alert (Runicode ("I Am a Pig"));
</script>

image dimensions are loaded and modified to fit the page width

<script>
function foo () {
var K=document.all;
for (Var i=0;i<k.length;i++) {
if (k.tagname== "IMG") {
if (k.width>500) {//500 is the maximum width
var height=math.ceil (k.height*500/k.width);
K.height=height;
K.width= "500";
}
}
}
}
</script>
<body >

<input type= "button" value= "sub"/>


A countdown function, 5 minutes of reminders, 0 seconds automatic submission

<script language= "JavaScript" >
<!--
var time= "3600";
var atime= "300"; Define Time
var etime= "0";
function count () {
SetTimeout ("Count ()", 1000); 1000 ms = 1 seconds Load count
Document.form1.rest.value=format (Math.floor (TIME/60)) + "min:" +format (time%60) + "seconds";
time--;
if (time==atime) {
Alert ("5 minutes left");
}
if (time==etime-1) {
Alert ("Time to, has been automatically submitted");
Document.form1.submit ();
}
}

function format (time) {
var len=time.tostring (). length;
for (i= "0"; i<2-len;i++) {
time+= "0";
}
return time;
}

-->
</SCRIPT>
<form action= "save.asp" method= "Post" Name= "Form1"/>
<input type= "button" value= "Start"/>
<input type= "text" name= "Rest" value= "60 Minutes: 00 Seconds"/>
</form>

a simple calculator.

<form name= "Form2" >
<input type= "button" value= "0" name= "a" >
<input type= "button" value= "1" name= "a" >
<input type= "button" value= "2" name= "a" >
<input type= "button" value= "3" name= "a" >
<input type= "button" value= "4" name= "a" >
<input type= "button" value= "5" name= "a" >
<input type= "button" value= "6" name= "a" >
<input type= "button" value= "7" name= "a" >
<input type= "button" value= "8" name= "a" >
<input type= "button" value= "9" name= "a" >
<input type= "button" value= "-" name= "a" >
<input type= "button" value= "+" Name= "a" >
<input type= "button" value= "*" Name= "a" >
<input type= "text" name= "Total" readonly>
<input type= "button" value= "calculation" name= "B" >
<input type= "button" value= "Empty" name= "C" >
</form>
<script language= "JavaScript" >
<!--
function count (Form1) {
var k = event.srcelement;
if (k.tagname== "INPUT") {
if (k.name== "a") {
Form1.total.value+=k.value;
}
else if (k.name== "B") {
var a=eval (Form1.total.value);
alert (a);
}
else if (k.name== "C") {
Form1.total.value= "";
}
}
}
-->
</SCRIPT>



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.