Link to home
Start Free TrialLog in
Avatar of Sirocco
Sirocco

asked on

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.
Avatar of smaccari
smaccari

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

if (confirm("Are you sure?")) {
//YES PRESSED
}
else {
//NO PRESSED
}
Avatar of Sirocco

ASKER

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>
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?
Avatar of Michel Plungjan
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!');
}
Avatar of Sirocco

ASKER

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.
Avatar of Sirocco

ASKER

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.
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>
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!');
}
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... ;)
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
Avatar of Sirocco

ASKER

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.
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>
Put the VBScript before the JS though ;)
Avatar of Sirocco

ASKER

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>



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>
Please reload. I made some adjustments
Avatar of Sirocco

ASKER

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>
<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>
Avatar of Sirocco

ASKER

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>
Avatar of Sirocco

ASKER

also, it possible use Javascript write data to:

var WshShell = WScript.CreateObject("WScript.Shell");
...
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
Avatar of Sirocco

ASKER

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

Sirocco
You are right, I do not understand what you want
Avatar of Sirocco

ASKER

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>

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>
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>
Avatar of Sirocco

ASKER

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.
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
Avatar of Sirocco

ASKER

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
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)
ASKER CERTIFIED SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Sirocco

ASKER

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.
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
Avatar of Sirocco

ASKER

OK, how to replace the confirm box with just alert box?
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()
}
PS: 125 points are long gone
Avatar of Sirocco

ASKER

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?
Avatar of Sirocco

ASKER

Thank you for your patience and support. I'll choose 'user-friendly' version.

Sirocco