Javascript Course

The window object is created for each window that appears on the screen. A window can be the main browser window, an iframe, or even a new window created with JavaScript (pop-up).
The properties and methods of the window object gives you some control over browser windows.
- A list with properties and methods of the window object, with code and examples it is to the page:

Some examples

The parent property refers to the parent of the current window (if that content is into an <iframe>).
<h4>Example parent</h4>
<p>Click on the button to change the background color of the parent window of this iframe.</p>
<button id='btn1'>Parent background</button>

document.getElementById('btn1').addEventListener('click', (ev)=>{ ='#bfbffe';
The self property refers to the current window.
- With this property you can check if the content is in <iframe> or in the main window.
<h4>Example self</h4>
<p>Click on the button to see if this content is into an iframe or in the main window.</p>
<button id='btn1'>Check window</button>
<blockquote id='resp'>#resp</blockquote>

document.getElementById('btn1').addEventListener('click', (ev)=>{
 if( != window.self) var msg ='This content is in iframe.';
 else var msg ='This content is in the main window.';
 document.getElementById('resp').innerHTML = msg;
The top property refers to the main window in browser.
<h4>Example top</h4>
<p>When click on the button, the background color of the main (top) window is changed.</p>
<button id='btn1'>Top background</button>

document.getElementById('btn1').addEventListener('click', (ev)=>{ ='#c0c0fe';
outerHeight - returns the height of the outside of the browser window (including toolbar), in pixels.
<h4>Example outerHeight</h4>
<p>Click on the button to see the height of this window.</p>
<button id='btn1'>Get outerHeight</button>
<blockquote id='resp'>#resp</blockquote>

document.getElementById('btn1').addEventListener('click', (ev)=>{
 document.getElementById('resp').innerHTML ='outerHeight: '+ window.outerHeight;
outerWidth - returns the width of the outside of the browser window (in pixels).
<h4>Example outerWidth</h4>
<p>Click on the button to see the width of this window.</p>
<button id='btn1'>Get outerWidth</button>
<blockquote id='resp'>#resp</blockquote>

document.getElementById('btn1').addEventListener('click', (ev)=>{
 document.getElementById('resp').innerHTML ='outerWidth: '+ window.outerWidth;

Pop-Up window

Pop-up windows can be created from JavaScript with specified dimensions and positions on screen.
Use this syntax:'URL', 'name', 'props')
- 'URL' - address for page content in the window. If a blank string is added, it will display an empty page.
- 'name' - the name of the pop-up window.
- 'props' - (optional) nume=valoare pairs (separated by comma) with properties for defining the window. If the 'props' is not added, the window will open as a new tab in the browser.

- Example, clicking on a button it opens a pop-up window with specified size and position.
<p>When click on the button it opens a pop-up window with these properties: <em>'width=650, height=450, left=30, top=50, menubar=0, titlebar=0'</em>.</p>
<button id='btn1'>Open pop-up</button>

document.getElementById('btn1').addEventListener('click', (ev)=>{
 var popup ='//', 'TestWin', 'width=650, height=450, left=30, top=50, menubar=0, titlebar=0');

Pop-Up window with content from javascript

The method returns a reference to the opened window. Using this reference, you can open pop-up window with content created directly from JavaScript. In this case, the argument for 'URL' is an empty string.
In the pop-up window you can add a button that can close that window.

- Example, a button to open a pop-up window with content and Close button created from JavaScript.
<h4>Example, window with content from JS and Close button</h4>
<p>When you click on the button, it opens a window with HTML elements and Close button added from JavaScript.</p>
<button id='btn1'>Open pop-up</button>

document.getElementById('btn1').addEventListener('click', (ev)=>{
//opens pop-up
var popup ='', 'TestWindow', 'width=350, height=300, left=20, top=25');

//adds html and text in pop-up
popup.document.write('<h1>Be Happy, Smile</h1>');
popup.document.write(`<blockquote style="color:#0000e0; font-size:18px;">Your most precious wealth, no one can take it from you,<br>
You can give and share without decreasing.<br>
You are always, yourself, your state of being.<br>
Give joy and peace through your state,<br>
What and how you give, you receive.</blockquote>
<h4>Share Peace and Joy, be Happy!</h4>`);
popup.document.write('<p> - I\'m not telling you what to do, the world reflects you, You transmit this to you.</p>');

//adds close button
popup.document.write('<button onclick="window.close()">Close</button>');

//sets a background to the body in pop-up ='#fefee9';

Scroll bars

The scroll bars of the page belong to the window object. It contains properties and methods for reading and setting the position of the page scroll bars.

• To detect when the page is scrolled, you can register the 'scroll' to the window object.

- Example, button for scroll with scrollTo(), and displays the coordinates of the scroll bars.
<h4>Example scrollTo()</h4>
<p style='background:#fdfddb; height:1500px; width:130%;'>When you click the button, the horizontal scroll bar moves to position 300, and the vertical bar scrolls down to position 1100 (pixels).<br>
The coordinates of the scroll bars are displayed in the button (with the 'scroll' event).<br>
 - At the bottom is a scroll-top button.</p>
<button id='btn1' style='position:fixed;left:20%; top:170px;'>Scroll</button>
<button onclick='window.scrollTo(0,0)' style='display:block; margin:5px 25px 8px auto;'>Go-Top</button>

var btn = document.getElementById('btn1');

btn.addEventListener('click', (ev)=>{
 window.scrollTo(300, 1100);

//detecs the scroll event
window.addEventListener('scroll', (ev)=>{
 //displays in btn the scroll bars coordinates
 btn.textContent = window.scrollX +', '+ window.scrollY;

Saving data in browser

With the localStorage property you can store data from JavaScript in the user's browser for unlimited time, until they are deleted by the user or other script.
Datas stored with localStorage are saved as string, and can be accessed on the website from which they were added, each time the user visits that site.
- This property returns an object with methods to add and reads data stored in the browser.

- Example, a button to save in web browser data added into an input field, and another button to display data stored in localStorage.
<h4>Example localStorage</h4>
<p>When click on the button, the text from the input field is saved in browser.<br>
Then, click on the next button to display the text saved in localStorage.</p>
<div id='dv1'>
Text: <input type='text' value='' id='txt1'><br>
<button id='btn1'>Add in storage</button>
<div id='dv2' style='display:none'>
<button id='btn2'>Get from storage</button>
<blockquote id='resp'>#resp</blockquote>

//stores in browser the value from #txt1, with key 'some_key'
document.getElementById('btn1').addEventListener('click', (ev)=>{
 var str = document.getElementById('txt1').value;
 localStorage.setItem('some_key', str);

 //display the div with the next button
 document.getElementById('dv1').style.display ='none';
 document.getElementById('dv2').style.display ='block';

//adds in #resp the text saved in localStorage, 'some_key'
document.getElementById('btn2').addEventListener('click', (ev)=>{
 document.getElementById('resp').textContent = localStorage.getItem('some_key');

Daily Test with Code Example

Which tag adds a new line into a paragraph?
<b> <br> <p>
First line ...<br>
Other line...
Which CSS property can be used to add space between letters?
text-size word-spacing letter-spacing
#id {
  letter-spacing: 2px;
What JavaScript function can be used to get access to HTML element with a specified ID?
getElementById() getElementsByTagName() createElement()
var elm = document.getElementById("theID");
var content = elm.innerHTML;
Click on the "echo" correct instruction.
echo "" echo ""; echo """;
echo "Address URL:";
Window Object

Last accessed pages

  1. Simple arithmetic calculator in PHP (2104)
  2. querySelector and querySelectorAll (30064)
  3. array_map and array_search in JavaScript (4059)
  4. ActionScript 3 - Change MovieClip Color (8965)
  5. Display data from PHP Array, or MySQL in HTML table (26805)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (326)
  2. PHP Unzipper - Extract Zip, Rar Archives (86)
  3. Read Excel file data in PHP - PhpExcelReader (82)
  4. The Mastery of Love (78)
  5. PHP-MySQL free course, online tutorials PHP MySQL code (65)