Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Javascript code to detect which button is pressed

Posted on 2006-04-06
40
Medium Priority
?
457 Views
Last Modified: 2012-06-27
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.
0
Comment
Question by:Sirocco
  • 17
  • 16
  • 6
39 Comments
 
LVL 9

Expert Comment

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

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

Author Comment

by:Sirocco
ID: 16394792
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
 
LVL 9

Expert Comment

by:smaccari
ID: 16394924
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 16394947
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
 

Author Comment

by:Sirocco
ID: 16394955
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
 

Author Comment

by:Sirocco
ID: 16395035
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
 
LVL 9

Expert Comment

by:smaccari
ID: 16395103
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
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 16395150
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
 
LVL 9

Expert Comment

by:smaccari
ID: 16395184
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
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 16395217
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
 

Author Comment

by:Sirocco
ID: 16395294
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
 
LVL 9

Expert Comment

by:smaccari
ID: 16395343
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
 
LVL 9

Expert Comment

by:smaccari
ID: 16395359
Put the VBScript before the JS though ;)
0
 

Author Comment

by:Sirocco
ID: 16395435
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
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 16395606
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
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 16395704
Please reload. I made some adjustments
0
 

Author Comment

by:Sirocco
ID: 16399022
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
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 16400126
<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
 

Author Comment

by:Sirocco
ID: 16401413
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
 

Author Comment

by:Sirocco
ID: 16403731
also, it possible use Javascript write data to:

var WshShell = WScript.CreateObject("WScript.Shell");
...
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 16406436
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
 

Author Comment

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

Sirocco
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 16408219
You are right, I do not understand what you want
0
 

Author Comment

by:Sirocco
ID: 16485856
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
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 16486521
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
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 16486590
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
 

Author Comment

by:Sirocco
ID: 16487179
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
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 16488267
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
 

Author Comment

by:Sirocco
ID: 16488695
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
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 16490082
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
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 500 total points
ID: 16495321
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
 

Author Comment

by:Sirocco
ID: 16496585
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
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 16497096
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
 

Author Comment

by:Sirocco
ID: 16497200
OK, how to replace the confirm box with just alert box?
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 16497226
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
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 16497231
PS: 125 points are long gone
0
 

Author Comment

by:Sirocco
ID: 16497696
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
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 16497822
Sure
0
 

Author Comment

by:Sirocco
ID: 16498029
Thank you for your patience and support. I'll choose 'user-friendly' version.

Sirocco
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…
Suggested Courses

571 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