JS to open serach results in a new tab/window.

I have a JS Code which goes to a new site upon click or/and enter. I would like to the new site to open in a new tab or window. Right now it is opening in the same window. I have tried adding window.open(" ") around my links, but that didn't work. Also use of iframes is not allowed. Your help is greatly appreciated.
Member_2_7971128Asked:
Who is Participating?
 
OMC2000Commented:
Add

 onsubmit="return false"

to form definition:

      <form name="search_form" id="search_form" onsubmit="return false">

form is getting submitted on Enter into the save window in parallel with new window openning
0
 
OMC2000Commented:
Could you post an example of your window.open() statement. It works in general. Probably your browser doesn't allow popup windows?
0
 
Member_2_7971128Author Commented:
Hum may be I did it wrong. This is what I tried.
document.location = window.open("http://query.nytimes.com/search/query?query=" + document.search_form.search.value + "&date_select=full");
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
OMC2000Commented:
document.location is a string containing URL

 window.open("http://query.nytimes.com/search/query?query=" + document.search_form.search.value + "&date_select=full");
returns window object.

So, statement
document.location = window.open("http://query.nytimes.com/search/query?query=" + document.search_form.search.value + "&date_select=full");
is not correct

However, window.open should work before and window should appear. What do you mean does not work? You don't see new window or it appears and does not contain expected result?
0
 
Member_2_7971128Author Commented:
It opens a new window with results, but on the current window, it also give a error saying "HTTP Error 404.0 - Not Found

The resource you are looking for has been removed, had its name changed, or is temporarily unavailable."

how should I fix this? Thanks.
0
 
OMC2000Commented:
Well, command

 window.open("http://query.nytimes.com/search/query?query=" + document.search_form.search.value + "&date_select=full");

opens a new windows and fills it with result, it returns windows object. You assign it to current page location property and it reloads current page with result of window object conversion to string. It is not URL, and that's why you are getting 404 error.

just leave statement
 window.open("http://query.nytimes.com/search/query?query=" + document.search_form.search.value + "&date_select=full");

instead of

document.location = window.open("http://query.nytimes.com/search/query?query=" + document.search_form.search.value + "&date_select=full");
0
 
Member_2_7971128Author Commented:
Thanks! It works. On click is now perfect. On enter however is now opening an extra window with my home page. Any tips? Thanks.
0
 
OMC2000Commented:
Then I need more details. Post HTML code, where you embedded this JS statement.
0
 
Member_2_7971128Author Commented:
Sure, see attached.
FullCode.txt
0
 
OMC2000Commented:
remove  'target="_blank"' from the form description. This is the reason of the last problem. Form is getting submitted.

And, I guess, you should replace in searchthis ()

document.location = "http://query.nytimes.com/search/query?query=" + document.search_form.search.value + "&date_select=full";

with

window.open("http://query.nytimes.com/search/query?query=" + document.search_form.search.value + "&date_select=full");
0
 
Member_2_7971128Author Commented:
ok, I made those changes, but still same behavior on IE and Firefox. Chrome Ok.
0
 
Member_2_7971128Author Commented:
That did it! Thanks for all the help!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.