By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,132 Members | 1,417 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,132 IT Pros & Developers. It's quick & easy.

IE javascript window.open problem

P: n/a
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
Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Samir Pandey wrote:
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 <gw*****@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_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-deve...upgrade_2.html
Jul 23 '05 #2

P: n/a
Samir Pandey wrote:
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">
<script type="text/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>
</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
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.