Solved

Running a Javacript if Another Script Returns True

Posted on 2007-11-26
16
258 Views
Last Modified: 2011-09-20
I have a web form where I'd to like combine 2 different javascripts where the second runs only if the first returns true (out of true or false)  the first one is activated by

onSubmit="return checkSubmit()"

and runs the following script:

function checkSubmit()
{      
      if(frmCOShipping.AlreadyPosted.value=="NO")
      {                        
            frmCOShipping.AlreadyPosted.value="YES"
            return true;
      }
      else
      {
            return false;
      }      
}


The Second is activated by

onSubmit="runPrintJob(this)"

and runs the following script

function runPrintJob() {

  var shell = new ActiveXObject("WScript.shell");
  shell.run("S:/Scripts/PrintCatadyneSerialNumberLabel.bat "+frmCOShipping.Item.value+" "+frmCOShipping.Serial.value);
}


Right now,

onsubmit="if(checkSubmit() == true) {runPrintJob(this)}">

Runs the second as long as the first returns either true of false, how do i change it to run runPrintJob only if the first one does not return a false on frmCOShipping.AlreadyPosted?
0
Comment
Question by:deltex141
  • 4
  • 4
  • 3
  • +2
16 Comments
 
LVL 29

Accepted Solution

by:
Badotz earned 250 total points
ID: 20352207
Run the 2nd function from the 1st:

function checkSubmit()
{      
      if(frmCOShipping.AlreadyPosted.value=="NO")
      {                        
            frmCOShipping.AlreadyPosted.value="YES"
           
            runPrintJob();
             
            return true;
      }
      else
      {
            return false;
      }      
}
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 20371379
If you can the method Badotz suggested is the best way to do this.  However your script looks good too.  You could try using just ...

onsubmit="if(checkSubmit()) {runPrintJob(this)}">

... but that should only do runPrintJob if it is true.  I would suggest the problem is some other error or your If in checkSubmit isn't working the way you expect.  Add some alerts to troubleshoot it.  Let me know if you need some suggestions of how to do it but I'll probably need more of the script.  The basic idea of what you are doing is good though and I can't see an error (or reason for runPrintJob to be run both times) in the snippets you did provide.

Let me know if you have a question or need more info.

bol
0
 
LVL 75

Assisted Solution

by:Michel Plungjan
Michel Plungjan earned 250 total points
ID: 20375311
So if Badotz suggests works for you, I'd say points to him
However do you need the form to submit at all or is it just a placeholder for the fields needed for the run?


function runPrintJob(theForm) {
  if (theForm.AlreadyPosted.value=="YES") {
    alert('Already running')
    return false
  }
  theForm.AlreadyPosted.value="YES"

  var shell = new ActiveXObject("WScript.shell");
  shell.run("S:/Scripts/PrintCatadyneSerialNumberLabel.bat "+theForm.Item.value+" "+theForm.Serial.value);
  return true; // submits the form - return false here to just run the shell.run
}

and have


onSubmit="return runPrintJob(this)"
0
 
LVL 16

Expert Comment

by:LeeKowalkowski
ID: 20567859
onsubmit="if(checkSubmit()) {runPrintJob(this)}" doesn't return anything, therefore the submit event is not blocked.  It needs to be onsubmit="if(checkSubmit()) runPrintJob(this); else return false;"

--
Lee
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 20571544
As i already pointed out in my post http:#20375311
0
 
LVL 16

Expert Comment

by:LeeKowalkowski
ID: 20571888
You did?

Nobody said onsubmit="if(checkSubmit()) {runPrintJob(this)}" doesnt return anything.  This is why I think the author thinks it "Runs the second as long as the first returns either true [or] false" - because the form will be submitted unless onsubmit returns false.

Nobody suggested onsubmit="if(checkSubmit()) runPrintJob(this); else return false;" before, and I was only suggesting it as a correction to http:#20371379.

As it stands, I prefer Badotz's solution as I think it minimizes the code in an onsubmit attribute ("return checkSubmit()") - If you're going to use inline event handlers, it's absolutely a good idea to limit them to a single function call to keep the implementation contained.

I was only explaining why the author's attempt at onsubmit="if(checkSubmit() == true) {runPrintJob(this)}" would not work for them.

It looks to me that the author would like the form to be submitted, because checkSubmit() only prevents a double-click submission from what I can tell, but you never know...

--
Lee
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 20572025
yes I did:

 return true; // submits the form - return false here to just run the shell.run
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 16

Expert Comment

by:LeeKowalkowski
ID: 20572044
Yes, you said that, but that is not what I was saying!

--
Lee
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 20576969
Please post and explain why you closed this question as you did.  The B grade makes me wonder if you got the solution or answer.  However there were a number of good posts with info to help.  Also you never commented so it seems odd that you would give a B grade.  Please respond.

If you'd like info on what the grades mean and some guidelines on grading then look at http://www.experts-exchange.com/help.jsp#hi97 .  Your grading history seems unusually harsh.  I hope it doesn't really reflect the help you have received here since it isn't typical.  I'm glad we could help but the closing of this needs an explanation.  If it was an accident then let me know and I can help fix it.

b0lsc0tt
EE Zone Advisor
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 20577010
And a split was definitely in order here
0
 
LVL 29

Expert Comment

by:Badotz
ID: 20577225
Grade B? Was I bad?
0
 
LVL 29

Expert Comment

by:Badotz
ID: 20609554
Since deltex141 does not care to comment, I say award the points as you see fit, modus_operandi.

Better yet, delete the question but do *not* refund the points.
0
 
LVL 4

Author Closing Comment

by:deltex141
ID: 31411051
The split was in order since it was a placeholder as mplugian suggested. It was christmas time, relax guys. Badotz, the suggestion to delete the question was NOT appreciated, calm down.
0
 
LVL 29

Expert Comment

by:Badotz
ID: 20610177
Nice that the Asker finally responded:

"The split was in order since it was a placeholder as mplugian suggested. It was christmas time, relax guys. Badotz, the suggestion to delete the question was NOT appreciated, calm down."
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now