Helping ordinary people create extraordinary websites!
GET OUR NEWSLETTER
Your Email:
 

JavaScript Popup Windows

Often you will want to show some text to the user in a popup window, ask them to confirm an action, or even ask them a question. JavaScript provides a few useful functions for doing so.

JavaScript Alert

An alert is the simplest popup, simply showing a message to the user and typically an "OK" button.

alert('Message goes here.');

JavaScript Confirm

If you want your user to confirm an action, use the confirm() function:

var intent = confirm('Do you really want to delete this file?');

The function takes one parameter, the message to be displayed, and returns true if the user clicked "OK"/"Yes", false otherwise.

JavaScript Prompt

Occasionally you will want to request input from the user. The prompt() function allows the user to enter some brief text into a popup box. This could be used, for example, to ask the user their name.

var name = prompt('What is your name?');

The function works similarly to confirm(), returning the user's input. The return value will be a string.

Web page popup

You can also open a popup window to an entirely seperate web page. This can be achieved via window.open().

window.open('http://www.yahoo.com/');

The window.open() function takes one parameter, the URL of the page to open to. It will usually open an entirely new window, but sometimes it will open a new tab (browser permitting).

You can also tie this to an onclick event (or other event) within your HTML:

<script type="text/javascript">
function open_popup() {
window.open('http://www.yahoo.com/');
}
</script>
<a href="#" onclick="javascript:open_popup()">Click me!</a>

When the "Click me!" link is clicked on, a popup window will open.

« JavaScript getElementById JavaScript Redirect »

More JavaScript Tutorials:
» 9 Javascripts You Better Not Miss!
» Redirecting URL
» Browser-Correct Download Instructions
» Top 10 Custom JavaScript Functions Of All Time
» Submit Forms Conditionally using JavaScript
» Dynamic External JavaScript Files
Advertise with Us!


Learn JavaScript Tutorials Scripts