Using the JavaScript keyword Eval() with AJAX makes it possible to dynamically add JavaScript code at run-time to your web page.
Here's the complete code dynalert.html, dynalert.js, and dynalert.cgi
file:dynalert.html
1
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
3 "http://www.w3.org/TR/xhtml1-strict.dtd">
4 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
5 <head>
6 <title>dynalert</title>
7 <meta http-equiv="Content-Type" content="type=text/html; charset=ISO-8859-1" />
8 <script type="text/javascript" src="dynalert.js"></script>
9 </head>
10
11 <body>
12 <p id="i1">123 </p><input type="button" value="go" onclick="update()" />
13 </body>
14 </html>
file: dynalert.js
1
2 function populatePage(results){
3 eval(results);
4 }
5
6 //send the update
7 function update() {
8
9 var strServer = "dynalert.cgi";
10 url2 = "";
11 SubmitRBData(strServer,"?" + url2);
12
13 }
14
15 function SubmitRBData(strUrl, strPostFields) {
16 http.open("GET", strUrl + strPostFields, true);
17 http.onreadystatechange = handleHttpResponse;
18 http.send(null);
19 }
20 function handleHttpResponse() {
21
22 if (http.readyState == 4) {
23 if (http.status == 200)
24 {
25 results = http.responseText;
26 populatePage(results); // string response
27 }
28 }
29
30 }
31
32 function getHTTPObject() {
33
34 var objXMLHttp=null
35
36 if (window.XMLHttpRequest)
37 {
38 objXMLHttp=new XMLHttpRequest()
39 }
40 else if (window.ActiveXObject)
41 {
42 objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
43 }
44
45 return objXMLHttp
46
47 }
48
49 var http = getHTTPObject(); // We create the HTTP Object
file:dynalert.cgi
1
2
3
4
5 require 'cgi'
6
7 cgi = CGI.new
8
9
10
11 puts "Content-Type: text/html"
12 puts
13 puts "function transcript(){ alert('and Bob said this idea might work.');}"
14 puts "alert('this is a success');"
15 puts "alert('we can now pass back anything we like');"
16 puts "transcript();"
17 puts "oNew = document.createElement('strong');"
18 puts "oNew.innerHTML = 'yes - this is bold text';"
19 puts "document.getElementById('i1').appendChild(oNew);"