timotta / TralhaController

A javascript class to observe URL changes

This URL has Read+Write access

TralhaController / test-tralhacontroller.html
100644 57 lines (54 sloc) 2.023 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<html>
<head>
<script src="tralhacontroller.js"></script>
</head>
<body>
<hr/><div id="mydiv"></div><hr/>
    <a href="#your_control_here-1">Click here 1</a>
    <a href="#your_control_here-2">Click here 2</a>
    <a href="#your_control_here-3">Click here 3</a>
    <hr/>
    <a href="#blue-observer-here">Click that blue observer see</a>
    <a href="#here-a-green-observer">Click that green observer see</a>
    <hr/>
    <a href="#" onclick="return errorMessage()">Test error message</a>
<script>
  var observer_one = {
      update: function(url) {
          var html = document.getElementById('mydiv').innerHTML
          html = html + "observer <b>one</b> url["+ url +"]<br/>"
          document.getElementById('mydiv').innerHTML = html;
      }
  }
  var observer_two = {
      update: function(url) {
          var html = document.getElementById('mydiv').innerHTML
          html = html + "observer <b>two</b> url["+ url +"]<br/>"
          document.getElementById('mydiv').innerHTML = html;
      }
  }
  var blue_observer = {
update: function(url) {
var html = document.getElementById('mydiv').innerHTML
html = html + "<font color=\"blue\">blue observer url["+ url +"]</font><br/>"
document.getElementById('mydiv').innerHTML = html;
}
  }
  var green_observer = {
update: function(url) {
var html = document.getElementById('mydiv').innerHTML
html = html + "<font color=\"green\">green observer url["+ url +"]</font><br/>"
document.getElementById('mydiv').innerHTML = html;
}
  }
 
  TralhaController.addObserver( observer_one );
  TralhaController.addObserver( observer_two );
  TralhaController.addObserver( "\\#.*(blue).*", blue_observer );
  TralhaController.addObserver( "\\#.*(green).*", green_observer );
 
  function errorMessage() {
try{ TralhaController.addObserver( {} ); } catch(e) { alert(e); }
try{ TralhaController.addObserver( "a", {} ); } catch(e) { alert(e); }
  }
  
</script>
</body>
</html>