Referencing window elements


/ Published in: JavaScript
Save to your folder(s)

Source: Browser inspecific referencing @ howtocreate


Copy this code and paste it in your HTML
  1. /*
  2. self and window
  3. -> These refer to the global object of current web page.
  4. parent
  5. -> Refers to the window object of the page that is holding the current page in a frameset.
  6. top
  7. -> Refers to the window object of the page at the top of the frames hierarchy.
  8. window.frames[nameOrNumberOfFrame]
  9. -> Refers to a frame or iframe held by the current page.
  10. */
  11.  
  12. // -----------------------------
  13. // frames
  14.  
  15. window.frames[nameOrNumberOfFrame].window
  16.  
  17.  
  18.  
  19. // -----------------------------
  20. // breaking out of frame
  21.  
  22. /*
  23. If someone is loading your page into their frameset and you don't want them to, you can use the self-top relationship to remove your page from their frameset and replace their frameset page with your page using:
  24. */
  25.  
  26. if( self != top ) { top.location.replace(self.location.href); }
  27.  
  28.  
  29.  
  30. // -----------------------------
  31. // hierarchy
  32.  
  33. parent.frames['otherframename'].document.images['imagename'].src = "sample.gif";
  34.  
  35.  
  36.  
  37. // -----------------------------
  38. // parent
  39.  
  40. window.parent.parent.frames['left']
  41. window.top.frames['left']
  42.  
  43.  
  44.  
  45. // -----------------------------
  46. // forms
  47.  
  48. reference_to_form.inputname
  49. reference_to_form.elements['inputname']
  50. reference_to_form.elements[number_of_input_(not_image_input)]
  51. document.nameOfForm.mybutton[1]
  52.  
  53.  
  54.  
  55. // -----------------------------
  56. // selectbox
  57.  
  58. if( document.myForm.mySelect.selectedIndex == 1 ) {
  59. document.myForm.mySelect.options[3].selected = true;
  60. if( !document.myForm.myRadio[1].checked ) {
  61. document.myForm.myRadio[2].checked = false;
  62. document.myForm.myRadio[1].checked = true;
  63. document.myForm.myRadio[0].checked = false;
  64. }
  65. }
  66.  
  67.  
  68.  
  69. // -----------------------------
  70. // setting a select option to null will remove
  71. // it from the select box
  72.  
  73. document.forms[number_of_form].mySelect.options[0] = null;
  74.  
  75.  
  76.  
  77. // -----------------------------
  78. // images
  79.  
  80. if( document['imagename'].src == "pig.gif" ) {
  81. ...
  82. if( document['imagename'].src == myImage.src ) {
  83.  
  84.  
  85.  
  86. // -----------------------------
  87. // anchors
  88.  
  89. document.anchors[number_of_<a>_element]
  90. // if the href attribute is set, you can refer to it using this:
  91. document.links[number_of_<a>_element]

URL: http://www.howtocreate.co.uk/tutorials/javascript/browserinspecific

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.