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.
Could you post an example of your window.open() statement. It works in general. Probably your browser doesn't allow popup windows?
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.searc h.value + "&date_select=full");
document.location = window.open("http://query.nytimes.com/search/query?query=" + document.search_form.searc
document.location is a string containing URL
window.open("http://query.nytimes.com/search/query?query=" + document.search_form.searc h.value + "&date_select=full");
returns window object.
So, statement
document.location = window.open("http://query.nytimes.com/search/query?query=" + document.search_form.searc h.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?
window.open("http://query.nytimes.com/search/query?query=" + document.search_form.searc
returns window object.
So, statement
document.location = window.open("http://query.nytimes.com/search/query?query=" + document.search_form.searc
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?
ASKER
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.
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.searc h.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.searc h.value + "&date_select=full");
instead of
document.location = window.open("http://query.nytimes.com/search/query?query=" + document.search_form.searc h.value + "&date_select=full");
window.open("http://query.nytimes.com/search/query?query=" + document.search_form.searc
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.searc
instead of
document.location = window.open("http://query.nytimes.com/search/query?query=" + document.search_form.searc
ASKER
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.
ASKER
Sure, see attached.
FullCode.txt
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.searc h.value + "&date_select=full";
with
window.open("http://query.nytimes.com/search/query?query=" + document.search_form.searc h.value + "&date_select=full");
And, I guess, you should replace in searchthis ()
document.location = "http://query.nytimes.com/search/query?query=" + document.search_form.searc
with
window.open("http://query.nytimes.com/search/query?query=" + document.search_form.searc
ASKER
ok, I made those changes, but still same behavior on IE and Firefox. Chrome Ok.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That did it! Thanks for all the help!