Module: Browser::DocumentMethods

Defined in:
lib/opal/jquery/document.rb

Overview

Document includes these methods to extend Element.

Generally, you will want to use the Document top level instance of Element.

Usage

A useful method on Document is the #ready? method, which can be used to run a block once the document is ready. This is equivalent to passing a function to the jQuery constructor.

Document.ready? do
  puts "Page is ready to use!"
end

Just like jQuery, multiple blocks may be passed to #ready?.

Document head and body elements

Every document has atleast two elements: a head and body. For convenience, these are both exposed as #head and #body respectively, and are just instances of Element.

puts Document.head
puts Document.body

# => #<Element: [<head>]>
# => #<Element: [<body>]>

Events

Document instances also have #on, #off and #trigger methods for handling events. These all just delegate to their respective methods on Element, using document as the context.

Document.on :click do |evt|
  puts "someone clicked somewhere in the document"
end

Instance Method Summary collapse

Instance Method Details

#bodyElement

Element instance wrapping document.body.


82
83
84
# File 'lib/opal/jquery/document.rb', line 82

def body
  Element.find `document.body`
end

#headElement

Element instance wrapping document.head.


75
76
77
# File 'lib/opal/jquery/document.rb', line 75

def head
  Element.find `document.head`
end

#ready?(&block) ⇒ Boolean

Register a block to run once the document/page is ready.

Examples:

Document.ready? do
  puts "ready to go"
end

54
55
56
# File 'lib/opal/jquery/document.rb', line 54

def ready?(&block)
  `$(#{block})` if block_given?
end

#titleString

Returns document title.


61
62
63
# File 'lib/opal/jquery/document.rb', line 61

def title
  `document.title`
end

#title=(title) ⇒ Object

Set document title.


68
69
70
# File 'lib/opal/jquery/document.rb', line 68

def title=(title)
  `document.title = title`
end