/    Sign up×
Community /Pin to ProfileBookmark

javascript popup help

Im having a few problems with a script. The idea is to define a DIV on a page (out of the body of the document and not shown on the page) containing some HTML to be displayed in a popup window. The DIV id tag is sent to this function, which retrieves the DIV from the page via document.getElementByID().

The function then finds an appropriate size for the popup window based on the size of the DIV and sets up the window properties string accordingly. It then opens a new window with this string, writes some HTML into the new window, giving the body the “popup_body” id, and inserts the DIV using
doc.getElementById(“popup_body”).appendChild(element);

Now it gets as far as opening a window, but the width and height are given as 20, and i get a blank window. All the HTML is there when i click view source, apart from the elment sent to the function. Scryptix says the above appendChild line has a syntax error on it, but i dont see it…? What could i be doing wrong? I have a similar script for images, which works perfectly…

Thanks in advance for any help

-Jonskichov (code follows)

[FONT=Courier New]function html_popup(element_id) {
var element = document.getElementById(element_id);

//get correct width and height for window, making sure it isn’t bigger than the screen resolution//
var winWidth = element.style.width;
var winHeight = element.style.height;
var wThreshold = screen.availWidth;
var hThreshold = screen.availHeight;

winWidth = winWidth + 20;
if(winWidth > wThreshold) {
winWidth = wThreshold;
}
winHeight = winHeight + 20;
if(winHeight > hThreshold) {
winHeight = hThreshold;
}

alert(“w ” + winWidth + ” h ” + winHeight);

//open new window and make correct size//
var settingsString = “toolbar=no,” +
“location=no,” +
“directories=no,” +
“status=no,” +
“menubar=no,” +
“scrollbars=yes,” +
“resizable=yes,” +
“width=” + winWidth + “,” +
“height=” + winHeight;
var newWin = window.open(“”, “_blank”, settingsString, “false”);

//write html and add element//
var doc = newWin.document;
doc.open();
doc.writeln(“<html>n”);
doc.writeln(” <head>n”);
doc.writeln(” <title> a title </title>n”);
doc.writeln(” <link rel=”stylesheet” type=”text/css” href=”style.css”>n”);
doc.writeln(” </head>n”);
doc.writeln(” <body id=”popup_body”>n”);
doc.writeln(” </body>n”);
doc.write(“</html>n”);
doc.close();
doc.getElementById(“popup_body”).appendChild(element);

//focus new window//
newWin.focus();

}[/FONT]

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@jzwpJan 23.2006 — I don't know what you are trying to do but, here is what I have scratched together so far..
[code=php]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD><TITLE> </TITLE>
<STYLE type=text/CSS>

</STYLE>
<SCRIPT type="text/javascript">
function html_popup(element_id,pix) {
var element = document.getElementById('MyDiv');
//get correct width and height for window, making sure it isn't bigger than the screen resolution//
var winWidth = element.style.width;
var winHeight = element.style.height;
var wThreshold = screen.availWidth;
var hThreshold = screen.availHeight;

winWidth = winWidth + 20;
if(winWidth > wThreshold) {
winWidth = wThreshold;
}
winHeight = winHeight + 20;
if(winHeight > hThreshold) {
winHeight = hThreshold;
}

alert("w " + winWidth + " h " + winHeight);
//open new window and make correct size//
var settingsString = "toolbar=no," +
"location=no," +
"directories=no," +
"status=no," +
"menubar=no," +
"scrollbars=yes," +
"resizable=yes," +
"width=" + winWidth + "," +
"height=" + winHeight;
var newWin = window.open("", "_blank", settingsString, "false");

//write html and add element//
var doc = newWin.document;
doc.open();
doc.writeln("<html>n");
doc.writeln(" <head>n");
doc.writeln(" <title> a title </title>n");
doc.writeln(" </head>n");
doc.writeln(" <body id="popup_body" style="margin:0px">n");
//doc.writeln(" <div src=""+pix+""> n");
doc.writeln(" </body>n");
doc.write("</html>n");
doc.close();
//doc.getElementById("popup_body").appendChild(element);

//focus new window//
newWin.focus();
}
</SCRIPT>
</HEAD>
<BODY>
<button onclick="html_popup('MyDiv','39.jpg');">my div</button>
<div id="MyDiv" style="height:200px;width:100px;border:1px solid teal">some stuff</div>
</BODY></HTML>[/code]
×

Success!

Help @jonskichov spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...