Script Tag & Functions in JavaScript in Hindi – JavaScript में Script Tag और Functions

Script Tag & Functions in JavaScript in Hindi – आज हम JavaScipt में Script Tag & Functions के बारे में जानेगें, JavaScript में script tag का प्रयोग कैसे कर सकते हैं ? चलिए देखते हैं

JavaScript का code हमेशा <script> और </script>के बीच में लिखा जाता है।

<script>
document.getElementById("demo").innerHTML = "My First JavaScript Program";
</script>
JavaScript में  <script>और  </script>का प्रयोग निम्न उदाहरण से समझ सकते हैं –
<!DOCTYPE html>
<html>
<head>
<style>
body {
color:black
}
h1 {
background-color:black;
color:white;
}
</style>
</head>
<body>
<h1 style="text-decoration: underline; text-align:left;">www.aforapple.co.in</h1>
<h2>Body of the JavaScript Program</h2>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = "My First JavaScript Program";
</script>
</body>
</html>
उक्त program को run करने पर निम्न output प्राप्त होता है-
JavaScript in Hindi
JavaScript में Functions और Events  का प्रयोग कैसे कर सकते हैं ? चलिए देखते हैं – JavaScript में  Functionएक प्रकार का JavaScript  का ही code block होता है जो कि किसी event के occur होने पर run होता है।  जैसे कि यदि कोई button यूजर द्वारा click  किआ जाता है तो उस onClick event से सम्बंदित एक निश्चित code  execute होगा।  आगे हम इसके बारे में विस्तार से जानेगे। JavaScript में <head>और <body>टैग का प्रयोग – एक HTML document  में हम चाहे जितनी scripts लिक्घ सकते हैं।  ये scripts या तो <head>या  <body> tag  में लिखी जाती हैं। निम्न उदाहरण में JavaScript के एक फंक्शन को HTML के <head> tag  में लिखा गया है, जो कि तब call होता है तक button  को click  किआ जाता है।
<!DOCTYPE html>
<html>
<head>
<style>
body {
color:black
}
h1 {
background-color:black;
color:white;
}
</style>
<script>
function myFunction() {
  document.getElementById("demo").innerHTML = "Text after calling the function.";
}
</script>
</head>
<body>
<h1 style="text-decoration: underline; text-align:left;">www.aforapple.co.in</h1>

<h2>Demo Text in Head</h2>

<p id="demo">Text in current Paragraph </p>
<button type="button"onclick="myFunction()">Try it</button>
</body>
</html>
उक्त program को run करने पर निम्न output प्राप्त होता है-
“Try it” बटन पर click करने के बाद निम्न output  आता है –
निम्न उदाहरण में JavaScript के एक फंक्शन को HTML के <body> tag  में लिखा गया है, जो कि तब call होता है जब button  को click  किआ जाता है।
<!DOCTYPE html>
<html>
<head>
<style>
body {
color:black
}
h1 {
background-color:black;
color:white;
}
</style>
</head>
<body>
<h1 style="text-decoration: underline; text-align:left;">www.aforapple.co.in</h1>

<h2>Demo Text in Body</h2>

<p id="demo">Text in Current Paragraph</p>

<button type="button"onclick="myFunction()">Try it</button>

<script>
function myFunction() {
  document.getElementById("demo").innerHTML = "Text after calling the function.";
}
</script>

</body>
</html>
JavaScript का किसी external file  में External JavaScript के रूप में प्रयोग- जब एक समान code बार-बार कई web pages में काम आता है तो आप JavaScript को किसी दूसरी फाइल में लिखकर उसे .js file extension के साथ save करके प्रयोग कर सकते है।  इस External script का प्रयोग करने के लिए आपको <script> tag के  src (source) attribute में External script file का नाम लिखना होता है। उदाहरण के लिए आप निम्न file को myExternalScript.js  नाम से save करे तथा उसमे निम्न code लिखे
Function myFunction() {
  document.getElementById("demo").innerHTML = "Text after calling the external JavaScript file.";
}
अब उसी folder में निम्न code को लिखकर एक दूसरी file बनाये तथा उसे linking.html नाम से save करें
<!DOCTYPE html>
<html>
<head>
<style>
body {
color:black
}
h1 {
background-color:black;
color:white;
}
</style>
</head>
<body>
<h1 style="text-decoration: underline; text-align:left;">www.aforapple.co.in</h1>
<h2>Demo External JavaScript</h2>
<p id="demo">Demo Text. </p>
<button type="button" onclick="myFunction()">Try it</button>
<p>Example showing the linking of external JavaScript file "myExternalScript.js".</p>
<p>(myFunction is stored in "myExternalScript.js")</p>
<script src="myExternalScript.js"></script>
</body>
</html>
उक्त linking.html file को run करने पर निम्न output प्राप्त होता है-
“Try it” बटन पर click करने के बाद निम्न output  आता है –
इस पोस्ट के पहले मैंने और भी कई पोस्ट लिखी हैं, जैसे की HTML , Microsoft Excel , Gmail , JavaScript आदि, आप अपने इच्छा के अनुसार इन्हे देख सकते है। Script Tag & Functions in JavaScript in Hindi
error: Content is protected !!