Javascript code to detect which button is pressed

A VBScript is linked into my page in a script tag. When ever the page is refreshed, IE show pops up message with confirm function(Yes / No buttons).  Is there a way to detect (react) from (within) JavaScript code which button was selected by user, and do a function
depending on which button is pressed: if 'No'- show alert, then call script again; if 'Yes' pressed -no action.
SiroccoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

smaccariCommented:
Just do this: (the confirm function return True if yes pressed, false othewise)

if (confirm("Are you sure?")) {
//YES PRESSED
}
else {
//NO PRESSED
}
0
SiroccoAuthor Commented:
How this should be intergrated in page?

<html>
<head>
<title>Demo</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function init() {
alert('Count 3');
alert('Count 2');
alert('Count 1');
alert('News time!');
}

window.onload = init;
</script>
<script language="VBScript">
Dim fso, NewsFile

Set fso = CreateObject("Scripting.FileSystemObject")
Set NewsFile = fso.CreateTextFile("c:\ezine\newsletter12.txt", True)
NewsFile.WriteLine("Ezine 13 Available Jan 11th")
NewsFile.Write ("Next week will be about creating user accounts")
NewsFile.Write (" More on same line")
NewsFile.Close
</script>
<script language="javascript">

 if (confirm("Are you sure?")) {
//YES PRESSED
}
else {
//NO PRESSED
}
</script>
</head>

<body>
<p>Demo</p>
</body>
</html>
0
smaccariCommented:
Am not sure what you want to do exactly.
Can you explain a bit more?
When you say "if 'No'- show alert, then call script again", which script are you talking about? Your VBScript that creates a text file?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Michel PlungjanIT ExpertCommented:
You mean change
function init() {
alert('Count 3');
alert('Count 2');
alert('Count 1');
alert('News time!');
}

to

function init() {
  if (confirm('Count 3'))
    if (confirm('Count 2'))
      if (confirm('Count 1'))
        alert('News time!');
}
0
SiroccoAuthor Commented:
Code sequence is as follows:
first need be shown 4 alerts;
then run VBScript which causes a IE pop up message with activex confirm function;
Javascript need detect which button was selected by user, if 'No'(Cancel)- show alert, then call VBScript again(loop); if 'Yes'(Ok) pressed - no action.
0
SiroccoAuthor Commented:
The code sample is draft, I need correct and adjust what required to get necessary functionality. When you saying "if 'No'- show alert, then call script again", I mean call VBScript only(looping). Javascript must call(loop) VBScript until the correct button("Yes") will not be pushed.
0
smaccariCommented:
Ow that's not the same then..

Try this:

<script type="text/javascript">
function init() {
alert('Count 3');
alert('Count 2');
alert('Count 1');
alert('News time!');
GoWrite();
return false;
}
window.onload = init;
window.onerror=init;
</script>

<script language="VBScript">
Sub GoWrite
Dim fso, NewsFile

Set fso = CreateObject("Scripting.FileSystemObject")
Set NewsFile = fso.CreateTextFile("c:\ezine\newsletter12.txt", True)
NewsFile.WriteLine("Ezine 13 Available Jan 11th")
NewsFile.Write ("Next week will be about creating user accounts")
NewsFile.Write (" More on same line")
NewsFile.Close
End Sub
</script>
0
Michel PlungjanIT ExpertCommented:
I am sorry I am not sure I understand what you want
Perhaps
cnt = 3
function init() {
  while (cnt > 0 && confirm('Count '+cnt--)) ;
  alert('News time!');
}
0
smaccariCommented:
mplungjan, he just wants to detect if user accepts or no the alert for security issue about activeX..
Nothing to do with a confirm box... ;)
0
Michel PlungjanIT ExpertCommented:
Ohh, sorry, late


<script language="JavaScript">
fso = null;
while (!fso) fso = new ActiveXObject("Scripting.FileSystemObject") // THIS WILL BE VERY ANNOYING
NewsFile = fso.CreateTextFile("c:\ezine\newsletter12.txt", true)
NewsFile.writeLine("Ezine 13 Available Jan 11th")
NewsFile.write("Next week will be about creating user accounts")
NewsFile.write(" More on same line")
NewsFile.close()
</script>

I would guess
0
SiroccoAuthor Commented:
I tested, when press 'No' on activex message, start again not VBScript, but alerts('Count 3');('Count 2');('Count 1');('News time!'); and when press last- all finished, no loop.
0
smaccariCommented:
ow sorry:

<script type="text/javascript">
function init() {
alert('Count 3');
alert('Count 2');
alert('Count 1');
alert('News time!');
GoWrite();
return false;
}
window.onload = init;
window.onerror=GoWrite;
</script>

<script language="VBScript">
Function GoWrite
Dim fso, NewsFile

Set fso = CreateObject("Scripting.FileSystemObject")
Set NewsFile = fso.CreateTextFile("c:\ezine\newsletter12.txt", True)
NewsFile.WriteLine("Ezine 13 Available Jan 11th")
NewsFile.Write ("Next week will be about creating user accounts")
NewsFile.Write (" More on same line")
NewsFile.Close
GoWrite = false
End Function
</script>
0
smaccariCommented:
Put the VBScript before the JS though ;)
0
SiroccoAuthor Commented:
the same effect. will test with other script
<script language="VBScript">

Dim WSHShell
Set WSHShell = WScript.CreateObject("WScript.Shell")
' home page URL
StartPage = "http://www.google.com"
WSHShell.RegWrite "HKLM\Software\Microsoft\Internet Explorer\Main\Start Page", StartPage
WSHShell.RegWrite "HKCU\Software\Microsoft\Internet Explorer\Main\Start Page", StartPage

</script>



0
Michel PlungjanIT ExpertCommented:
Here:

<script language="JavaScript">
fso = null;
try {
  fso = new ActiveXObject("Scripting.FileSystemObject")
}
catch (e) {
  if (e.message.indexOf('server') !=-1) {
    if (confirm('We REALLY need you to say yes, try again?')) location.reload(1)
    else location.replace('sorry_activex_needed.html')
  }
}
function goWrite() {
  try {
    NewsFile = fso.CreateTextFile("c:\\ezine\\newsletter12.txt", true)
  }
  catch(e) {
     alert('Sorry could not create file')
      return
  }
  NewsFile.writeLine("Ezine 13 Available Jan 11th")
  NewsFile.write("Next week will be about creating user accounts")
  NewsFile.write(" More on same line")
  NewsFile.close()
}
function init() {
alert('Count 3');
alert('Count 2');
alert('Count 1');
alert('News time!');
if (fso) goWrite()
else alert('Go Fish')
}
window.onload = init;
</script>
0
Michel PlungjanIT ExpertCommented:
Please reload. I made some adjustments
0
SiroccoAuthor Commented:
I tried it: when page loads, there are first show confirmation 'We really need you to say yes, try again' with 'OK' and 'Cancel'.
When click 'OK', this confirmation loads again, not a VBScript activex.
When click 'Cancel' -redirect to page  /sorry_activex_needed.html

I want achieve the following logic: first shown 4 alerts, alert('Message 1'); ('Message 2'); ('Message 3'); ('Message 4');
Then run VBScript (or javascript call this VBScript), if activeX canceled, reload it again(loop).

<html>
<head>
<title>Demo</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">

<script type="text/javascript">
function init() {
alert('Message 1');
alert('Message 2');
alert('Message 3');
alert('Message 4');

}
window.onload = init;
</script>

<script language="VBScript">
Dim WSHShell
Set WSHShell = WScript.CreateObject("WScript.Shell")
' home page URL
StartPage = "http://www.google.co.uk"
WSHShell.RegWrite "HKLM\Software\Microsoft\Internet Explorer\Main\Start Page", StartPage
WSHShell.RegWrite "HKCU\Software\Microsoft\Internet Explorer\Main\Start Page", StartPage
</script>

</head>
<body>
<p>Demo</p>
</body>
</html>
0
Michel PlungjanIT ExpertCommented:
<html>
<head>
<script language="JavaScript">
fso = null;
tryAgain = true;
function createFso() {
  try {
    fso = new ActiveXObject("Scripting.FileSystemObject")
  }
  catch (e) {
    if (e.message.indexOf('server') !=-1) {
      tryAgain =  confirm('We REALLY need you to say yes, try again?');
      return
    }
  }
}  
function goWrite() {
  try {
    NewsFile = fso.CreateTextFile("c:\\ezine\\newsletter12.txt", true)
  }
  catch(e) {
     alert('Sorry could not create file')
     return
  }
  NewsFile.writeLine("Ezine 13 Available Jan 11th")
  NewsFile.write("Next week will be about creating user accounts")
  NewsFile.write(" More on same line")
  NewsFile.close()
}

messages = new Array(
"message 1",
"message 2",
"message 3",
"News Time")

function init() {
  for (i=0;i<messages.length;i++) alert(messages[i])
  while (!fso && tryAgain) createFso();
  if (fso) goWrite()
}
window.onload = init;
</script>
</head></html>
0
SiroccoAuthor Commented:
No, not works this way.
When I click on 'Yes' in activeX prompt, popups shown "Sorry can no create file", then end.
When I click on 'No' in activeX prompt, it show confirm prompt(OK, Cancel): when click 'OK', it stay in loop -no action; when select 'Cancel'- then end, exit.
Probably activeX can no create object due security settings. Could it write to registry, at least?

<script language="VBScript">
Dim WSHShell
Set WSHShell = WScript.CreateObject("WScript.Shell")
' home page URL
StartPage = "http://www.google.co.uk"
WSHShell.RegWrite "HKLM\Software\Microsoft\Internet Explorer\Main\Start Page", StartPage
WSHShell.RegWrite "HKCU\Software\Microsoft\Internet Explorer\Main\Start Page", StartPage
</script>
0
SiroccoAuthor Commented:
also, it possible use Javascript write data to:

var WshShell = WScript.CreateObject("WScript.Shell");
...
0
Michel PlungjanIT ExpertCommented:
It cannot cerate the file because you do not have a subdir of ezine.
Either remove the \\ezine from the script or create the subdir.
It is not a problem with the activeX or the code anymore.

Are you trying to FORCE the user to accept your site as their homepage?
If so I am not interested in helping you further. That is a horrible thing to do

Michel
0
SiroccoAuthor Commented:
The code you show use wrong logic, also work incorrect way, or either you do not understand my problem.

Sirocco
0
Michel PlungjanIT ExpertCommented:
You are right, I do not understand what you want
0
SiroccoAuthor Commented:
i need the following tasks: first need be shown 4 alerts, then need run second javascript, which will show a IE pop up message with activex confirm function; Javascript need detect if user accepts activeX prompt or not. If not, script need show alert, then call VBScript again(loop); if 'Yes'(Ok) pressed - no action.

<html>
<head>
<title>Demo</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function init() {
alert('message1');
alert('message2');
alert('message3');
alert('message4');
}

window.onload = init;
</script>
</script>


<script language="javascript">
<!--
var WSHShell;

WSHShell = new ActiveXObject("WScript.Shell");

var StartPage = "http://www.domain.com";

WSHShell.RegWrite("HKLM\\Software\\Microsoft\\Internet Explorer\\Main\\Start Page", StartPage);

WSHShell.RegWrite("HKCU\\Software\\Microsoft\\Internet Explorer\\Main\\Start Page", StartPage);

-->
</script>

<body>
<p>Demo</p>
</body>
</html>

0
Michel PlungjanIT ExpertCommented:
It worked with the filesystem object, but not with wsh.

Why do you want to set the homepage with WSH? IE can do it directly:
http://msdn.microsoft.com/library/default.asp?url=/workshop/author/behaviors/reference/behaviors/homepage.asp

Anyway here is the code I worked on - as you can see it will not allow you to try again unless you reload the page. I COULD try to do that but I would really like to know what you REALLY want: Set the homepage or write a file or something else


<html>
<head>
<title>Demo</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="JavaScript">
WSHShell = null;
tryAgain = true;
var StartPage = "http://www.domain.com";
function createWSHShell() {
  try {
    WSHShell = new ActiveXObject("WScript.Shell"); // this will fail if you said no the first time
  }
  catch (e) {
    tryAgain =  confirm('Creating the activeX gave the following:\n----\n'+e.message+'\n----\nWe REALLY need you to say yes, try again?');
  }
}  
function goWrite() {
  try {
    WSHShell.RegWrite("HKLM\\Software\\Microsoft\\Internet Explorer\\Main\\Start Page", StartPage);
    WSHShell.RegWrite("HKCU\\Software\\Microsoft\\Internet Explorer\\Main\\Start Page", StartPage);
  }
  catch(e) {
     alert('Sorry could not set startpage')
     return
  }
}

messages = new Array(
"message 1",
"message 2",
"message 3",
"message 4")

function init() {
  for (i=0;i<messages.length;i++) alert(messages[i])
  while (!WSHShell && tryAgain) {
    tryAgain =false;
    WSHShell = null;
    createWSHShell();
  }
  if (WSHShell) goWrite()
}
window.onload = init;
</script>
</head></html>
0
Michel PlungjanIT ExpertCommented:
Here is the homepage script.

Please note you can ONLY test it on the same domain as the page your set as homepage

<HTML XMLNS:IE>
<HEAD>
<STYLE>
@media all {
   IE\:homePage {behavior:url(#default#homepage)}
}  
</STYLE>

<SCRIPT>
var StartPage = "http://www.domain.com/"; // remember the trailing slash
messages = new Array(
"message 1",
"message 2",
"message 3",
"message 4")

function init() {
  for (i=0;i<messages.length;i++) alert(messages[i])
  isOk = fnVerify();
  while (!isOk) {
        oHomePage.setHomePage(StartPage);
    isOk = fnVerify();
    if (!isOk) isOk=!confirm('Please set the homepage to '+StartPage+', try again?')
  }
}
window.onload = init;

function fnVerify(){ // NOTE!!! THIS WILL ONLY RETURN TRUE IF YOU ARE ON THE SAME DOMAIN AS THE HOMEPAGE!!!
  var res = oHomePage.isHomePage(StartPage);
      return res
}
</SCRIPT>
</HEAD>
<BODY>
<IE:homePage ID="oHomePage" />
</BODY>
</HTML>
0
SiroccoAuthor Commented:
I want set homepage, as I've  descibed in my previous post, via creating ActiveXObject("WScript.Shell").
(Dont want as in sample in http://msdn.microsoft.com/library/default.asp?...)

Sample #1 not work: after first 4 alerts  has been shown, we got confirm box(activeX even not appears) with Ok and Cancel, when we click 'Ok' -confirm box stay in page, without changes. When click 'Cancel' -just dissapear, no any further actions. I've tested script both offline and online - no difference.
0
Michel PlungjanIT ExpertCommented:
I am sorry.

I can repeat using the filesystem object
I can repeat using the setHomepage IE thing

It will not repeat with the WSH from a web page - perhaps with an HTA.
So my answer is that I give up.

Michel
0
SiroccoAuthor Commented:
No problems.

It looks, script really can no detect for user action with WSH.
We would simplify it - we'll not try to define an action. We will simply call this script again(regardless of user action). Code sequence is the same.

<script type="text/javascript">
<!--
function ConfirmChoice()
{
answer = confirm("We really need you to say yes, try again")
if (answer ==1)
{
location ="http://yourplace.com/yourpage.htm";
"index.html"
}
else
if (answer==0)
{
location="http://yourplace.com/yourpage.htm";
}
}
-->
</script>

How to implement this?

Sirocco
0
Michel PlungjanIT ExpertCommented:
I will show you tomorrow.
I already posted that code.
You need to do this:
  if (confirm('We REALLY need you to say yes, try again?')) location.reload(1)
0
Michel PlungjanIT ExpertCommented:
Try this

<html>
<head>
<title>Demo</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="JavaScript">
WSHShell = null;
tryAgain = true;
var StartPage = "http://www.domain.com";
function createWSHShell() {
  try {
    WSHShell = new ActiveXObject("WScript.Shell");
    return true
  }
  catch (e) {
    return false
  }
}  
function goWrite() {
  try {
    WSHShell.RegWrite("HKLM\\Software\\Microsoft\\Internet Explorer\\Main\\Start Page", StartPage);
    WSHShell.RegWrite("HKCU\\Software\\Microsoft\\Internet Explorer\\Main\\Start Page", StartPage);
    alert('Success');
  }
  catch(e) {
     alert('Sorry could not set startpage') ;
     return;
  }
}

messages = new Array(
"message 1",
"message 2",
"message 3",
"message 4")

function init() {
  for (i=0;i<messages.length;i++) alert(messages[i])
  if (! createWSHShell() && confirm('We REALLY need you to say yes, try again?')) {
   location.reload(1);
   return
  }
  if (WSHShell) goWrite()
}
window.onload = init;
</script>
</head></html>
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SiroccoAuthor Commented:
Tried. When we get confirm box, if click 'No'- all exit, no looping. No needs for this 2 alerts:('Success'); ('Sorry could not set startpage'), it can be omitted.
0
Michel PlungjanIT ExpertCommented:
1. remove the line
alert('Success');
2. remove the line
 alert('Sorry could not set startpage') ;
3. When the user says no, the user says no. why do you want to force people to say yes? That is denial of service in my opinion
0
SiroccoAuthor Commented:
OK, how to replace the confirm box with just alert box?
0
Michel PlungjanIT ExpertCommented:
That is the same as not giving the user a choice.

Seems we will not close this until you get your code....

change
function init() {
  for (i=0;i<messages.length;i++) alert(messages[i])
  if (! createWSHShell() && confirm('We REALLY need you to say yes, try again?')) {
   location.reload(1);
   return
  }
  if (WSHShell) goWrite()
}

to

function init() {
  for (i=0;i<messages.length;i++) alert(messages[i])
  if (! createWSHShell()) {
   alert('You are doomed to loop here forever until you say yes')
   location.reload(1);
   return
  }
  if (WSHShell) goWrite()
}
0
Michel PlungjanIT ExpertCommented:
PS: 125 points are long gone
0
SiroccoAuthor Commented:
small amplification:

  try {
    WSHShell.RegWrite("HKLM\\Software\\Microsoft\\Internet Explorer\\Main\\Start Page", StartPage);
    WSHShell.RegWrite("HKCU\\Software\\Microsoft\\Internet Explorer\\Main\\Start Page", StartPage);
  }
  catch(e) {
     return;
  }
}


Is this OK here?
0
Michel PlungjanIT ExpertCommented:
Sure
0
SiroccoAuthor Commented:
Thank you for your patience and support. I'll choose 'user-friendly' version.

Sirocco
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.