Discussion Locked

Arrrrgh! Javascript window.open ()

0 helpful
3 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Doing a simple project for myself at home and cannot for the life of me figure out what is wrong with my script.

I am building a simple search function in that I want to open a new window once the search parameters have been entered and a "search" formbutton is pushed...

function tuneform() {
document.Muz_form.q.select();
document.Muz_form.q.focus();
}

function isblank(s)
{
for(var i = 0; i < s.length; i++) {
var c = s.charAt(i);
if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
}
return true;
}
function formcheck() {
var d = document.Muz_form.db[1].checked;
var e = document.Muz_form.q.value;
if ((e == null) || (e == "") || isblank(e)) {
alert("Please enter a word to lookup.");
tuneform();
}
else if (d == 1) {
location.href = window.open("http://www.mydatabase2.com/cgi-bin/search?config=thes&words=" + escape(e));
}
else {
location.href = window.open("http://www.mydatabase.com/search?q=" + escape(e));
}
return false;
}

My Dilemma is this. The window opens beautifully without issue. BUT the parent window decides it wants to display a DNS error page instead of staying unchanged. It seems like a simple script, but I must be forgetting something stupid. :)

The form appears as thus:

3 replies

by i_am_whopper

Within the form make the action attribute blank:

action=""

instead of

action="/search"

If you leave a value in the action attribute, then after your script has finished processing the page will navigate to the page specified in the action attribute (in this case it is pointing to a directory, hence the error).

by jay eckles

You're trying to assign to location.href (basically the URL of the current page) the handle (a pointer if you're familiar with C) to the window that's been opened. That's why the parent page displays a DNS error. It's trying to load a non-sensicalURL, at your request.

Instead of
location.href = window.open( ... ) ;
try
var winHandle = window.open( ... ) ;
location.href = "http://wherever.you.want" ;

If you don't want the URL of the parent page to change at all when you open the new window, then you don't need to assign anything to location.href.

By the way, the reason you save the window handle in a variable is so you can manipulate the window from the parent window, as in
winHandle.close() ;

Jay

This discussion is closed to new replies.