I can try to get my Javascript output. if I write only 1 function I can, But if I would write 2 or more function, how I can get output of function which I want?
JS File:
var time = new Date();
var currenthour = time.getHours();
var welcome;
if(currenthour<=12)
welcome = 'Good Morning';
else if(currenthour >=12 && currenthour <= 17)
welcome = 'Good Afternoon';
else if(currenthour >= 17 && currenthour <= 24)
welcome = 'Good Evening';
document.write(welcome);
HTML File
<!DOCTYPE html>
<html>
<head>
<title>WinMachines</title>
<link rel = "shortcut icon" type= "image/png" href="images\logo.png">
<link rel = "stylesheet" type ="text/css" href= "css/style.css">
</head>
<body background="images/lg.jpg">
<h2></h2>
<script src="js/main.js"></script>
</body>
</html>
If you have multiple functions in your JS file and you want to call a specific one you should be able to do so by calling that function out in another set of <script>
tags:
<script src="js/main.js" type="text/javascript"></script>
<script>
function_1();
</script>
<script>
function_2();
</script>
etc. I've done this before and it has worked for me.
I can try to get my Javascript output. if I write only 1 function I can, But if I would write 2 or more function, how I can get output of function which I want?
JS File:
var time = new Date();
var currenthour = time.getHours();
var welcome;
if(currenthour<=12)
welcome = 'Good Morning';
else if(currenthour >=12 && currenthour <= 17)
welcome = 'Good Afternoon';
else if(currenthour >= 17 && currenthour <= 24)
welcome = 'Good Evening';
document.write(welcome);
HTML File
<!DOCTYPE html>
<html>
<head>
<title>WinMachines</title>
<link rel = "shortcut icon" type= "image/png" href="images\logo.png">
<link rel = "stylesheet" type ="text/css" href= "css/style.css">
</head>
<body background="images/lg.jpg">
<h2></h2>
<script src="js/main.js"></script>
</body>
</html>
Don't use document.write
for modifying existing elements. Instead use DOM element selector methods like document.getElementById
(if the element has a unique id
), or the more modern document.querySelector(cssSelectorString)
which will return the first match in the document
.
const time = new Date(); const currenthour = time.getHours(); let welcome; if (currenthour <= 12) welcome = 'Good Morning'; else if (currenthour >= 12 && currenthour <= 17) welcome = 'Good Afternoon'; else if (currenthour >= 17 && currenthour <= 24) welcome = 'Good Evening'; document.querySelector('h2').textContent = welcome;
<h2></h2>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.