PDA

View Full Version : Help With Javascript.



XghostriderX
10-05-2003, 12:56 PM
This for all you script-guru's:

Yesterday I was trying to make a simple Jscript for my brother's homepage that allows to use different searchengines to search the web, but I was surprise when the script didn't work with Explorer, while it did with Opera 7.2.

Now here is what I wrote:


<HEAD>
&#60;script type="text/javascript" >
function goToSearch()
{
&nbsp; &nbsp; if (window.event.keyCode == 13)
&nbsp; &nbsp; {&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Search()
&nbsp; &nbsp; &nbsp; }

}
function Search()
{
&nbsp; &nbsp; term = window.document.searchform.querry.value ;
&nbsp; &nbsp; location.href ="http://www.google.nl/search?q=" + term

}
</script >
</head>
<body>
<FORM name="searchform">
<input type=&#39;text&#39; size=&#39;20&#39; name=&#39;querry&#39;&nbsp; onKeyPress="goToSearch()">
<input type=&#39;button&#39; size=&#39;10&#39; value=&#39;Go&#39; onclick="Search()">
</FORM>
bla blas
bla bla
.
.
.
.
</body>

did I do somthing wrong? Oh yeah I forgot the script works fine in Explorer & Opera in case I use the button instead of the Enter-key, but that just annoying. I mean everyone is use the type somethng in the the textfield and than hit the enter-key .
So the real problem is how to get this script to work with a the Enter-key?.
I tried onSumbit="", but that didn&#39;t work(It was a dumb move I know <_< ). I also tried to copy the body of the second function to the first one, but that also didn&#39;t help.
One thing that did work for all browers was the open commad, if I use window.open() instead of loction.href="", I get no problems at all except that my brother doesn&#39;t want that.
So any help would be aprceiated guys.



PS: I apologize if this the wrong place to post such a problem.

[B][O][T]
10-05-2003, 01:08 PM
Try this:


&#60;script type=&#34;text/javascript&#34; &#62;
function goToSearch&#40;&#41;
{
if &#40;window.event.keyCode == 13&#41;
{
Search&#40;&#41;
}

}
function Search&#40;&#41;
{
term = window.document.searchform.querry.value;
location.href =&#34;http&#58;//www.google.nl/search?q=&#34; + term

}
&#60;/script&#62;
&#60;body&#62;
&#60;FORM name=&#34;searchform&#34;&#62;
&#60;input type=&#39;text&#39; size=&#39;20&#39; name=&#39;querry&#39; onKeyPress=&#34;goToSearch&#40;&#41;&#34;&#62;
&#60;input type=&#39;button&#39; size=&#39;10&#39; value=&#39;Go&#39; onclick=&#34;Search&#40;&#41;&#34;&#62;
&#60;/FORM&#62;
bla blas
bla bla
.
.
.
.
&#60;/body&#62;
&#60;/script&#62;
&#60;/html&#62;

BOT

XghostriderX
10-05-2003, 01:33 PM
Hi Bot&#33;

You trick didn&#39;t work&#33;
May i ask why did you add this tag </script> after the body tag? were you trying to put the form inside the script?

Anyway thanx for yoor help.

[B][O][T]
10-05-2003, 06:42 PM
Sorry My fault, this code work:


&#60;HTML&#62;
&#60;HEAD&#62;

&#60;/HEAD&#62;

&#60;script type=&#34;text/javascript&#34; &#62;
function goToSearch&#40;&#41;
{
&nbsp; &nbsp;if &#40;window.event.keyCode == 13&#41;
&nbsp; &nbsp;{ &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Search&#40;&#41;
&nbsp; &nbsp; &nbsp;}

}
function Search&#40;&#41;
{
&nbsp; &nbsp;term = window.document.searchform.querry.value&#59;
&nbsp; &nbsp;location.href =&#34;http&#58;//www.google.nl/search?q=&#34; + term

}
&#60;/script&#62;
&#60;body&#62;
&#60;FORM name=&#34;searchform&#34;&#62;
&#60;input type=&#39;text&#39; size=&#39;20&#39; name=&#39;querry&#39; &nbsp;onKeyPress=&#34;goToSearch&#40;&#41;&#34;&#62;
&#60;input type=&#39;button&#39; size=&#39;10&#39; value=&#39;Google&#39; onclick=&#34;Search&#40;&#41;&#34;&#62;
&#60;/FORM&#62;
bla
bla
&#60;/body&#62;
&#60;/HTML&#62;

BOT

VB
10-05-2003, 06:48 PM
Better code would be:


&#60;script type=&#34;text/javascript&#34;&#62;
function search&#40;&#41;
{
term = window.document.searchform.query.value;
location.href =&#34;http&#58;//www.google.nl/search?q=&#34; + term

}
&#60;/script&#62;
&#60;body&#62;
&#60;FORM name=&#34;searchform&#34; action=&#34;search&#40;&#41;;&#34;&#62;
&#60;input type=&#34;text&#34; size=&#34;20&#34; name=&#34;query&#34;&#62;
&#60;input type=&#34;button&#34; size=&#34;10&#34; value=&#34;Go&#34;&#62;
&#60;/FORM&#62;&#60;/body&#62;
&#60;/script&#62;
&#60;/html&#62;

XghostriderX
10-05-2003, 07:11 PM
Hi Bot&#33;

The code you posted is the same as my code. So let explain the problem agian.

What I wanna do is envoke the script by hitting the ENTER-key, not with clicking a button. is that possible? [ I&#39;m no scriptkiddy, I usually get my cripts from internet, or just copy them from other poeple&#39;s pages.]

And Paul thanx for your reply, but I tried that code before and it works only with Opera [it&#39;s a shame that opera is not popular as IE].

VB
10-05-2003, 07:58 PM
This works 100%:


&#60;html&#62;&#60;body&#62;&#60;script type=&#34;text/javascript&#34;&#62;
function search&#40;&#41;
{
term = document.searchform.query.value;
location.href =&#34;http&#58;//www.google.nl/search?q=&#34; + term

}
&#60;/script&#62;
&#60;body&#62;
&#60;FORM name=&#34;searchform&#34; action=&#34;javascript&#58;search&#40;&#41;;&#34;&#62;
&#60;input type=&#34;text&#34; size=&#34;20&#34; name=&#34;query&#34;&#62;
&#60;input type=&#34;submit&#34; value=&#34;Go&#34;&#62;
&#60;/FORM&#62;
&#60;/body&#62;
&#60;/html&#62;

3rd gen noob
10-05-2003, 08:02 PM
great piece of code, Paul :D

XghostriderX
10-05-2003, 08:49 PM
Thanx a lot Pual&#33; You the MAN.
I couldn&#39;t say it better than 3rd gen noob&#33;

Nice avatar bij the way&#33;&#33;&#33;&#33;&#33;&#33;&#33; Where did you get it? hahahah :P

Wizzandabe
10-05-2003, 08:54 PM
Wooh, Nice. :D

XghostriderX
10-05-2003, 11:24 PM
wooooh&#33;
those avatar keep getting better and better&#33;

Nice Wizzandabe