Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can't get this javascript syntax quite right - help!

Status
Not open for further replies.

cbsarge

IS-IT--Management
Jun 20, 2001
219
US
I have been working on this forever and can't get it to work quite right. The start page has the user check up to 4 search engines and then submit a search term to all checked ones. The next page has 2 or 4 iframes that display the search results. There is a pair of buttons above each to open and close the iframe but, I can't get that function to work. I've used the same code in other pages successfully but, this is the first time I've tried to use javascript to draw the page with the open/close iframe function.

I'd really rather just have a search box that by default displays the search results on the SAME page in the iframes. Then I could use my working open/close iframes code.

Here is what I have so far - help/suggestions are welcome
Code:
<html>
<head>
<script type="text/javascript">
function show(which) {
var hide = new Array();
hide = document.getElementsByName(which);
for (i=0; i<hide.length; i++) {
hide[i].style.display = 'inline';
}
}
function hide(which) {
var hide = new Array();
hide = document.getElementsByName(which);
for (i=0; i<hide.length; i++) {
hide[i].style.display = 'none';
}
}
// End  -->
</script>
<script type="text/JavaScript">
<!-- Begin
Yahoo = "[URL unfurl="true"]http://search.yahoo.com/bin/search?p=";[/URL]
Google = "[URL unfurl="true"]http://www.google.com/search?q=";[/URL]
MSN = "[URL unfurl="true"]http://search.msn.com/results.aspx?q=";[/URL]
AltaVista = "[URL unfurl="true"]http://www.altavista.com/web/results?pg=q&q=";[/URL]
var got=0;
var url = "";
var plus="";
var mag="";
function search4(item){
stringPlus();
resultsWindow=window.open();
resultsWindow.document.open();
resultsWindow.document.write("<head><title>Close this window "
+ "to return to Search Page</title>"
+ "<script>"
+ "function show(which) {"
+ "var hide = new Array();"
+ "hide = document.getElementsByName(which);"
+ "for (i=0; i<hide.length; i++) {"
+ "hide[i].style.display = 'inline';"
+ "}"
+ "}"
+ "function hide(which) {"
+ "var hide = new Array();"
+ "hide = document.getElementsByName(which);"
+ "for (i=0; i<hide.length; i++) {"
+ "hide[i].style.display = 'none';"
+ "}"
+ "}"
+ "</script></head>"
+ "<body>"
+ "<div>"
+ "<td style='padding:0px;margin:0px;' align='center' width='200px' valign='top' nowrap='nowrap'>"
+ "<input type='button' value='Show' onclick='show('a');'>"
+ "<input type='button' value='Hide' onclick='hide('a');'></td>"
+ "<td style='padding:0px;margin:0px;' align='left' width='200px' valign='top' nowrap='nowrap'>"
+ "<iFRAME NAME='a' WIDTH=1024 HEIGHT=400 SRC='" + computeFrameSrc(0) + "'></iframe></td>"
+ "<td style='padding:0px;margin:0px;' align='center' width='200px' valign='top' nowrap='nowrap'>"
+ "<input type='button' value='Show' onclick='show('b');'>"
+ "<input type='button' value='Hide' onclick='hide('b');'></td>"
+ "<td style='padding:0px;margin:0px;' align='left' width='200px' valign='top' nowrap='nowrap'>"
+ "<iFRAME NAME='b' WIDTH=1024 HEIGHT=400 SRC='" + computeFrameSrc(1) + "'></iframe></td>"
+ "<td style='padding:0px;margin:0px;' align='center' width='200px' valign='top' nowrap='nowrap'>"
+ "<input type='button' value='Show' onclick='show('c');'>"
+ "<input type='button' value='Hide' onclick='hide('c');'></td>"
+ "<td style='padding:0px;margin:0px;' align='left' width='200px' valign='top' nowrap='nowrap'>"
+ "<iFRAME NAME='c' WIDTH=1024 HEIGHT=400 SRC='" + computeFrameSrc(2) + "'></iframe></td>"
+ "<td style='padding:0px;margin:0px;' align='center' width='200px' valign='top' nowrap='nowrap'>"
+ "<input type='button' value='Show' onclick='show('d');'>"
+ "<input type='button' value='Hide' onclick='hide('d');'></td>"
+ "<td style='padding:0px;margin:0px;' align='left' width='200px' valign='top' nowrap='nowrap'>"
+ "<iFRAME NAME='d' WIDTH=1024 HEIGHT=400 SRC='" + computeFrameSrc(3) + "'></iframe></td>"
+ "</body>");
resultsWindow.document.close();
got = 0;
}
function search2(item){
stringPlus();
resultsWindow=window.open();
resultsWindow.document.open();
resultsWindow.document.write("<head><title>Close this window "
+ "to return to Search Page</title>"
+ "<script>"
+ "function show(which) {"
+ "var hide = new Array();"
+ "hide = document.getElementsByName(which);"
+ "for (i=0; i<hide.length; i++) {"
+ "hide[i].style.display = 'inline';"
+ "}"
+ "}"
+ "function hide(which) {"
+ "var hide = new Array();"
+ "hide = document.getElementsByName(which);"
+ "for (i=0; i<hide.length; i++) {"
+ "hide[i].style.display = 'none';"
+ "}"
+ "}"
+ "</script>"
+ "</head>"
+ "<body>"
+ "<div>"
+ "<td style='padding:0px;margin:0px;' align='center' width='200px' valign='top' nowrap='nowrap'>"
+ "<input type='button' value='Show' onclick='show('a');'>"
+ "<input type='button' value='Hide' onclick='hide('a');'></td>"
+ "<td style='padding:0px;margin:0px;' align='left' width='200px' valign='top' nowrap='nowrap'>"
+ "<iFRAME NAME='a' WIDTH=1024 HEIGHT=400 SRC='" + computeFrameSrc(0) + "'></iframe></td>"
+ "<td style='padding:0px;margin:0px;' align='center' width='200px' valign='top' nowrap='nowrap'>"
+ "<input type='button' value='Show' onclick='show('b');'>"
+ "<input type='button' value='Hide' onclick='hide('b');'></td>"
+ "<td style='padding:0px;margin:0px;' align='left' width='200px' valign='top' nowrap='nowrap'>"
+ "<iFRAME NAME='b' WIDTH=1024 HEIGHT=400 SRC='" + computeFrameSrc(1) + "'></iframe></td>"
+ "</body>");
resultsWindow.document.close();
got = 0;
}
function stringPlus() {
for (var j=0; j < window.document.choose4.text.value.length; j++) {
if (window.document.choose4.text.value.charAt(j) == " ")
mag += "+";
else mag += window.document.choose4.text.value.charAt(j);}
}
function numChecked(item) {
plus = escape(item.text.value);
var h=0;
num=0;
for (var l=0; l < item.check1.length; l++) {
if (item.check1[l].checked) {
h++;
if (h++ <= 4){
if (l == "0"){
num = 1;}
else if (l == "1") num = 2;
else if (l == "2") num = 3;
else if (l == "3") num = 4;
else if (l == "4") num = 5;
      }
   }
}
if (h == 2)
Results(num-1);
else if (h == 4)
search2(item);
else
search4(item);
}
function computeFrameSrc(num) {
var k=-1;
for (var j=got; j < document.choose4.check1.length; j++) {
if (document.choose4.check1[j].checked){
k++;
if (k++ <= num){
if (j == "0"){
url = Yahoo + plus;
got = 1;}
else if (j == "1"){
url = AltaVista + plus;
got = 2;}
else if (j == "2"){
url = MSN + plus;
got = 3;}
else if (j == "3"){
url = Google + plus;
got = 4;}
return url;
      }
   }
}
url = "javascript:void(0)";
return url;
}
function Results(place){
stringPlus();
resultsWin = window.open("","results");
if (place == "0")
url = Yahoo + plus;
else if (place == "1")
url = AltaVista + plus;
else if (place == "2")
url = MSN + plus;
else if (place == "3")
url = Google + plus + "&hits=25&disp=Text+Only";
resultsWin.location = url;
}
// End  -->
</script>
</head>
<td>
<table>
<tbody>
<tr>
<center>
<form name="choose4" action="javascript:numChecked(document.choose4) //"><p>
<table><tr><td align="left"><b>Search Engines</b><br>
<input type="checkbox" name="check1" value="Yahoo" onClick="choose4.check1.value='Yahoo'">Yahoo<br>
<input type="checkbox" name="check1" value="AltaVista" onClick="choose4.check1.value='AltaVista'">AltaVista<br>
<input type="checkbox" name="check1" value="SearchCom" onClick="choose4.check1.value='SearchCom'">MSN<BR>
<input type="checkbox" name="check1" value="Google" onClick="choose4.check1.value='Google'">Google<br>
</td>
</tr>
</table>
<P>
<B>Enter text to find</B>:<BR>
<input type="text" name="text" size=20><P>
<input type="submit" name="send" value="Search">
</form>
</center>
</tr>
</tbody>
</table>
</td>
</html>

[!]The AutoSavers![/!] [2thumbsup]
 
Ack! Just messy code from monkeying around with it so much. I've got a less-messy version on one of my machines that I haven't moved things around in so much. This is a little cleaner (although probably still messy!):
Code:
<html>
<head>
<script type="text/javascript">
function show(which) {
var hide = new Array();
hide = document.getElementsByName(which);
for (i=0; i<hide.length; i++) {
hide[i].style.display = 'inline';
}
}
function hide(which) {
var hide = new Array();
hide = document.getElementsByName(which);
for (i=0; i<hide.length; i++) {
hide[i].style.display = 'none';
}
}
// End  -->
</script>
<script type="text/JavaScript">
<!-- Begin
Yahoo = "[URL unfurl="true"]http://search.yahoo.com/bin/search?p=";[/URL]
Google = "[URL unfurl="true"]http://www.google.com/search?q=";[/URL]
MSN = "[URL unfurl="true"]http://search.msn.com/results.aspx?q=";[/URL]
AltaVista = "[URL unfurl="true"]http://www.altavista.com/web/results?pg=q&q=";[/URL]
var got=0;
var url = "";
var plus="";
var mag="";
function search4(item){
stringPlus();
resultsWindow=window.open();
resultsWindow.document.open();
resultsWindow.document.write("<head><title>Close this window "
+ "to return to Search Page</title>"
+ "<script>"
+ "function show(which) {"
+ "var hide = new Array();"
+ "hide = document.getElementsByName(which);"
+ "for (i=0; i<hide.length; i++) {"
+ "hide[i].style.display = 'inline';"
+ "}"
+ "}"
+ "function hide(which) {"
+ "var hide = new Array();"
+ "hide = document.getElementsByName(which);"
+ "for (i=0; i<hide.length; i++) {"
+ "hide[i].style.display = 'none';"
+ "}"
+ "}"
+ "</script></head>"
+ "<body>"
+ "<div>"
+ "<td style='padding:0px;margin:0px;' align='center' width='200px' valign='top' nowrap='nowrap'>"
+ "<input type='button' value='Show' onclick='show('a');'>"
+ "<input type='button' value='Hide' onclick='hide('a');'></td>"
+ "<td style='padding:0px;margin:0px;' align='left' width='200px' valign='top' nowrap='nowrap'>"
+ "<iFRAME NAME='a' WIDTH=1024 HEIGHT=400 SRC='" + computeFrameSrc(0) + "'></iframe></td>"
+ "<td style='padding:0px;margin:0px;' align='center' width='200px' valign='top' nowrap='nowrap'>"
+ "<input type='button' value='Show' onclick='show('b');'>"
+ "<input type='button' value='Hide' onclick='hide('b');'></td>"
+ "<td style='padding:0px;margin:0px;' align='left' width='200px' valign='top' nowrap='nowrap'>"
+ "<iFRAME NAME='b' WIDTH=1024 HEIGHT=400 SRC='" + computeFrameSrc(1) + "'></iframe></td>"
+ "<td style='padding:0px;margin:0px;' align='center' width='200px' valign='top' nowrap='nowrap'>"
+ "<input type='button' value='Show' onclick='show('c');'>"
+ "<input type='button' value='Hide' onclick='hide('c');'></td>"
+ "<td style='padding:0px;margin:0px;' align='left' width='200px' valign='top' nowrap='nowrap'>"
+ "<iFRAME NAME='c' WIDTH=1024 HEIGHT=400 SRC='" + computeFrameSrc(2) + "'></iframe></td>"
+ "<td style='padding:0px;margin:0px;' align='center' width='200px' valign='top' nowrap='nowrap'>"
+ "<input type='button' value='Show' onclick='show('d');'>"
+ "<input type='button' value='Hide' onclick='hide('d');'></td>"
+ "<td style='padding:0px;margin:0px;' align='left' width='200px' valign='top' nowrap='nowrap'>"
+ "<iFRAME NAME='d' WIDTH=1024 HEIGHT=400 SRC='" + computeFrameSrc(3) + "'></iframe></td>"
+ "</body>");
resultsWindow.document.close();
got = 0;
}
function search2(item){
stringPlus();
resultsWindow=window.open();
resultsWindow.document.open();
resultsWindow.document.write("<head><title>Close this window "
+ "to return to Search Page</title>"
+ "<script>"
+ "function show(which) {"
+ "var hide = new Array();"
+ "hide = document.getElementsByName(which);"
+ "for (i=0; i<hide.length; i++) {"
+ "hide[i].style.display = 'inline';"
+ "}"
+ "}"
+ "function hide(which) {"
+ "var hide = new Array();"
+ "hide = document.getElementsByName(which);"
+ "for (i=0; i<hide.length; i++) {"
+ "hide[i].style.display = 'none';"
+ "}"
+ "}"
+ "</script>"
+ "</head>"
+ "<body>"
+ "<div>"
+ "<td style='padding:0px;margin:0px;' align='center' width='200px' valign='top' nowrap='nowrap'>"
+ "<input type='button' value='Show' onclick='show('a');'>"
+ "<input type='button' value='Hide' onclick='hide('a');'></td>"
+ "<td style='padding:0px;margin:0px;' align='left' width='200px' valign='top' nowrap='nowrap'>"
+ "<iFRAME NAME='a' WIDTH=1024 HEIGHT=400 SRC='" + computeFrameSrc(0) + "'></iframe></td>"
+ "<td style='padding:0px;margin:0px;' align='center' width='200px' valign='top' nowrap='nowrap'>"
+ "<input type='button' value='Show' onclick='show('b');'>"
+ "<input type='button' value='Hide' onclick='hide('b');'></td>"
+ "<td style='padding:0px;margin:0px;' align='left' width='200px' valign='top' nowrap='nowrap'>"
+ "<iFRAME NAME='b' WIDTH=1024 HEIGHT=400 SRC='" + computeFrameSrc(1) + "'></iframe></td>"
+ "</body>");
resultsWindow.document.close();
got = 0;
}
function stringPlus() {
for (var j=0; j < window.document.choose4.text.value.length; j++) {
if (window.document.choose4.text.value.charAt(j) == " ")
mag += "+";
else mag += window.document.choose4.text.value.charAt(j);}
}
function numChecked(item) {
plus = escape(item.text.value);
var h=0;
num=0;
for (var l=0; l < item.check1.length; l++) {
if (item.check1[l].checked) {
h++;
if (h++ <= 4){
if (l == "0"){
num = 1;}
else if (l == "1") num = 2;
else if (l == "2") num = 3;
else if (l == "3") num = 4;
else if (l == "4") num = 5;
      }
   }
}
if (h == 2)
Results(num-1);
else if (h == 4)
search2(item);
else
search4(item);
}
function computeFrameSrc(num) {
var k=-1;
for (var j=got; j < document.choose4.check1.length; j++) {
if (document.choose4.check1[j].checked){
k++;
if (k++ <= num){
if (j == "0"){
url = Yahoo + plus;
got = 1;}
else if (j == "1"){
url = AltaVista + plus;
got = 2;}
else if (j == "2"){
url = MSN + plus;
got = 3;}
else if (j == "3"){
url = Google + plus;
got = 4;}
return url;
      }
   }
}
url = "javascript:void(0)";
return url;
}
function Results(place){
stringPlus();
resultsWin = window.open("","results");
if (place == "0")
url = Yahoo + plus;
else if (place == "1")
url = AltaVista + plus;
else if (place == "2")
url = MSN + plus;
else if (place == "3")
url = Google + plus + "&hits=25&disp=Text+Only";
resultsWin.location = url;
}
// End  -->
</script>
</head>
<table>
<tbody>
<tr>
<center>
<form name="choose4" action="javascript:numChecked(document.choose4) //"><p>
<table><tr><td align="left"><b>Search Engines</b><br>
<input type="checkbox" name="check1" value="Yahoo" onClick="choose4.check1.value='Yahoo'">Yahoo<br>
<input type="checkbox" name="check1" value="AltaVista" onClick="choose4.check1.value='AltaVista'">AltaVista<br>
<input type="checkbox" name="check1" value="SearchCom" onClick="choose4.check1.value='SearchCom'">MSN<BR>
<input type="checkbox" name="check1" value="Google" onClick="choose4.check1.value='Google'">Google<br>
</td>
</tr>
</table>
<P>
<B>Enter text to find</B>:<BR>
<input type="text" name="text" size=20><P>
<input type="submit" name="send" value="Search">
</form>
</center>
</tr>
</tbody>
</table>
</html>

[!]The AutoSavers![/!] [2thumbsup]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top