Members

Note: You can use your Sitellite.org account here and vice versa.

Username

Password

Remember Login

Forgot your password?

Not a member? Click here to register

Javascript Prompt Alert

Home Page | All Pages | Recently Revised | Authors | Feeds |

Sitellite features powerful drop-in replacements for <span class="highlighted">the</span> browser's built-in <span class="highlighted"><span class="highlighted">prompt</span></span>() and alert() <span class="highlighted">Javascript</span> functions. These replacements offer a few more features, but mainly serve <span class="highlighted">to</span> add polish and style <span class="highlighted">to</span> your web-based applications.

Traditionally, <span class="highlighted"><span class="highlighted">prompt</span></span>() was an easy way <span class="highlighted">of</span> getting a bit <span class="highlighted">of</span> information out <span class="highlighted">of</span> a user, and alert() was an easy way <span class="highlighted">of</span> conveying a bit <span class="highlighted">of</span> information <span class="highlighted">to</span> them, such as a data input error.

<span class="highlighted">The</span> problem with them is that their visual output doesn't feel like part <span class="highlighted">of</span> your website, and can even be confusing <span class="highlighted">to</span> users because <span class="highlighted">of</span> this (a <span class="highlighted"><span class="highlighted">prompt</span></span> dialogue in Firefox keeps <span class="highlighted">the</span> page loading graphic spinning until it's been closed, and I know <span class="highlighted">of</span> at least one user who has asked me if that means something's wrong).

So we went about replacing these in recent versions <span class="highlighted">of</span> Sitellite, with <span class="highlighted">the</span> latest being a totally integrated experience in 4.2.12 with <span class="highlighted">the</span> popups occurring as floating divs complete with animation as they open and close (courtesy <span class="highlighted">of</span> Script.aculo.us).

Inclusion in Your Script

<span class="highlighted">To</span> include <span class="highlighted">the</span> replacements, simply call this somewhere in your PHP code:


<?php
loader_import ('saf.GUI.<span class="highlighted"><span class="highlighted">Prompt</span></span>');
?>

You can also call that in a .spt file via:

{exec loader_import ('saf.GUI.<span class="highlighted"><span class="highlighted">Prompt</span></span>')}

This package automatically includes <span class="highlighted">the</span> necessary <span class="highlighted">Javascript</span> and CSS files from <span class="highlighted">the</span> /js/ folder.

Alerts

At their simplest, coding an alert is identical <span class="highlighted">to</span> <span class="highlighted">how</span> one was made before:

alert ('This is an alert box');

This will cause an alert box <span class="highlighted">to</span> appear in <span class="highlighted">the</span> middle <span class="highlighted">of</span> <span class="highlighted">the</span> current browser window for 3 seconds and then fade away. There's one new feature for alerts with more text or more important messages though, which is that you can specify an extended length <span class="highlighted">of</span> time for them <span class="highlighted">to</span> appear, in seconds:

alert ('This is an important notice', 10);

This alert box will appear for 10 seconds then fade away. Users can also dismiss an alert earlier than that simply by clicking on <span class="highlighted">the</span> window.

So a complete alert example you can include into your .spt files would be:

{exec loader_import ('saf.GUI.<span class="highlighted"><span class="highlighted">Prompt</span></span>')}

<a href="#" onclick="return alert ('This is an important notice', 10)">Click me!</a>

Basic <span class="highlighted"><span class="highlighted">Prompt</span></span>

Traditionally, a <span class="highlighted"><span class="highlighted">prompt</span></span>() call worked like this:

function say_hello () {
    name = <span class="highlighted"><span class="highlighted">prompt</span></span> ('Please enter your name:');
    if (name == false || name == null || name.length == 0) {
        return false;
    }
    alert ('Hello ' + name);
    return false;
}

With <span class="highlighted">the</span> <span class="highlighted"><span class="highlighted">prompt</span></span> replacement, this changes <span class="highlighted">to</span> become:

function say_hello () {
    <span class="highlighted"><span class="highlighted">prompt</span></span> (
        'Please enter your name:',
        '',
        function (name) {
            if (name == false || name == null || name.length == 0) {
                return false;
            }
            alert ('Hello ' + name);
        }
    );
    return false;
}

<span class="highlighted">The</span> differences are as follows:

  • <span class="highlighted">The</span> second parameter is a default <span class="highlighted">value</span> (empty in <span class="highlighted">the</span> above case)
  • <span class="highlighted">The</span> third parameter is an abstract function which is called with <span class="highlighted">the</span> <span class="highlighted"><span class="highlighted">prompt</span></span>'s return <span class="highlighted">value</span>

Advanced <span class="highlighted"><span class="highlighted">Prompt</span></span>

Sitellite's <span class="highlighted"><span class="highlighted">prompt</span></span>() replacement has <span class="highlighted">the</span> ability <span class="highlighted">to</span> create <span class="highlighted"><span class="highlighted">prompt</span></span> windows with select boxes and textareas in addition <span class="highlighted">to</span> <span class="highlighted">the</span> usual single-line text input fields. <span class="highlighted">The</span> Xed editor uses this, for example, for its HTML source view mode.

This is done by creating a Prompter object and setting properties on it, then calling its open() method. <span class="highlighted">The</span> replacement <span class="highlighted"><span class="highlighted">prompt</span></span>() function is actually simply a function that calls this object behind <span class="highlighted">the</span> scenes.

In <span class="highlighted">the</span> example below, we create a Prompter object named "p" and we pass it <span class="highlighted">the</span> function that will handle <span class="highlighted">the</span> return <span class="highlighted">value</span> <span class="highlighted">of</span> <span class="highlighted">the</span> prompting. Next, we set <span class="highlighted">the</span> <span class="highlighted"><span class="highlighted">prompt</span></span> type <span class="highlighted">to</span> "select" and set <span class="highlighted">the</span> options we'd like <span class="highlighted">the</span> user <span class="highlighted">to</span> have. In this example we also change <span class="highlighted">the</span> width and height <span class="highlighted">of</span> <span class="highlighted">the</span> <span class="highlighted"><span class="highlighted">prompt</span></span> window, which is usually not necessary unless you have a large textarea <span class="highlighted"><span class="highlighted">prompt</span></span>. Lastly, we call <span class="highlighted">the</span> open() method and pass it <span class="highlighted">the</span> label for <span class="highlighted">the</span> window.

<script language="<span class="highlighted">javascript</span>" src="/js/<span class="highlighted"><span class="highlighted">prompt</span></span>.js"> </script>
<script language="<span class="highlighted">javascript</span>">

function birthday () {
    p = new Prompter (function (choice) {
        if (choice == 'Yes') {
            alert ('Happy Birthday!');
        }
    });

    p.type = 'select';
    p.options = ['No', 'Yes'];
    p.width = 300;
    p.height = 150;

    p.open ('Is it your birthday today?');

    return false;
}

</script>

As you can see from these examples, our <span class="highlighted"><span class="highlighted">prompt</span></span>() replacement is a flexible and elegant alternative <span class="highlighted">to</span> <span class="highlighted">the</span> default <span class="highlighted"><span class="highlighted">prompt</span></span>() function provided by web browsers.

Style Changes

<span class="highlighted">To</span> override <span class="highlighted">the</span> style <span class="highlighted">of</span> <span class="highlighted">the</span> <span class="highlighted"><span class="highlighted">prompt</span></span> or alert boxes, open <span class="highlighted">the</span> file js/<span class="highlighted"><span class="highlighted">prompt</span></span>.css and copy <span class="highlighted">the</span> styles there into your own CSS (ie. in your template set). When you make adjustments, add !important <span class="highlighted">to</span> <span class="highlighted">the</span> end <span class="highlighted">of</span> those lines, like this:

background: #cde !important;

That's all there is <span class="highlighted">to</span> it..

Revision from August 1, 2007 1:35 PM by anonymous

Forward in time (1 more) | Back in time (2 more) | See current | See changes | Linked from: Libraries