www.webdeveloper.com
Results 1 to 6 of 6

Thread: help on a javascript program

  1. #1
    Join Date
    Dec 2012
    Posts
    4

    help on a javascript program

    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+')}
    but it's not working pls help me out.

  2. #2
    Join Date
    Mar 2009
    Posts
    452
    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+')}

  3. #3
    Join Date
    Dec 2012
    Posts
    4

    help on javascript program

    Thanks a lot ZABI . It worked fine for me . Thank you very much for your kind help.

  4. #4
    Join Date
    Dec 2012
    Posts
    4
    Here's one more question can i pass more than two parametres in a function ?? Thanx in advance.

  5. #5
    Join Date
    Mar 2009
    Posts
    452
    yes as many as you like

    here is a good explaination http://javascript.info/tutorial/arguments

  6. #6
    Join Date
    Dec 2012
    Posts
    4
    The site you mentioned, really helped me. Thanx again.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles