...

View Full Version : Simple javascript window nameing question



Spinacia
08-10-2004, 01:02 PM
Hi,
I am not a developer and apologize for this simple question/request.

I use the following script on my website to open windows that display images:

<script language="JavaScript">
function SmallWindow(wintype) {
SmallWin=window.open(wintype,"SmallWin","toolbar=no,directories=no,status=no,scrollbars=yes,menubar=no,width=650,height=500"); SmallWin.window.focus()}
</script>
The windows that open have no title and display the path to the image.

1. How can I pass to the function the desired name I want to assign to the window?
2. How can I globally assign a name to every window that is opened with this function?

Please help.

Thanks.

Willy Duitt
08-10-2004, 01:34 PM
The window name is here highlighted in red....
SmallWin=window.open(wintype,"SmallWin","toolbar=no,....

But it would be easier to help you if we could see how you are calling the function and exactly what names you are wanting to use. However, you could pass the name as an arguement as you did with the url. (function SmallWindow(wintype))....

.....Willy

Spinacia
08-10-2004, 01:43 PM
Hi Willy,
Here is how I call the function:

<a href="javascript:SmallWindow('open.jpg')"></a>

Let me clarify further:
I would like to give the window being opend a name, similar to:

<title>This is the window name</title>
as used in HTML.

Thanks.

glenngv
08-10-2004, 02:11 PM
Since you are directly opening an image and not an html page, there's no title in the browser window. By default in IE, the full url is displayed, in Firefox the filename including image size.

Try using the code below instead. I modified the popup image viewer (http://www.codingforums.com/showthread.php?t=9836) script to make the title dynamic.


function popImg(imageURL, title) {
var imgWin = window.open('about:blank','imgWin','width=200, height=200, left=100, top=100');

with (imgWin.document) {
writeln('<html><head><title>Loading...</title>');
writeln('<style type="text/css"><!-- body { margin: 0px; } --></style></head>');
writeln('<body onload="self.focus();"><img id="pic" style="display:none" /></body></html>');
close();
}
var img = new Image();
img.onload = function() { sizeImgWin(imgWin, img, title) };
img.src = imageURL;
return false;
}

function sizeImgWin(win, img, title) {
var new_w = img.width;
var new_h = img.height;
var old_w = win.innerWidth || win.document.body.offsetWidth;
var old_h = win.innerHeight || win.document.body.offsetHeight;
if (!new_w) { new_w = old_w; }
if (!new_h) { new_h = old_h; }
new_w -= old_w; new_h -= old_h;
win.resizeBy(new_w,new_h);
win.document.title = (title) ? title:img.src.substring(img.src.lastIndexOf("/")+1);
var pic = win.document.getElementById('pic');
pic.src = img.src;
pic.style.display = 'block';
}

You would use it this way:

<a href="open.jpg" onclick="return popImg(this.href, 'This is the open image')"></a>

Spinacia
08-10-2004, 02:16 PM
Hi Glenn,
Quite clever and thanks for this script.

You wrote:
Since you are directly opening an image and not an html page, there's no title in the browser window. By default in IE, the full url is displayed, in Firefox the filename including image size.

So, is there any way to change the default way IE behaves? Is there a way to place a title instead of the full URL?

Thanks.

glenngv
08-10-2004, 02:44 PM
Why are you still insisting on your original implementation which can't be done? The script I posted already solves this problem.

Spinacia
08-10-2004, 02:55 PM
Why are you still insisting on your original implementation which can't be done? The script I posted already solves this problem.

Glenn,
You gave me fine alternative but I need a solution addressing the original script because it is embedded in many HTML pages I use. I could easily alter the original script with a batch search and replace.

You said: your original implementation which can't be done. So, are you declaring that the title problem can not be programmatically solved, with the original script calling an image?

Thanks.

Willy Duitt
08-10-2004, 03:02 PM
So, are you declaring that the title problem can not be programmatically solved, with the original script calling an image?

That's exactly what he said....



I could easily alter the original script with a batch search and replace.
If you can use a batch search and replace to modify part of your original function, why can't you do the same to replace the entire function with the new function?

Also, you are incorrectly using the anchor to call your function and thus making your links inaccessable to users whom have javascript disabled. This too, should also be addressed....

.....Willy

Spinacia
08-10-2004, 03:10 PM
That's exactly what he said....
I guess you agree with him... :)



If you can use a batch search and replace to modify part of your original function, why can't you do the same to replace the entire function with the new function?
I could replace a function, but not introduce a full script. I don't have the tools to do multiple-line search and replace.

In any case, thanks for the help.

glenngv
08-11-2004, 02:43 AM
Put the code I posted in an external file, name it as popimage.js. Then just include it in your pages that need that script.

<script type="text/javascript" src="popimage.js"></script>

Spinacia
08-11-2004, 07:59 AM
Put the code I posted in an external file, name it as popimage.js. Then just include it in your pages that need that script.

<script type="text/javascript" src="popimage.js"></script>

That's the best idea by far and also feasible. Thanks.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum