JavaScript
  Home arrow JavaScript arrow Page 3 - Building Pop-Up DIVs with the DOM and AJAX
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

Building Pop-Up DIVs with the DOM and AJAX
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 15
    2007-01-31

    Table of Contents:
  • Building Pop-Up DIVs with the DOM and AJAX
  • Developing a basic pop-up DIV
  • Dynamically creating a pop-up DIV
  • Completing the pop-up DIV script

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Building Pop-Up DIVs with the DOM and AJAX - Dynamically creating a pop-up DIV


    (Page 3 of 4 )

    In consonance with the concepts that I expressed in the previous section, the next step to take to attach a single pop-up DIV to the web page that was shown before consists of defining some comprehensive JavaScript functions. These will be tasked with displaying the referenced pop-up box when the mouse is over a selected element, and hiding it when the cursor is out.

    Having explained how the pop-up DIV is going to work into the web page, here are the two JavaScript functions responsible for showing and hiding the mentioned element respectively:

    // display Pop Up div element
    function displayPopupDiv(e){
       var posx=0;
       var posy=0;
       if(!e){var e=window.event};
       // determine target DIV
       var targ=e.target?e.target:e.srcElement;
       // calculate mouse coordinates
       if(e.pageX||e.pageY){
         posx=e.pageX;
         posy=e.pageY;
       }
       else if(e.clientX||e.clientY){
         posx=e.clientX;
         posy=e.clientY;
       }
       // assign attributes to pop-up DIV element and append it to
       // web document tree
       var div=document.getElementById('popup');
       if(!div){
         var div=document.createElement('div');
         div.setAttribute('id','popup');
         div.className='popupdiv';
         div.appendChild(document.createTextNode('This is a sample
    content for pop-up DIV element.'));
         document.getElementsByTagName('body')[0].appendChild
    (div);  
       }
       // move pop-up DIV element
       div.style.top=posy+5+'px';
       div.style.left=posx+5+'px';
    }
    // remove pop-up DIV element
    function hidePopupDiv(){
       var div=document.getElementById('popup');
       if(!div){return};
       div.parentNode.removeChild(div);
    }

    As you may have noticed, the first JavaScript function, called “displayPopUpDiv(),” is responsible for performing some crucial tasks, such as creating a DIV element on the fly, and finally appending it to the web document tree. Besides, the pop-up box is positioned relatively to the mouse coordinates, which means that it‘s possible to create a “chasing” effect if the appropriate event handler is annexed to a particular element of the web page.

    Finally, the “hidePopUpDiv()” function is definitely simpler, since all it does is remove the pop-up box from the document tree. Period. That was really easy, was it?

    However, I’d like to mention that the two above JavaScript functions are completely useless if I don’t attach some event handlers to the DIV element identified as “container,” in this way turning the pop-up box into a functional component. Thus, in response to this, below I included a simple JavaScript code which assigns the corresponding event handlers to the “container” DIV element. Here is the short code block:

    // display pop-up DIV element when web page has been loaded
    window.onload=function(){
       if(document.getElementById && document.createElement &&
    document.createTextNode){
         var div=document.getElementById('container');
         if(!div){return};
         // display pop-up DIV element
         div.onmousemove=displayPopupDiv;
         // hide pop-up DIV element
         div.onmouseout=hidePopupDiv;
       }

    As you can see, the above script will fire up the “displayPopUpDiv()” function when the mouse cursor is over the selected element, and will hide the pop-up box when the mouse is out, in this way achieving the “mouse chasing” effect that I mentioned previously.

    Naturally, I’d like to clarify some important points regarding how the pop-up DIV works. First, if you study the previous JavaScript functions, you’ll realize that the pop-up box won’t be displayed by Internet Explorer if the web page is scrolled down. Also, at least for the moment, the DIV in question isn’t capable of displaying content pulled out from a remote source, like a database table.

    However, don’t worry about all these issues, since they’ll be properly fixed in the next article of the series.

    Okay, having clarified the limitations of this simple pop-up DIV, let’s move on to the following section, where I’ll be listing the full source code that corresponds to this basic DHTML application.

    Go ahead. I’ll be there, waiting for you.

    More JavaScript Articles
    More By Alejandro Gervasio


       · In this first article of the series, a basic method for creating pop-up DIVS is...
     

    JAVASCRIPT ARTICLES

    - Validating Digits and Dates with jQuery`s Va...
    - Validating Ranges, Emails, and URLs with jQu...
    - More Uses for the jQuery Tooltip Plug-in`s b...
    - Building Image-Based Tooltips with the jQuer...
    - Using the jQuery Tooltip Plug-in`s bodyHandl...
    - Using Rangelength, Min and Max with the Vali...
    - Using Minlength and Maxlength with the Valid...
    - Modifying Tooltip Coordinates with the jQuer...
    - Applying a Fade Out Effect with the jQuery T...
    - Tracking Mouse Movements with the jQuery Too...
    - Checking Online Forms with the Validator jQu...
    - Nested JavaScript Functions as Objects
    - The jQuery Tooltip Plug-in
    - Active Client Pages at the Server
    - ACP Tab Web Page







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT