IE javascript window.open problem

S

Samir Pandey

Hello,

I am using the following javascript code to open a new window.
Somehow, IE always opens a new window. It doesn't open target url in
the window name given.

All i want is, if there is a window "MyWin" already open, then the
main window should load the target url in "MyWin" and not open a new
window again.

<!-- Code Starts -->
<html><head></head>
<Script Language="javascript">
function Callme()
{
var win2 = window.open("Test2.htm", "MyWin");
}

/* I don't want to use following type of code as it serves the purpose
initially but not if you refresh the main page.*/
/*
function Callme(location){
var win2;
if (typeof win2 == "object") {
win2.location.replace(location);
}
else{
win2=window.open(location,"MyWin",null,true);
}
}
// thanks
*/

</Script>
<body>
<form>
<input type="button" value="Click Me" onClick="Callme();">
</form>
</body>
</html>
<!-- Code Ends -->

Any hint/help apprreciated.
IE version : 6.0.2800.1106.xpsp2.030422-1633
OS : Windows XP

Thanks,
Samir
 
G

Grant Wagner

Samir said:
Hello,

I am using the following javascript code to open a new window.
Somehow, IE always opens a new window. It doesn't open target url in
the window name given.

All i want is, if there is a window "MyWin" already open, then the
main window should load the target url in "MyWin" and not open a new
window again.

<!-- Code Starts -->
<html><head></head>
<Script Language="javascript">

<script> tags should appear either inside <head>...</head> or inside
<body>...</body>.

The "language" attribute is deprecated. Use type="text/javascript"
instead.
function Callme()
{
var win2 = window.open("Test2.htm", "MyWin");
}

/* I don't want to use following type of code as it serves the purpose
initially but not if you refresh the main page.*/
/*
function Callme(location){
var win2;
if (typeof win2 == "object") {

Because you are declaring win2 on every call to the function, it will
_never_ be typeof "object". If you want to do something like this, use a
global variable to track the existance of win2. This task is covered in
the FAQ:

<url: http://jibbering.com/faq/#FAQ4_10 />

--
| Grant Wagner <[email protected]>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/author/dhtml/reference/dhtml_reference_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-developer/upgrade_2.html
 
T

Thomas 'PointedEars' Lahn

Samir said:
I am using the following javascript code to open a new window.
Somehow, IE always opens a new window. It doesn't open target url in
the window name given.

All i want is, if there is a window "MyWin" already open, then the
main window should load the target url in "MyWin" and not open a new
window again.

<!-- Code Starts -->
<html><head></head>

In Valid (X)HTML, the "head" element must not be empty. It must at
least contain a "title" element. Besides, have you used a proper
DOCTYPE declaration? If not, that would make the document invalid,
too. You cannot build a house on a swamp.
<Script Language="javascript">

function Callme()
{
var win2 = window.open("Test2.htm", "MyWin");
}

/* I don't want to use following type of code as it serves the purpose
initially but not if you refresh the main page.*/
/*
function Callme(location){
var win2;
if (typeof win2 == "object") {

Since win2 is declared local (note the `var' keyword), this branch will
be never executed as `win2''s value and type is always `undefined'.
win2.location.replace(location);
}
else{
win2=window.open(location,"MyWin",null,true);

You maybe run into a scope problem here. `location' is already a
property of the window object. Use another variable identifier.

window.open has only three arguments: The URI of the resource to
access, the internal name of the new Window object, and a *string*
containing definitions of its features. I wonder what you think
`null' and `true' will do.

And there is no need to check if the window is already open if you use
a unique internal window name. If it is not open, it will be opened,
if it is still open, it will be reused. All you may want to do is to
focus the window so it does not remain in the background if it is reused.

function isMethodType(s)
{
return (s == "function" || s == "object");
}

function callMe(sURI)
{
var result = window.open(sURI, "MyWin", "");

if (result && isMethodType(typeof result.focus))
{
result.focus();
}

return result;
}

var win2;
// ...
win2 = callMe("a_fool");
// thanks
?

*/

As you comment everything out, ...
</Script>

<body>
<form>
<input type="button" value="Click Me" onClick="Callme();">

.... why do you expect this to work anyway?
[...]
IE version : 6.0.2800.1106.xpsp2.030422-1633
OS : Windows XP

Ohh, would just anyone post this precise information in the first place!


PointedEars
 

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

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top