Methods of window n document object
Methods of window n document object
Method Description
confirm() displays the confirm dialog box containing message with ok and
cancel button.
1. <script type="text/javascript">
2. function msg(){
3. alert("Hello TRIDENT");
4. }
5. </script>
6. <input type="button" value="click" onclick="msg()"/>
It displays the confirm dialog box. It has message with ok and cancel buttons.
1. <script type="text/javascript">
2. function msg(){
3. var v= confirm("Are u sure?");
4. if(v==true){
5. alert("U ve clicked on ok");
6. }
7. else{
8. alert("U ve clicked on cancel");
9. }
10. }
11. </script>
It displays prompt dialog box for input. It has message and textfield.
1. <script type="text/javascript">
2. function msg(){
3. var v= prompt("What is ur name?");
4. alert("I am "+v);
5. }
6. </script>
1. <script type="text/javascript">
2. function msg(){
3. open("http://www.google.com");
4. }
5. </script>
6. <input type="button" value="GOOGLE" onclick="msg()"/>
1. <script type="text/javascript">
2. function msg(){
3. var b=open("","my win","width=20,height=15");
4. b.document.write("my window");
5. }
6. </script>
1. <script type="text/javascript">
2. var b;
3. function msg(){
4. b=open("","my win","width=20,height=15");
5. b.document.write("my window");
6. }
7. function closewin(){
8. b.close();
9. }
10. </script>
11. <input type="button" value="create window" onclick="msg();">
setTimeout Example
<head>
<title>
</title>
</head>
<body>
Press me
</button>
<script>
function tat() {
alert('Welcome to TRIDENT');
function tact()
{
setTimeout(tat,2000)
</script>
</body>
</html>
setInterval Example
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Timing</h2>
<p id="demo"></p>
<script>
setInterval(myTimer, 1000);
function myTimer() {
document.getElementById("demo").innerHTML = d.toLocaleTimeString();
}
</script>
</body>
</html>
The getElementById() is a method used to return the element that has the ID
attribute with the specified value.
function tact()
myElement.style.color = "red";
document.getElementById("demo").style.color = "red";
Example of getElementsByName()
<!DOCTYPE html>
<html>
<head>
<body>
<script>
document.write(temp[0].innerHTML);
document.write(temp[1]);
document.write(temp[2]);
</script>
</body>
</html>
The innerHTML property can be used to write the dynamic html on the html
document.
Example of getElementsByTagName()
<!DOCTYPE html>
<html>
<script>
function t()
</script>
<body>
<p>This is a paragraph.</p>
<p>This is a paragraph.</p>
<p>This is a paragraph.</p>
</body>
</html>
EXAMPLE 2
<!DOCTYPE html>
<html>
<head>
<style>
div {
margin: 5px;
</style>
</head>
<body>
<div id="myDIV">
</div>
<p>Click the button to add a background color to all p elements inside the
div element.</p>
<script>
function myFunction() {
var x = document.getElementById("myDIV");
var y = x.getElementsByTagName("P");
var i;
y[i].style.backgroundColor = "red";
</script>
</body>
</html>