Current location: Hot Scripts Forums » General Web Coding » JavaScript » [Help] Javascript pop up window with background color


[Help] Javascript pop up window with background color

Reply
  #1 (permalink)  
Old 01-13-05, 02:23 AM
empty.container empty.container is offline
New Member
 
Join Date: Jan 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
[Help] Javascript pop up window with background color

Hello everyone. Killed half a day trying to get this right, and thought i'd post for some help .. and leave it alone for a while before I rip out my hair.

The request is quite simple. I want to create a javascript behavior that opens up a chromeless window (no scrollbars, menu, status etc.) that contains an image only and which will allow me to set the background color. I've tried various methods, including the document.write function .. but i've failed every way i go.

I can get the pop up to create properly but always I stumble at the size of the window. The window is always too small and appears to cut off the image. I'm not quite sure why. For instance, I used this script from Javascript Kit:

Code:
// for free JavaScript tutorials and scripts
// This notice must stay intact for use

var popbackground="lightskyblue" //specify backcolor or background image for pop window
var windowtitle="Image Window"  //pop window title

function detectexist(obj){
return (typeof obj !="undefined")
}

function jkpopimage(imgpath, popwidth, popheight, textdescription){

function getpos(){
leftpos=(detectexist(window.screenLeft))? screenLeft+document.body.clientWidth/2-popwidth/2 : detectexist(window.screenX)? screenX+innerWidth/2-popwidth/2 : 0
toppos=(detectexist(window.screenTop))? screenTop+document.body.clientHeight/2-popheight/2 : detectexist(window.screenY)? screenY+innerHeight/2-popheight/2 : 0
if (window.opera){
leftpos-=screenLeft
toppos-=screenTop
}
}

getpos()
var  winattributes='width='+popwidth+',height='+popheig
ht+',resizable=yes,left='+leftpos+',top='+toppos
var bodyattribute=(popbackground.indexOf(".")!=-1)? 'background="'+popbackground+'"' : 'bgcolor="'+popbackground+'"'
if (typeof jkpopwin=="undefined" || jkpopwin.closed)
jkpopwin=window.open("","",winattributes)
else{
//getpos() //uncomment these 2 lines if you wish subsequent popups to be centered too
//jkpopwin.moveTo(leftpos, toppos)
jkpopwin.resizeTo(popwidth, popheight+30)
}
jkpopwin.document.open()
jkpopwin.document.write('<html><title>'+windowtitle+'</title><body '+bodyattribute+'><img src="'+imgpath+'" style="margin-bottom: 0.5em"><br>'+textdescription+'</body></html>')
jkpopwin.document.close()
jkpopwin.focus()
}

</script>
Everything works to perfection, except that all tested browser (Internet Explorer(PC), Firefox(PC) and Safari on Mac cut off the image on teh bottom and right) Its as if the window is too small. I'm positive I've set the window height and width properly. Several other scripts have this problem as well.

Concindentally if I use the basic open browser window function of dreamweaver (simple window.open (URL, winname, winattributes) it works perfectly .. but I can't because i can't set the background color.

I'd really appreciate if someone could shed some light on this. I very open to suggestions including sample scripts. Thanks everyone.
Reply With Quote
  #2 (permalink)  
Old 01-13-05, 03:11 AM
the_mole001's Avatar
the_mole001 the_mole001 is offline
Newbie Coder
 
Join Date: Feb 2004
Location: Australia
Posts: 96
Thanks: 0
Thanked 0 Times in 0 Posts
Hello There,

First things first, u mentioned there was always a small piece being cut off, thats because html always adds rough 10px to the top and left of the screen when showing a page so u must cancel them out (hence topmargin=0 leftmargin=0 included in the bodyattribute var), nextly to even let the script work on my pc i had to add the ';'s to lines that needed them and remove "(popbackground.indexOf(".")!=-1)?" from the beginning of body attribute. I hope thats all i changed and it doesn't damaged the value of your script. Anyways I did manage to get it working fine on my pc in the end and heres the code i was using:

Code:
<html>
<head>
<title>Testing</title>
<script type='text/javascript'>
// for free JavaScript tutorials and scripts
// This notice must stay intact for use

var popbackground="lightskyblue"; //specify backcolor or background image for pop window
var windowtitle="Image Window" ; //pop window title

function detectexist(obj){
return (typeof obj !="undefined");
}

function jkpopimage(imgpath, popwidth, popheight, textdescription){
	function getpos(){
		leftpos=(detectexist(window.screenLeft))? screenLeft+document.body.clientWidth/2-popwidth/2 : detectexist(window.screenX)? screenX+innerWidth/2-popwidth/2 : 0
		toppos=(detectexist(window.screenTop))? screenTop+document.body.clientHeight/2-popheight/2 : detectexist(window.screenY)? screenY+innerHeight/2-popheight/2 : 0
		if (window.opera){
			leftpos-=screenLeft;
			toppos-=screenTop;
		}
	}
	getpos()
	var  winattributes = 'width='+popwidth+', height='+popheight+', resizable=yes, left='+leftpos+', top='+toppos;
	var bodyattribute = 'topmargin=0 leftmargin=0 bgcolor='+popbackground;
	if (typeof jkpopwin=="undefined" || jkpopwin.closed){
		jkpopwin = window.open("","",winattributes);
	} else {
		//getpos() //uncomment these 2 lines if you wish subsequent popups to be centered too
		//jkpopwin.moveTo(leftpos, toppos)
		jkpopwin.resizeTo(popwidth, popheight+30);
	}
	jkpopwin.document.open();
	jkpopwin.document.write('<html><title>'+windowtitle+'</title><body '+bodyattribute+'><img src="'+imgpath+'" style="margin-bottom: 0.5em"><br><center>'+textdescription+'</body></html>');
	jkpopwin.document.close();
	jkpopwin.focus();
}
</script>
</head>
<body>
<input type='button' onclick='jkpopimage("logout.gif", 150, 25, "a pretty pic");'>
</body>
</html>
Hope that helps get your code to working order even if you need to add parts yourself. Sorry for the non-complete original script.
__________________
Current Project: GGAC Website
Project Link: http://peter.5gigs.com/
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Blank window pop up message leocordero Script Requests 1 12-12-04 01:43 PM
Background color of image page FinalFan HTML/XHTML/XML 3 10-17-04 06:47 PM
checkbox background color in [puke] IE nassau CSS 2 10-12-04 02:24 PM
pop up window nitefire JavaScript 1 08-03-04 10:10 AM
Javascript opens links in new window podman JavaScript 2 08-21-03 01:49 PM


All times are GMT -5. The time now is 10:38 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.