I want to make a command line interface with javascript. So that it can search google , wiki & google image simultaneously . Here's the syntax i want to use for google search g_search term. I made a form with a input field with id= text1 . I gave the form an onSubmit=return main() event. And here's the java script in external js file.
Code:
function main(){var inputdata=document.getElementById('text1').value;
var part=inputdata.split('_');
var cmd=part[0];
var arg=part[1];
var enarg=encodeURIComponent(arg);
run();return false;}
function run(){
if(cmd=='g'){g();};
if(cmd=='gi'){gi();};
if(cmd=='wiki'){w();};
if(cmd=='al'){al();};}
function g(){location.href='http://google.com/search/?q='+enarg}
function gi(){location.href='http://googld.com/images/?q='+enarg}
function w(){location.href='http://en.wikipedia.org/wiki/speacial:search/?search='+enarg}
function al(){alert('+arg+')}
var keyword is used to declare a local variable within a function. that means you are using var cmd in main() function, so it is only accessible in main() function only; same is the case with var enarg for more info about javascript variable scope you can go to w3schools.com
you can get it working by just removing var before cmd and enarg, but i would recommend passing those values to run() function as parameters /arguments like this:
Code:
function main(){var inputdata=document.getElementById('text1').value;
var part=inputdata.split('_');
var cmd=part[0];
var arg=part[1];
var enarg=encodeURIComponent(arg);
run(cmd,enarg);return false;}
function run(cmd,enarg){
if(cmd=='g'){g();};
if(cmd=='gi'){gi();};
if(cmd=='wiki'){w();};
if(cmd=='al'){al();};}
function g(){location.href='http://google.com/search/?q='+enarg}
function gi(){location.href='http://googld.com/images/?q='+enarg}
function w(){location.href='http://en.wikipedia.org/wiki/speacial:search/?search='+enarg}
function al(){alert('+arg+')}
Bookmarks