Building Pop-Up DIVs with the DOM and AJAX - Completing the pop-up DIV script
(Page 4 of 4 )
As I promised in the section that you just read, here is the complete source code that corresponds to this pop-up DIV application, this time including some CSS rules to polish the look and feel of the respective web page and make the pop-up work correctly.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1" />
<title>JavaScript-based Pop-Up Div</title>
<style type="text/css">
body{
padding: 0;
margin: 0;
background: #fff;
}
h1{
font: bold 24px Arial, Helvetica, sans-serif;
color: #000;
}
.popupcontainer{
width: 300px;
padding: 10px;
background: #eee;
border: 1px solid #ccc;
font: normal 12px Arial, Helvetica, sans-serif;
color: #000;
}
.popupdiv{
position: absolute;
width: 200px;
padding: 5px;
background: #ffc;
border: 1px solid #ccc;
font: normal 12px Arial, Helvetica, sans-serif;
color: #000;
}
</style>
<script language="javascript">
// 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);
}
// 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;
}
}
</script>
</head>
<body>
<h1>JavaScript-based Pop-up Div</h1>
<div class="popupcontainer" id="container">
<p>Content for containing DIV goes here...<br />
Content for containing DIV goes here...<br />
Content for containing DIV goes here...<br />
Content for containing DIV goes here...<br />
Content for containing DIV goes here...<br />
Content for containing DIV goes here...<br />
Content for containing DIV goes here...</p>
</div>
</body>
</html>
All right, that’s all you need to have a fully-functional pop-up DIV, ready to include into any web document. In addition, you can download a ZIP file (included at the beginning of this article) that contains a working example that includes the limitations that I outlined in a previous section. I hope you find this script useful! (link to pop_up_div.zip goes here).
Final thoughts
In the first part of the series, I showed you how to create a simple pop-up DIV, which admittedly includes some limitations, since it won’t be displayed by Internet Explorer when the web page is scrolled down.
Nevertheless, this bug will be fixed in the next article, in addition to expanding the initial script to work with multiple pop-up DIVs. This is really interesting, thus don’t miss the next tutorial!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |