Javascript alert

Javascript Alert
An annoying thing when writing javascript is using the alert() function to send out debugging messages and data. The dialog box it gives you is a "modal" window so everything stops until you click Ok. Fine for one off messages and end of process values. But what happens if you want several values from a running process? Because writing down the values before clicking Ok gets a little tedious to say the least.

A quick method is to utilise the fact that javascript is a "soft" language, meaning that you can override built in methods with one of your own. So we can rewrite the alert() with
JAVASCRIPT Code :: InPage Alert
Click in the text to select the javascript code

<script type="text/javascript">
e = document.getElementById("msg")

function _
alert(message) {
e.innerHTML += message + "<br>"
}
</script>



this will redirect any message sent using the alert() method to an element with an id of msg on the page

HTML Code :: html
Click in the text to select the html code

<body>
<div id="msg"><div>
rest of page code
</body>



Now when using alert("message " + varValue) several lines can be printed out in sequence, in the same way server side code can display values.

This article was added by Chris Hirst and was posted on 29 September 2008. Chris has been a member since 09 January 2008 and has written 36 article(s) for modTalk
So long, and thanks for all the fish.
Good idea
Abel
17 October 2008 at 13:58
Sweet, Chris, that is a good idea.
Your email will not be shown
Your Name
Comment Title
Comment Text
Page Tags
No Tags to show

copyright © modTalk
2007 - 2009

7 queries - Generated in:- 1.199 seconds. This page has been viewed 3323 times
{Privacy} {Faqs} {T & C} {Contact} {Home} {About} {RSS}

Got a webmaster question? Join us at Webmaster Talk Forum