Newbie help with opening new window part 2...

S

StormJr

Alright, I figured out the code that allows me to open a new window
using a button. Here is the code:

The new function:

function newWindow() {newwindowa = window.open("../New
Folder/imageloop.htm", "nwindow", "toolbar=yes, menubar=yes,
location=yes, scrollbar=yes, status=yes, resizable=yes");
}


In the Body:

<INPUT TYPE=BUTTON ID="newwindowButton" VALUE="New Window"
ONCLICK="newWindow()" onmouseover="self.status ='Opens Another Window';
return true" onmouseout="self.status=''">


Button:

#newwindowButton {
font: 12px verdana,helvetica,arial;
color : #FFFFFF;
border-color: #3333FF;
background-color : #3333AA;
font-weight: bold;
padding-left: 0;
padding-right: 0;
}

My question is, this only opens one window. I want it to open multiple
windows if needed. What am I missing?

Thanks in advance
 
R

RobG

StormJr said:
Alright, I figured out the code that allows me to open a new window
using a button. Here is the code:

The new function:

function newWindow() {newwindowa = window.open("../New
Folder/imageloop.htm", "nwindow", "toolbar=yes, menubar=yes,
location=yes, scrollbar=yes, status=yes, resizable=yes");
}

Manually wrap code at about 70 characters before posting. Allowing
automatic wrapping nearly always introduces errors not in the original
script. Indenting helps too (though it looks like you may be using a
web interface that might be stripping leading spaces).

The URL should also be valid, so spaces should be escaped
(i.e. replaced with '%20'). The easy way to to this is to use
encodeURI, that way any character that needs encoding will get it:

function newWindow() {
var p = encodeURI('../New Folder/imageloop.htm');
newwindowa = window.open(p,'nwindow',
'toolbar,menubar,location,scrollbar,status,resizable'
);
}
[...]

My question is, this only opens one window. I want it to open multiple
windows if needed. What am I missing?

You create the new window using a global variable - declaring a
variable inside a function without the 'var' keyword makes it global.
Declaring a variable outside any function makes it global whether the
'var' keyword is used or not.

Each time you call newWindow(), you are re-allocating the same
parameters to it.

The fact that newwindow is global is necessary in this case, but you
need to create a unique reference to each window - an array is one way
to achieve it. You probably also want to pass the path as a parameter
to the function so you can control what is opened in the window from
elsewhere:

// Declare global variables
var i=0;
var newwindow=[];

function newWindow( p ) {

p = encodeURI( p );

// Create a unique variable name each time
newwindow[i++] = window.open( p, 'nwindow',
'toolbar,menubar,location,scrollbar,status,resizable'
);
}

And your button:

<input type="button" ... onclick="
newWindow('../New Folder/imageloop.htm)');
" ... >

Remember that most users dislike pop-ups, so unless you have a good
reason for them, you should probably do something else.
 
D

DU

RobG wrote:

newwindow[i++] = window.open( p, 'nwindow',
'toolbar,menubar,location,scrollbar,status,resizable'

It should be scrollbars; if you write scrollbar, then the windowFeature
will be ignored because it is unrecognized and scrollbars feature will
be turned off if content overflows window dimensions.

DU
 
R

RobG

DU said:
RobG wrote:

newwindow[i++] = window.open( p, 'nwindow',
'toolbar,menubar,location,scrollbar,status,resizable'


It should be scrollbars; if you write scrollbar, then the windowFeature
will be ignored because it is unrecognized and scrollbars feature will
be turned off if content overflows window dimensions.

DU

Doh... :-x

Copy/paste laziness rules...
 
R

Random

RobG said:
StormJr said:
Alright, I figured out the code that allows me to open a new window
using a button. Here is the code:

The new function:

function newWindow() {newwindowa = window.open("../New
Folder/imageloop.htm", "nwindow", "toolbar=yes, menubar=yes,
location=yes, scrollbar=yes, status=yes, resizable=yes");
}
....

function newWindow() {
var p = encodeURI('../New Folder/imageloop.htm');
newwindowa = window.open(p,'nwindow',
'toolbar,menubar,location,scrollbar,status,resizable'
);
}
[...]

My question is, this only opens one window. I want it to open multiple
windows if needed. What am I missing?

You create the new window using a global variable - declaring a
variable inside a function without the 'var' keyword makes it global.
Declaring a variable outside any function makes it global whether the
'var' keyword is used or not.

Each time you call newWindow(), you are re-allocating the same
parameters to it.

The fact that newwindow is global is necessary in this case,

Not necessarily, but I won't stickle on that one.
but you
need to create a unique reference to each window - an array is one way
to achieve it. You probably also want to pass the path as a parameter
to the function so you can control what is opened in the window from
elsewhere:

// Declare global variables
var i=0;
var newwindow=[];

function newWindow( p ) {

p = encodeURI( p );

// Create a unique variable name each time
newwindow[i++] = window.open( p, 'nwindow',
'toolbar,menubar,location,scrollbar,status,resizable'
);
}


You're also using the same window name every time the function is
called. In addition to the suggestion about using an array to store the
JavaScript references to the windows being helpful, you will also need
to use different window names to actually make them different windows.
Otherwise, the same window will be reused.

Try this:

var newwindow = new Array();

function newWindow( URI ) {
URI = URI || 'about:blank';
newwindow.push(
window.open(
encodeURI( URI ),
'nwindow' + newwindow.length,
'toolbar,menubar,location,scrollbars,status,resizable'
)
);
}
 
S

StormJr

All the button will do is open the same page. Most users using this
page already open multiple windows since they have multiple monitors. I
know its just as easy to do ctrl-N, but a button on the website would
be nice.
 
S

StormJr

I created another thread, because this is a new issue, not the same. I
come here asking for your advice. If i wanted to be spoonfed, I would
go to a copy and paste website. A friend of mine who uses this site a
lot recommended it to me when I get stuck in my code (as i said, I new.
What may seem easy to you, is more difficult for me). He said everyone
would be helpful. Well, everyone has been, but you don't have to be a
jerk about it. If you don't like the way I post or my question, then
dont answer, it's that simple.
 
E

Evertjan.

StormJr wrote on 30 mei 2005 in comp.lang.javascript:
A friend of mine who uses this site a
lot recommended it to me when I get stuck in my code (as i said, I new.
What may seem easy to you, is more difficult for me). He said everyone
would be helpful. Well, everyone has been, but you don't have to be a
jerk about it. If you don't like the way I post or my question, then
dont answer, it's that simple.

It is not.

You are posting to Usenet, in existence 20 years before the notion of
"website" was born.

Perhaps you are posting VIA a website, but even so, please adhere to
Netiquette and the NG charter and/or FAQs.
 
R

RobG

Random said:
RobG said:
StormJr wrote:
[...]

The fact that newwindow is global is necessary in this case,


Not necessarily, but I won't stickle on that one.

OK, then let's call it convenient.
but you
need to create a unique reference to each window - an array is one way
to achieve it. You probably also want to pass the path as a parameter
to the function so you can control what is opened in the window from
elsewhere:

// Declare global variables
var i=0;
var newwindow=[];

function newWindow( p ) {

p = encodeURI( p );

// Create a unique variable name each time
newwindow[i++] = window.open( p, 'nwindow',
'toolbar,menubar,location,scrollbar,status,resizable'
);
}



You're also using the same window name every time the function is
called. In addition to the suggestion about using an array to store the
JavaScript references to the windows being helpful, you will also need
to use different window names to actually make them different windows.
Otherwise, the same window will be reused.

Yeah, thanks. Over-zealous 'cleaning up' before posting. Original
line was:

newwindow[i++] = window.open( p, 'nwindow' + i,
....
Try this:

var newwindow = new Array();

function newWindow( URI ) {
URI = URI || 'about:blank';
newwindow.push(
window.open(
encodeURI( URI ),
'nwindow' + newwindow.length,
'toolbar,menubar,location,scrollbars,status,resizable'
)
);
}

Yes, that's another way to do pretty much exactly the same thing.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top