We help IT Professionals succeed at work.

dhtml document.write onclick button

Medium Priority
490 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
Comment
Watch Question

CERTIFIED EXPERT
Commented:
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
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

bryan oakley-wigginsSenior Cloud Engineer

Author

Commented:
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
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.
bryan oakley-wigginsSenior Cloud Engineer

Author

Commented:
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
You are welcome and now maybe your colleague should join EE too!
bryan oakley-wigginsSenior Cloud Engineer

Author

Commented:
he he yes, I believe he is signing-up as we speak :-)

Commented:
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
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.