Link to home
Start Free TrialLog in
Avatar of Member_2_7971128
Member_2_7971128

asked on

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.
Avatar of OMC2000
OMC2000
Flag of Russian Federation image

Could you post an example of your window.open() statement. It works in general. Probably your browser doesn't allow popup windows?
Avatar of Member_2_7971128
Member_2_7971128

ASKER

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");
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?
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.
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");
Thanks! It works. On click is now perfect. On enter however is now opening an extra window with my home page. Any tips? Thanks.
Then I need more details. Post HTML code, where you embedded this JS statement.
Sure, see attached.
FullCode.txt
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");
ok, I made those changes, but still same behavior on IE and Firefox. Chrome Ok.
ASKER CERTIFIED SOLUTION
Avatar of OMC2000
OMC2000
Flag of Russian Federation image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That did it! Thanks for all the help!