?
Solved

dhtml document.write onclick button

Posted on 2009-02-13
8
Medium Priority
?
388 Views
Last Modified: 2013-11-19
Hi

I have a colleague that has asked me to ask the following question - Thanks in advance for any feedback/comments.

I have copied the code to the below. I want the display message to show only when the "DISPLAY" button is clicked. At the moment the message shows first then the "DISPLAY" button and when the button is clicked nothing happens.


***START OF CODE***

<html>
<script language = vbscript>
sub Onload
      document.write "click the display button to show the message <br>"
      document.write "<Input Type=button Value=Display Onclick=" & display & ">"
End sub
Function display
      msgbox "You have clicked the display button ;-)"
End Function
</script>
<body onload="OnLoad()">
</body>
</html>

***END OF CODE***

Cheers
Bry
0
Comment
Question by:bryan oakley-wiggins
8 Comments
 
LVL 29

Accepted Solution

by:
Göran Andersson earned 500 total points
ID: 23632478
The reason that the message box shows before the button, is that you call the function when you only intended to use the name of the function.

You should only use document.write for code that runs while the page is loading, but the onload event is triggered after the page is finished loading.

This should work better. (Note though that using VBScript as client language only is supported in Internet Explorer, you should use Javascript to cover more users.)
<html>
<script language="vbscript">
Function display
      msgbox "You have clicked the display button ;-)"
End Function
</script>
<body>
<script language="vbscript">
      document.write "click the display button to show the message <br>"
      document.write "<input type=""button"" value=""Display"" onclick=""display()"">"
</script>
</body>
</html>

Open in new window

0
 
LVL 19

Assisted Solution

by:daveamour
daveamour earned 500 total points
ID: 23632572
Well your code is wrong as you are using display like this:
Onclick=" & display & ">"
As if it is a variable but it itsn't.  You need :

Onclick=display()>
Secondly document.write is losing your existing html so no script there to be called!
Try something like the attached instead.

<html>
<head> 
<script language="vbscript"> 
  Sub Display()
    msgbox("You have clicked the display button ;-)") 
  End sub
 
  Sub OnLoad
    document.getElementById("MyDiv").innerHTML = "click the display button to show the message <br><input type=""button"" value=""Display"" onclick=""call Display()"">"
  End Sub
 
</script> 
</head> 
<body onload="OnLoad()"> 
<form> 
 <div id="MyDiv">
</form>
</body> 
</html> 

Open in new window

0
 

Author Closing Comment

by:bryan oakley-wiggins
ID: 31546552
Hi GreenGhost and daveamour

Thank you so much for getting back with the solution so quickly. My colleague is extremely happy, so again thanks so much for that.

I will split the points - DO you feel the 250 allocation was ok for this question?

I am happy to up the points to 500 for a 250 split (if I am able to?) as you have saved my colleague some major headache..!

Cheers
Bry
0
Industry Leaders: 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 19

Expert Comment

by:daveamour
ID: 23632784
I'm happy as it is, always glad to help.  I know how much it hurts when you can't get something working!
Best to stick to Javascript though unless you have a good reason not to.
0
 

Author Comment

by:bryan oakley-wiggins
ID: 23633013
daveamour

Thanks, your very quick help and expertise was extrememly helpful and it's good to see my colleague smiling again (especially as it's Friday :-)

Thanks also for the heads-up on jscript

Again thanks
Bry
0
 
LVL 19

Expert Comment

by:daveamour
ID: 23633024
You are welcome and now maybe your colleague should join EE too!
0
 

Author Comment

by:bryan oakley-wiggins
ID: 23633689
he he yes, I believe he is signing-up as we speak :-)
0
 

Expert Comment

by:Stevolee
ID: 23634004
My friend Bryan actually posted the code on my behalf cos I did not have an account. I was really impress with the quick response and signed up 10 mins ago. I now need to ask another quick question on how I can display the message on the right window frame.
Original solution from daveamour
document.getElementById("MyDiv").innerHTML = "click the display button to show the message <br><input type=""button"" value=""Display"" onclick=""call Display()"">"
 my code and I am getting errors relating to objects...
Window.parent.frames(1).document.getElementById("MyDiv").innerHTML = "click the display button to show the message <br><input type=""button"" value=""Display"" onclick=""call Display()"">"
I am not sure if I need to add some code in the Form tag????
 
 Thanks in advance
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

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

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
Suggested Courses

830 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