Home > Computer Tutorials > Computer Knowledge > How to call a function written in JavaScript on a web page

How to call a function written in JavaScript on a web page

WBOY
Release: 2024-01-23 12:03:14
forward
826 people have browsed it

How to call a function written in JavaScript on a web page

I wrote a function using javaScript. How should I call it on the web page?

Add a tag with the id "timer" in the body so that you can use document.getElementById("id_Value") to get an Element object in your JavaScript program. Please note that this method only applies to IE browsers and browsers based on IE core.

How to dynamically display the current time on a web page

Use JS

A very simple function, use the Date() object to get the current time, and then use setTimeout to get the latest time every 1 second.

I encountered a small problem during the writing process: the original idea was to use setInterval() to get the latest time every 1 second, but this would cause a memory leak (the specific reason has not yet been clarified). Fortunately, with Rocky's reminder, I solved this problem by using setTimeout() instead.

01 function nowTime(ev,type){

02 /*

03 * ev: element showing time

04 * type: time display mode. If 12 is passed in, it will be a 12-hour system, if not, it will be a 24-hour system

05 */

06 //Year, month, day, hour, minute and second

07 var Y,M,D,W,H,I,S;

08 //When the unit is month, day, hour, minute and second, add zero in front

09 function fillZero(v){

10 if(v

11 return v;

12 }

13 (function(){

14 var d=new Date();

15 var Week=['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];

16 Y=d.getFullYear();

17 M=fillZero(d.getMonth() 1);

18 D=fillZero(d.getDate());

19 W=Week[d.getDay()];

20 H=fillZero(d.getHours());

21 I=fillZero(d.getMinutes());

22 S=fillZero(d.getSeconds());

23 //12-hour display mode

24 if(type & type==12){

25 //If you want to display more time types such as noon and early morning, you can add judgment below

26 if(H

27 H;

28 }else if(H>12 & H

29 H-=12;

30 fillZero(H);

31 }else if(H==24){

32;

33 }

34 }

35 ev.innerHTML=Y 'Year' M 'Month' D 'Day' ' ' W ' ' H ':' I ':' S;

36 //Update time per second

37 setTimeout(arguments.callee,1000);

38 })();

Problems I encountered when making a web page:

Take a look at your code post. I guess you directly used the code in the book, where the name of a certain text box was specified, but you don’t have this text box, so you are missing an object. I’ll give you a paragraph. Here is a relatively simple code that can display the time:

----------------------------------------

current time

----------------------------------

Please use the date function to display the current date on your web page

Javascript written to display the current time. . . Copy--paste into notepad--save as 1.html to see the effect

Displayed as: May 12, 2011 23:54:7 Sunday 4

The above is the detailed content of How to call a function written in JavaScript on a web page. For more information, please follow other related articles on the PHP Chinese website!

source:docexcel.net
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