ការបង្ហាញ Output នៅក្នុង JavaScript



បន្ទាប់ពីយើងបានសរសេរ Script រួចរាល់ហើយនៅពេលដែលចង់ដំណើរការគឺយើងត្រូវបើក Page នោះនៅក្នុង Web Browser ប៉ុន្តែ JavaScript មិនមានភ្ជាប់មកជាមួយនឹងការបង្ហាញលទ្ធផលដែលយើងបានសរសេរនោះទេ យើងអាចបង្ហាញព័ត៌មានទាំងនោះបានតាមវិធីផ្សេងៗដូចខាងក្រោម៖  

  • window.alert() ឬ អាចសរសេរ alert()
  • document.write() 
  • document.getElementById().innerHTML 
  • console.log()

ការប្រើ alert()

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

<html> 

    <head> 

        <title>Testing my alert</title> 

    </head> 

    <body> 

        <script>

            window.alert("Text for alert full form");

            alert("You can use alert short form");

        </script> 

    </body> 

</html>

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

ការប្រើ document.write()

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

<html> 

    <head> 

        <title>Testing my document.write</title> 

    </head> 

    <body> 

        <script>

            documen.write("<h1>My first JavaScript.</h1>");

            documen.write("<p>JavaScript is a versatile, high-level programming language commonly used to create interactive and dynamic elements on websites. It allows developers to build functionalities like animations, form validations, interactive maps, and more, making web pages more engaging and user-friendly.</p>");

        </script> 

    </body> 

</html>

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

ការប្រើ document.getElementById().innerHTML

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

<html> 

    <head> 

        <title>Testing my document.getElementById().innerHTML</title>

    </head> 

    <body>

        <div id="result"></div> 

        <script>

            documen.getElementByID("result").innerHTML = "The document.getElementById() method in JavaScript is used to select an HTML element by its id attribute. Once selected, you can manipulate or interact with that element, like changing its content, style, or triggering an action.";

        </script> 

    </body> 

</html>

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

ការប្រើ console.log()

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

<html> 

    <head> 

        <title>Testing my console.log</title>

    </head> 

    <body>

        <script>

            console.log(127 * 2.33);

        </script> 

    </body> 

</html>

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

ចំណាំ៖ ដើម្បីមើលលទ្ធផលដែលប្រើប្រាស់​ Console ត្រូវចូលទៅកាន់ Console របស់ Web Browser តាមរយៈ Right Click on Page --> Inspect ឬ Shortcut Key F12 --> Console ។

គេក៏ប្រើ Cosole ដើម្បីស្វែងរកកូដ Error ផងដែរ។




Comments

Popular posts from this blog

ការសរសេរ JavaScript ចូលក្នុង Web Page

Create Pop up Form in JavaScript

ការប្រើ Switch ក្នុង JavaScript