Categories:
Partners

Reloading, closing, loading new content into a window using JavaScript

With JavaScript, we can write code that replaces the "reload" and "close" button of our browser.

To reload a window, use this method:

window.location.reload()

Here's an example:

To close a window, use this method:

window.close()

I was almost tempted to provide an example!

Ok, we can also load new content into a window using JavaScript-this is similar to the <a> tag in html, only now, using JavaScript, we have more control over this process.

The basic syntax when loading new content into a window is:

window.location="yoururl.com"

This is exactly the same as

<a href="yoururl.com></a> //if we use html

Lets provide an example, where a confirm box will allow users to choose between going to two places:

<script type="text/javascript">
<!--
function warp(){
var ok=confirm('Click "OK" to go to CSS Drive, "CANCEL" to go to CodingForums')
if (ok)
location="http://www.cssdrive.com"
else
location="http://www.codingforums.com"
}
//-->
</script>