If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
Window Object, Current Browser window represent करता है , window object browser द्वारा automatically create किया जाता है।
Tabbed Window (जब कई सारे Tabs एक ही window में Open हों ) में, प्रत्येक Opened Tab , इसके अपने window object द्वारा represent किया जाता है , इसलिए Run होने वाला JavaScript Code हमेशा उसी window को represent करता है , जिस Tab में उसे Run किया जा रहा है। लेकिन कुछ properties और methods ( like : resizeTo() and innerHeight ) overall उसी window पर apply होंगे जिस window में सभी Tabs मौजूद है।
window object का scope global होता है , means इस object की सभी properties और methods को आप without window के भी access कर सकते हैं।
? window Browser का object होता है न कि JavaScript का , String , Array , Math etc . JavaScript Objects है।
window की कई properties और methods हैं , कुछ important methods इस प्रकार हैं -
window.open() method का use new window ओपन करने के लिए किया जाता है।
let mywindow = window.open(url, window_name, features);
File : window_object.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript window.open() Object</title>
</head>
<body>
<script type="text/javascript">
let mywindow = window.open("https://www.learnhindituts.com", "My window", "width=400, height=300");
</script>
</body>
</htmll>
का use current window को close करने के लिए किया जाता है।
❕ Important
Note that , window.close() function का use करके सिर्फ उसी window को close किया जा सकता है , जिसे window.open() function का use करके open किया गया है। Otherwise console में warning generate होती है - Scripts may not close windows that were not opened by script.
window.close();
आपने कई जगह examples में alert() function का use करते देखा होगा , actually ये window object का ही method है।
window.alert() method , pass किये गए message और एक OK button के साथ एक alert dialog box display करता है।
alert("Thanks ! for displaying me."); or window.alert("Thanks ! for displaying me.");
? alert() का use सिर्फ user को inform करने के लिए किया जाता है , इसलिए इसे वहीँ use कर सकते हैं जहा पर user response की जरूरत नहीं है। इसके अलावा जब तक dialog box close नहीं हो जाता है , उससे आगे का code run नहीं होगा।
window.confirm() का use , user से confirmation लेने के लिए किया जाता है। confirm() pass किये गए message और OK , Cancel buttons के साथ dialog box display करता है।
let res = confirm('Are you happy ?.');
According तो user action , यह boolean value true या false ही return करता है , अगर OK par click किया तो true , otherwise false .
<script type="text/javascript">
if(confirm("Are you happy ?"))
document.write('You are happy');
else
document.write('You are not happy');
</script>
window.prompt() function का use, user से input लेने के लिए किया जाता है। यह user को message व default value के साथ user को dialog box display करता है। अगर user value provide नहीं करता है तो null return करेगा।
let user_input = prompt('how old are you ?', 25);
See Example
<script type="text/javascript">
let input_age = prompt('how old are you ?', 25);
document.write(`You are ${input_age} years old.`);
</script>
alert() , confirm(), prompt() ये तीनो methods जब dialog box को display करते हैं , तो rest of code तब तक run नहीं होता जब तक dialog box close नहीं हो जाता है।
setTimeout() किसी pass किये गए function को specified time के बाद run करता है , दिया जाने वाला time milliseconds में होता है।
1000 ms = 1 second
<script type="text/javascript">
setTimeout(function, delay, ...arguments_list);
</script>
Where
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript window Methods Example</title>
</head>
<body>
<script type="text/javascript">
let my_window = window.open("https://www.learnhindituts.com", "My window", "width=400, height=300");
setTimeout(function()
{
/*
here you can access optional parameter using [arguments object] or
using ...args (triple dots called variable length arguemnts function)
*/
my_window.close();
}, 1000 * 10); /* close the window after 10 seconds*/
</script>
</body>
</html>
Example में window.open() function की help से एक नयी window open की गयी है जिसे 10 seconds के बाद close किया गया है।
Hi ! My name is Rahul Kumar Rajput. I'm a back end web developer and founder of learnhindituts.com. I live in Uttar Pradesh (UP), India and I love to talk about programming as well as writing technical tutorials and tips that can help to others.
Get connected with me. :) LinkedIn Twitter Instagram Facebook