Important Notice:

Way of Adding JavaScript

Way of Adding JavaScript

21 views 11 min read

Way of Adding JavaScript :-

वेब पेज में JavaScript को जोड़ने के तीन मुख्य तरीके हैं।

1- Internal JavaScript:-

इसमें <script> टैग का उपयोग करके कोड सीधे HTML फ़ाइल के अंदर लिखा जाता है।
आमतौर पर इसे <head> या <body> सेक्शन के अंत में रखा जाता है।

Syntax:-                

                             <!DOCTYPE html>

                              <html>

                              <head>

                                     <script>

                                                 function showMsg()

                                                 {

                                                 alert("Hello Internal JS");

                                                 }

                                      </script>

                            </head>

                             <body>

 

                                               <button onclick="showMsg()">Click me</button>

 

                               </body>

                               </html>

2- External JavaScript:-

JavaScript कोड एक अलग .js फ़ाइल में लिखा जाता है और HTML में <script src="फ़ाइल का पथ"द्वारा जोड़ा जाता है।
यह तरीका सबसे अच्छा है क्योंकि इसमें कोड दोबारा इस्तेमाल किया जा सकता है और HTML साफ़ रहता है।

Syntax:-     

              index.html:-                                                                    script.js:-

                                    <!DOCTYPE html>                                                             function showMessage() {

                                                              <html>                                                               alert("नमस्ते! यह बाहरी JavaScript है।");}

                                                          <head>
                                                            <title>
बाहरी JS</title>
                                                       <script src="script.js"></script>
                                                         </head>
                                                         <body>
                                                       <button onclick="showMessage()">
क्लिक करें</button>
                                                        </body>
                                                         </html>

    3- Inline JavaScript:-

इसमें कोड सीधा HTML एलिमेंट के अंदर onclick, onmouseover जैसे एट्रिब्यूट में लिखा जाता है।
नोट: यह तरीका केवल बहुत छोटे कामों के लिए प्रयोग करें, क्योंकि इससे HTML और JS मिक्स हो जाते हैं, जिससे कोड मेंटेन करना मुश्किल होता है।

   syntax:-   

                         <!DOCTYPE html>
                       <head>
                      <title>इनलाइन JavaScript उदाहरण</title>
                       </head>
                        <body>
                                   <button onclick="alert('नमस्ते! यह इनलाइन JS है।');">क्लिक करें</button>
                       </body>
                        </html>     

Ways of Adding JavaScript

There are three main ways to add JavaScript to a web page.

1. Internal JavaScript

The code is written directly inside the HTML file using the <script> tag. It is usually placed inside the <head> or at the end of the <body> section.

Syntax:-                

                             <!DOCTYPE html>

                              <html>

                              <head>

                                     <script>

                                                 function showMsg()

                                                 {

                                                 alert("Hello Internal JS");

                                                 }

                                      </script>

                            </head>

                             <body>

 

                                               <button onclick="showMsg()">Click me</button>

 

                               </body>

                               </html>

2. External JavaScript

JavaScript code is written in a separate .js file and linked to HTML using <script src="file-path">. This method is best because code is reusable and HTML remains clean.

Syntax:-     

              index.html:-                                                                    script.js:-

                                    <!DOCTYPE html>                                                             function showMessage() {

                                                              <html>                                                               alert("नमस्ते! यह बाहरी JavaScript है।");}

                                                          <head>
                                                            <title>
बाहरी JS</title>
                                                       <script src="script.js"></script>
                                                         </head>
                                                         <body>
                                                       <button onclick="showMessage()">
क्लिक करें</button>
                                                        </body>
                                                         </html>

3. Inline JavaScript

Code is written directly inside an HTML element using attributes like onclick, onmouseover, etc.
Note: Use this only for very small tasks because mixing HTML and JavaScript makes code hard to maintain.

syntax:-   

                         <!DOCTYPE html>
                       <head>
                      <title>इनलाइन JavaScript उदाहरण</title>
                       </head>
                        <body>
                                   <button onclick="alert('नमस्ते! यह इनलाइन JS है।');">क्लिक करें</button>
                       </body>
                        </html>   

Related Notes