[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

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

Posted on 2018-02-09
12
Low Priority
?
33 Views
Last Modified: 2018-02-13
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.
0
Comment
Question by:Member_2_7971128
  • 6
  • 6
12 Comments
 
LVL 15

Expert Comment

by:OMC2000
ID: 42463753
Could you post an example of your window.open() statement. It works in general. Probably your browser doesn't allow popup windows?
0
 

Author Comment

by:Member_2_7971128
ID: 42463800
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
 
LVL 15

Expert Comment

by:OMC2000
ID: 42463809
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 

Author Comment

by:Member_2_7971128
ID: 42463823
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
 
LVL 15

Expert Comment

by:OMC2000
ID: 42463857
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
 

Author Comment

by:Member_2_7971128
ID: 42464011
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
 
LVL 15

Expert Comment

by:OMC2000
Then I need more details. Post HTML code, where you embedded this JS statement.
0
 

Author Comment

by:Member_2_7971128
Sure, see attached.
FullCode.txt
0
 
LVL 15

Expert Comment

by:OMC2000
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
 

Author Comment

by:Member_2_7971128
ok, I made those changes, but still same behavior on IE and Firefox. Chrome Ok.
0
 
LVL 15

Accepted Solution

by:
OMC2000 earned 1000 total points
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
 

Author Closing Comment

by:Member_2_7971128
That did it! Thanks for all the help!
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Join & Write a Comment

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
There's never been a better time to become a computer scientist. Employment growth in the field is expected to reach 22% overall by 2020, and if you want to get in on the action, it’s a good idea to think about at least minoring in computer science …
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

591 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question