Addition to JavaScript to open results in new window

JavaScript code text file is attached.

Right now when clicked, or hit entered the results display on the same window. Would like the results to open in new window or tab.

Thanks!
JSCode.txt
Member_2_7971128Asked:
Who is Participating?
 
Dorababu MConnect With a Mentor Senior Software EngineerCommented:
Have you tried
window.open("http://query.nytimes.com/search/query?query=" + document.search_form.search.value + "&date_select=full");

Open in new window

1
 
Anthony PangilinanConnect With a Mentor DeveloperCommented:
You might also need to pass "_blank" to Dorababu M's solution.

Edit: Nope, "_blank" is the default already. (https://www.w3schools.com/jsref/met_win_open.asp)
0
 
Slick812Commented:
greetings Member_2_7971128, , , If you add an <iframe> to your page then the user can see the New York Times search page on the same page as the search input

the code below may work for you -
<!DOCTYPE html>
<html><head><title>NYT SEARCH</title>
<script>
function searchthis (keypressed)
{
	var keynum

	if(window.event) // IE
		keynum = keypressed.keyCode;
	else if(keypressed.which) // Netscape/Firefox/Opera
		keynum = keypressed.which;

	if (keynum == 13){
		//alert(keynum + " key code");
		if (document.search_form.search.value.length > 2) {
        document.getElementById("iframe1").src = "http://query.nytimes.com/search/query?query=" + encodeURIComponent(document.search_form.search.value) + "&date_select=full";
		}
	else{
		alert( "Please enter the search term longer than 2." );
		document.search_form.search.focus();
		event.returnValue=false;
		}
		event.returnValue=false;
	}
}

function clickthis ()
{
	if (document.search_form.search.value.length > 2) 
		{
		document.getElementById("iframe1").src = "http://query.nytimes.com/search/query?query=" + encodeURIComponent(document.search_form.search.value) + "&date_select=full";
		}
	else
		{
		alert( "Please enter the search term longer than 2." );
		document.search_form.search.focus();
		event.returnValue=false;
		}
		event.returnValue=false;
}
</script>

</head>
<body>
<p>
<form name="search_form" id="search_form" target="_blank">
		<label for="s" style="color:CE0505;font-weight:bold;">Search the New York Times:&nbsp;</label>
		<input  type="text" name="search" id="search" size="15" value="" style="font-size: 12px;" onKeyPress="searchthis(event);">
		<INPUT   TYPE="button" NAME="SButton" VALUE="Go" style="height: 20px; vertical-align: center; font-size: 12px;" onClick="clickthis();"> 
</form>
</p>
<iframe id="iframe1" src="" style="width:99%;height:483px;">
<p>Your browser does not support iframes.</p>
</iframe>


</body>
</html>

Open in new window


I added the  encodeURIComponent( ) to make sure the input text was correct for a "URL" string, which does not allow certain characters
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
Member_2_7971128Author Commented:
Yes I tried window.open, didn't work.

Sorry can not use iframe, that is not a requirement. Will have to find another way.

Thanks.
0
 
Anthony PangilinanDeveloperCommented:
Is there default behavior on your click element that needs preventDefault() called?
0
 
Slick812Commented:
This question has been inactive fo more than 14 days, and will be closed for clean up, Points split for experts comments as a possible solution.
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.