JavaScript window Object In Hindi

📔 : Java Script 🔗

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 है।

JS window functions

window की कई properties और methods हैं , कुछ important methods इस प्रकार हैं -

  1. open()
  2. close()
  3. alert()
  4. confirm()
  5. prompt()
  6. setTimeout()

JS open function

window.open() method का use new window ओपन करने के लिए किया जाता है।

JS open Syntax

let mywindow = window.open(url, window_name, features);

Where
  •   url किसी file या website का path होता है जो कि browser द्वारा support किया जाता है , अगर path नहीं देते हैं तो new window blank page के साथ open होता है।
  • window_name , का use open होने वाले new window / tab / iframe का name set करने के लिए किया है, name में white space नहीं होना चाहिए। यह optional होता है।
  • और features में comma separated values देता है , जैसे width = 200 , height = 230 , etc . यह भी optional होता है।

JS open Example

File : window_object.html

Copy Fullscreen Close Fullscreen Run
<!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>

JS close function

का 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.

JS close() Syntax
window.close();

JS alert function

आपने कई जगह examples में alert() function का use करते देखा होगा , actually ये window object का ही method है।

window.alert() method , pass किये गए message और एक OK button के साथ एक alert dialog box display करता है।

JS alert Syntax
alert("Thanks ! for displaying me.");
     or
window.alert("Thanks ! for displaying me.");

JS Alert

? alert() का use सिर्फ user को inform करने के लिए किया जाता है , इसलिए इसे वहीँ use कर सकते हैं जहा पर user response की जरूरत नहीं है। इसके अलावा जब तक dialog box close नहीं हो जाता है , उससे आगे का code run नहीं होगा।

JS confirm function

window.confirm() का use , user से confirmation लेने के लिए किया जाता है। confirm() pass किये गए message और OK , Cancel buttons के साथ dialog box display करता है।

JS confirm Syntax
let res = confirm('Are you happy ?.');
JS Confirm

According तो user action , यह boolean value true या false ही return करता है , अगर OK par click किया तो true , otherwise false .

Copy Fullscreen Close Fullscreen
<script type="text/javascript">
if(confirm("Are you happy ?"))
document.write('You are happy');
else
document.write('You are not happy');
</script>

JS prompt function

window.prompt() function का use, user से input लेने के लिए किया जाता है। यह user को message व default value के साथ user को dialog box display करता है। अगर user value provide नहीं करता है तो null return करेगा।

JS prompt() syntax
let user_input = prompt('how old are you ?', 25);
JS Alert

See Example

Copy Fullscreen Close Fullscreen
<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 नहीं हो जाता है।

JS setTimeout function

setTimeout() किसी pass किये गए function को specified time के बाद run करता है , दिया जाने वाला time milliseconds में होता है।

1000 ms = 1 second

JS setTimeout() syntax
<script type="text/javascript">
setTimeout(function, delay, ...arguments_list);
</script>

Where

  • function एक pass किया जाने वाला callback function है। यह required है।
  • delay , milliseconds में pass किया जाने वाला time  है जो कि optional है। , इसी particular time के बाद ही callback function run होता है।
  • और arguments_list optional parameters हैं , आप कितने ही arguments pass कर सकते है , जिन्हे pass किये गए callback function के अंदर arguments Object या ...args की help से access / manage कर सकते हैं।

JS setTimeout Example

Copy Fullscreen Close Fullscreen Run
<!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 किया गया है।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers