Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Implementing a Personalized New Year Greeting Webpage with JavaScript and Flash

Tech 3

The webpage is a dynamic New Year's greeting card that personalizes a message by extracting a username from the URL query string. The core funcsionality is handled by client-side JavaScript, with embedded Flash content for visual effects.

Username Handling and Form Validation

The page first attempts to retrieve a username from the URL. If none is provided, a default name is used.

let extractedName = window.location.search.split('=')[1];
if (!extractedName) {
    extractedName = "Crystal Angel";
} else {
    extractedName = decodeURIComponent(extractedName);
}

A form validation function ensures the user input is appropriate before generating a shareable link.

function validateForm(formElement) {
    const nameField = formElement.userName;
    if (nameField.value.length > 28) {
        alert('Name is too long!');
        return false;
    }
    if (nameField.value.length === 0) {
        alert('Please enter your name.');
        return false;
    }

    const baseUrl = window.location.protocol + '//' + window.location.host + window.location.pathname;
    const shareableUrl = baseUrl + '?user=' + encodeURIComponent(nameField.value);
    const shareMessage = nameField.value + ' sends New Year wishes: ' + shareableUrl;

    // Attempt to copy to clipboard (older IE method)
    if (window.clipboardData) {
        window.clipboardData.setData('Text', shareMessage);
        alert('Link copied! Paste (Ctrl+V) into chat or email.');
    } else {
        alert('Share this link: ' + shareableUrl);
    }
    window.location.href = shareableUrl;
    return false;
}

Dynamic Content Rendering

The username is dynamically written into the page title and a prominent greeting.

<script>
    document.write('<h1 id="greeting">' + extractedName + ' Wishes You a Happy New Year!</h1>');
</script>

A VBScript block (for older IE compatibility) creates a color-changing effect for the greeting text.

<script language="vbscript">
Dim colorIndex
colorIndex = 1
Sub changeColor()
    window.clearTimeout(colorTimer)
    Select Case colorIndex
        Case 1
            greeting.style.color = "red"
            colorIndex = 2
        Case 2
            greeting.style.color = "blue"
            colorIndex = 3
        Case 3
            greeting.style.color = "yellow"
            colorIndex = 1
    End Select
    colorTimer = window.setTimeout("changeColor()", 200)
End Sub

Sub window_onload()
    colorTimer = window.setTimeout("changeColor()", 100)
End Sub
</script>

Embedded Flash Content

Multiple Flash objects are posisioned absolutely over a background image to create an animated scene. Each is embedded using the <object> and <embed> tags for cross-browser compatibility.

<div id="flashLayer1" style="position: absolute; top: 552px; left: 209px;">
    <object id="flashObj1" classid="clsid:D27CDB6E-AE6D-11CF-96B8-444553540000" width="237" height="233">
        <param name="movie" value="lantern.swf">
        <param name="quality" value="High">
        <param name="wmode" value="transparent">
        <!-- Fallback for non-IE browsers -->
        <embed src="lantern.swf" type="application/x-shockwave-flash" width="237" height="233" quality="High" wmode="transparent">
    </object>
</div>
<!-- Additional layers for fireworks, coins, butterflies, etc. follow the same pattern -->

Utility Functions: Cookies and Favorites

Helper functions manage cookies and prompt users to bookmark the site on their first visit.

function createCookie(name, value, minsToExpire, path, domain, isSecure) {
    let expires = '';
    if (minsToExpire) {
        const expiryDate = new Date();
        expiryDate.setTime(expiryDate.getTime() + (minsToExpire * 60 * 1000));
        expires = '; expires=' + expiryDate.toUTCString();
    }
    const cookieStr = encodeURIComponent(name) + '=' + encodeURIComponent(value) + expires + (path ? '; path=' + path : '') + (domain ? '; domain=' + domain : '') + (isSecure ? '; secure' : '');
    document.cookie = cookieStr;
}

function readCookie(name) {
    const match = document.cookie.match(new RegExp('(^| )' + name + '=([^;]+)'));
    return match ? decodeURIComponent(match[2]) : null;
}

function addToFavorites(url, title) {
    try {
        if (window.sidebar) { // Firefox
            window.sidebar.addPanel(title, url, '');
        } else if (window.external && 'AddFavorite' in window.external) { // IE
            window.external.AddFavorite(url, title);
        } else {
            alert('Please bookmark this page manually.');
        }
    } catch (e) {
        alert('Could not add bookmark.');
    }
}

function promptForBookmark() {
    if (!readCookie('site_bookmarked')) {
        createCookie('site_bookmarked', 'true', 5256000, '/'); // ~10 years
        alert('Welcome! Would you like to bookmark our site for easy access?');
        addToFavorites('http://www.example.com', 'Example Flash Site');
    }
}
// Prompt after a delay
setTimeout(promptForBookmark, 7000);

Floating Advertisement Script

A script creates a floating advertisement element that moves around the viewport.

let posX = 50, posY = 60;
let moveRight = true, moveDown = true;
const speed = 1;
const intervalDelay = 10;
const floatAd = document.getElementById('floatingAd');

function animateAd() {
    const maxX = document.body.clientWidth - floatAd.offsetWidth;
    const maxY = document.body.clientHeight - floatAd.offsetHeight;

    floatAd.style.left = posX + document.body.scrollLeft + 'px';
    floatAd.style.top = posY + document.body.scrollTop + 'px';

    posX += speed * (moveRight ? 1 : -1);
    posY += speed * (moveDown ? 1 : -1);

    if (posX <= 0) { moveRight = true; posX = 0; }
    if (posX >= maxX) { moveRight = false; posX = maxX; }
    if (posY <= 0) { moveDown = true; posY = 0; }
    if (posY >= maxY) { moveDown = false; posY = maxY; }
}

const animationInterval = setInterval(animateAd, intervalDelay);
floatAd.onmouseover = function() { clearInterval(animationInterval); };
floatAd.onmouseout = function() { animationInterval = setInterval(animateAd, intervalDelay); };

Page Structure and Additional Elements

The main content is a large table (<table id="__01">) with sliced background images, creating a long, scrollable scene. Absolute-positioned <div> layers containing the Flash objects are placed within this table.

A form at the bottom allows new users to generate their own personalized page:

<div id="inputForm">
    <form id="userForm" onsubmit="return validateForm(this);">
        <strong style="color: #ffff00;">Enter Your Name:</strong>
        <input type="text" id="userName" name="UserName">
        <input type="button" value="Create Greeting" onclick="validateForm(this.form)">
    </form>
</div>

A simple right-click disable script is also included:

document.onmousedown = function(e) {
    if (e.button === 2) { // Right-click
        alert('Function disabled.');
        return false;
    }
};

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.