- there are many ways to write javascript one is inline:
<button onclick="alert('Hi')">click me</button>click me
- another way is internal:
<button id="mybutton">click me</button>
<script type="text/javascript">
document.getElementById("mybutton").onclick = function(){
alert('Hi');
}
</script>
//another example
<p id="text">some text</p>
<script type="text/javascript">
alert(document.getElementById("text").innerHTML);
</script>in this method it is prefer to put the script at the end of the body or in head section.
- the third way is to use external file:
<script src="test.js"></script>- to write a comment:
//this is a comment
/*
this is a multiline comment
*/- to create variable:
var name = "ahmed";- to add element t"o the page:
document.write("hello")- to get the device pixel retio in chrome console:
window.devicePixelRatio- example to show date and time on click button
<button type="button" onclick="document.getElementById('demo').innerHTML = Data()">
click here to show data and time
<button>
<p id="demo"></p>