search
HomeComputer TutorialsComputer KnowledgeHow to call a function written in JavaScript on a web page

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!

Statement
This article is reproduced at:Excel办公网. If there is any infringement, please contact admin@php.cn delete
How to Download Technic Launcher on Windows/Mac/Linux? - MiniToolHow to Download Technic Launcher on Windows/Mac/Linux? - MiniToolMay 03, 2025 am 12:50 AM

Technic Launcher will let you play your favorite mod packs instantly. Some players want to download it but don’t know how to do that. Don’t worry! This post from php.cn tells you how to download and install Technic Launcher.

Top 7 Workarounds for Apps Keep Crashing on Android - MiniToolTop 7 Workarounds for Apps Keep Crashing on Android - MiniToolMay 03, 2025 am 12:49 AM

Do you meet apps crashing when using your Android device for a long time? What do you do to address it? If this is happening to you and you have no idea about what to do, you can try the solutions below in this guide on php.cn Website to fix it.

Resolved! How to Fix the Windows Update Error 0X80073BC3? - MiniToolResolved! How to Fix the Windows Update Error 0X80073BC3? - MiniToolMay 03, 2025 am 12:48 AM

In the most cases, the error 0X80073BC3 refers to the issue happened on the Windows update process. That can stop the normal update-related service, hassling people a lot. So, how to get rid of the Windows update error 0X80073BC3? This post on php.cn

Top 5 Solutions to Valorant Error Code Val 7 Windows 10/11 - MiniToolTop 5 Solutions to Valorant Error Code Val 7 Windows 10/11 - MiniToolMay 03, 2025 am 12:47 AM

Like any other games, there are quite a few glitches and bugs in Valorant. Error code Val 7 is among them. In order to get rid of it, we try our best to come up with some workarounds for you in this post on php.cn Website and hope it can help you out

Top 4 Proven Ways to Ctrl F Not Working on Windows 10/11Top 4 Proven Ways to Ctrl F Not Working on Windows 10/11May 03, 2025 am 12:46 AM

Ctrl F make our life easier and it must be quite frustrating when Ctrl F not working Windows 10. In this guide on php.cn Website, we’ll provide you with some easy and efficient ways to get this convenient shortcut to work for you again.

Easy Ways to Remove an Administrator Account on Windows 10/11Easy Ways to Remove an Administrator Account on Windows 10/11May 03, 2025 am 12:45 AM

For some reason, you might want to remove the administrator account on your Windows 10 or Windows 11. You can do this in the Settings app or in Control Panel. php.cn Software will introduce these two easy ways in this post.

How to Enable and Use Office Sidebar in Microsoft Edge? - MiniToolHow to Enable and Use Office Sidebar in Microsoft Edge? - MiniToolMay 03, 2025 am 12:44 AM

To enhance the coordination between Microsoft Edge and other functional software, Microsoft Edge has issued a new feature – Office Sidebar – that provides more quick and easy channels to access your targets. This article on php.cn Website will tell y

Four Ways to Fix the Windows Update Error Code 0x800703edFour Ways to Fix the Windows Update Error Code 0x800703edMay 03, 2025 am 12:43 AM

Your Windows may go malfunction when you perform a Windows Update and that’s when you meet an error code. Since many people find the Windows update error code 0x800703ed, this article on php.cn Website will list some useful methods for you.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Atom editor mac version download

Atom editor mac version download

The most popular open source editor