Guest User

Untitled

a guest
May 25th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. // The purpose of this function is to draw a div inside another div
  5. // at half dimensions (sort of like looking at a mirror in another
  6. // mirror...
  7. function turtles_all_the_way_down(previous_div, level, max_level){
  8. if(level == max_level){
  9. return;
  10. }
  11. var new_div = document.createElement('div');
  12. var previous_div_style = previous_div.getAttribute('style');
  13. var height_regex = /height:\s*([0-9]+)/;
  14. var width_regex = /width:\s*([0-9]+)/;
  15. var color_regex = /background-color:\s*([^;]*);/;
  16. var height_match = height_regex.exec(previous_div_style);
  17. var width_match = width_regex.exec(previous_div_style);
  18. var color_match = color_regex.exec(previous_div_style);
  19. var previous_height = height_match[1];
  20. var previous_width = width_match[1];
  21. var previous_color = color_match[1];
  22. if(previous_color.replace(' ', '') == 'red'){
  23. var new_color = 'blue';
  24. } else {
  25. var new_color = 'red';
  26. }
  27. new_div.setAttribute('style', 'height: ' + previous_height/2 +
  28. 'px; width: ' + previous_width/2 + 'px; ' +
  29. 'background-color: ' + new_color + ';');
  30. previous_div.appendChild(new_div);
  31. turtles_all_the_way_down(new_div, level+1, max_level);
  32. }
  33. </script>
  34. </head>
  35. <body>
  36. <div id="starter_div" style="height: 500px; width: 500px; margin: auto;
  37. background-color: red;">
  38. </div>
  39. <a
  40. href="javascript:turtles_all_the_way_down(document.getElementById('starter_div'),
  41. 0, 5);">Draw 6 Nested Divs</a>
  42. </body>
  43. </html>
Add Comment
Please, Sign In to add comment