rmedek
03-10-2006, 01:56 AM
I'm trying to adapt an image gallery script detailed in Jeremey Keith's book (http://domscripting.com/) to work with popup windows. The idea is a basic gallery has thumbnails and clicking on one shows a more detailed image in a new, popup window.
The HTML on the gallery page is:
<table id="popupgallery">
<tr>
<td>Description 1</td>
<td>Description 2</td>
<td>Description 3</td>
</tr>
<tr>
<td><a href="/images/image1.jpg" title="image description"><img src="/images/image1_thumb.jpg" width="150" height="100" alt="image description" /></a></td>
<td><a href="/images/image2.jpg" title="image description"><img src="/images/image2_thumb.jpg" width="150" height="100" alt="image description" /></a></td>
<td><a href="/images/image3.jpg" title="image description"><img src="/images/image3_thumb.jpg" width="150" height="100" alt="image description" /></a></td>
</tr>
</table>
The js that creates an onload event and prepares the gallery is:
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != "function") {
window.onload = func;
} else {
window.onload = function() {
oldonload();
func();
}
}
}
function preparePopUp () {
if(!document.getElementsByTagName) return false;
if(!document.getElementById) return false;
if(!document.getElementById("popupgallery")) return false;
var gallery = document.getElementById("popupgallery");
var links = gallery.getElementsByTagName("a");
for (var i=0; i < links.length; i++) {
links[i].onclick = function() {
return popUp(this);
}
}
}
addLoadEvent(preparePopUp);
Now, here's where I get stuck. I'd like to create my new window, assign it a name, and replace different attributes with references to the old window. I'm not sure how that works:
function popUp (whichpic) {
if(document.getElementById) {
newWin = window.open('popup.html','mywin', 'toolbar=no, scrollbars=no, resizable=no, menubar=no, status=no, directories=no, location=no, width=400, height=300');
newWin.document.getElementById('placeholder').src = whichpic.href;
if (whichpic.title) {
newWin.document.getElementById('desc').childNodes[0].nodeValue = whichpic.title;
} else {
newWin.document.getElementById('desc').childNodes[0].nodeValue = whichpic.childNodes[0].nodeValue;
}
return false;
} else {
return true;
}
}
popup.html:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>test</title>
</head>
<body>
<p id="desc">Description goes here</p>
<img id="placeholder" src="/images/placeholder.jpg" alt="" width="350" height="250" />
</body>
</html>
Running everything as-is gives me a "newWin.document.getElementById('placeholder').src has no properties" error which I am assuming is because the js gets fired before the popup is finished loading. But I can't figure out how to run the other half of that function after the page gets loaded. Am I taking the wrong approach here? Do I need a separate function in the new window?
The HTML on the gallery page is:
<table id="popupgallery">
<tr>
<td>Description 1</td>
<td>Description 2</td>
<td>Description 3</td>
</tr>
<tr>
<td><a href="/images/image1.jpg" title="image description"><img src="/images/image1_thumb.jpg" width="150" height="100" alt="image description" /></a></td>
<td><a href="/images/image2.jpg" title="image description"><img src="/images/image2_thumb.jpg" width="150" height="100" alt="image description" /></a></td>
<td><a href="/images/image3.jpg" title="image description"><img src="/images/image3_thumb.jpg" width="150" height="100" alt="image description" /></a></td>
</tr>
</table>
The js that creates an onload event and prepares the gallery is:
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != "function") {
window.onload = func;
} else {
window.onload = function() {
oldonload();
func();
}
}
}
function preparePopUp () {
if(!document.getElementsByTagName) return false;
if(!document.getElementById) return false;
if(!document.getElementById("popupgallery")) return false;
var gallery = document.getElementById("popupgallery");
var links = gallery.getElementsByTagName("a");
for (var i=0; i < links.length; i++) {
links[i].onclick = function() {
return popUp(this);
}
}
}
addLoadEvent(preparePopUp);
Now, here's where I get stuck. I'd like to create my new window, assign it a name, and replace different attributes with references to the old window. I'm not sure how that works:
function popUp (whichpic) {
if(document.getElementById) {
newWin = window.open('popup.html','mywin', 'toolbar=no, scrollbars=no, resizable=no, menubar=no, status=no, directories=no, location=no, width=400, height=300');
newWin.document.getElementById('placeholder').src = whichpic.href;
if (whichpic.title) {
newWin.document.getElementById('desc').childNodes[0].nodeValue = whichpic.title;
} else {
newWin.document.getElementById('desc').childNodes[0].nodeValue = whichpic.childNodes[0].nodeValue;
}
return false;
} else {
return true;
}
}
popup.html:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>test</title>
</head>
<body>
<p id="desc">Description goes here</p>
<img id="placeholder" src="/images/placeholder.jpg" alt="" width="350" height="250" />
</body>
</html>
Running everything as-is gives me a "newWin.document.getElementById('placeholder').src has no properties" error which I am assuming is because the js gets fired before the popup is finished loading. But I can't figure out how to run the other half of that function after the page gets loaded. Am I taking the wrong approach here? Do I need a separate function in the new window?