Link to home
Start Free TrialLog in
Avatar of Member_2_7971128
Member_2_7971128

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of Dorababu M
Dorababu M
Flag of India 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
SOLUTION
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
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
Avatar of Member_2_7971128
Member_2_7971128

ASKER

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.
Is there default behavior on your click element that needs preventDefault() called?
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.