H5P Guides

H5P Core Api

H5P Core Api

In this section you can find a reference of available functionality in the H5P Core.

H5P.isFramed

Tells whether the H5P is displayed in an iframe or directly on the page. Mostly used for H5P internals.

H5P.jQuery

THe H5P included version of jQuery 1.9.1. See the jQuery page for more information.

H5P.init()

Initializes the H5P core and starts all H5Ps on the page. 

This should be called once when the page is done loading. The H5P core currently calls this itself when the document is ready, and it should not be necessary to call directly. 

H5P.fullScreen ($el, obj, exitCallback)

Toggles full screen display of the H5P. This is used by the integrated Fullscreen button, but may be called from within libraries as well, if the library provides its own Fullscreen toggling feature.

Fullscreenmode will use the browser native fullscreen implementations if available. If not it will fall back to a semi-fullscreen mode, which is just the element maximized within the browser frame.

If the library object (obj) defines a resize() method, it will be called after the content has been resized to fullscreen, and again when the user exits fullscreen.

Parameters

$el - jQuery object of the element to expand to fullscreen

obj - The H5P library object of the element

exitCallback - Callback function called when the user exits fullscreen mode.

Return value

None 

H5P.fullScreenIframe (contentId, obj, exitCallback)

Toggles full screen state of an iframed H5P. This is called from the H5P.fullScreen method if it is invoked inside an iframe. It should not be necessary to call directly.

Parameters

contentId - H5P content id that is to be fullscreened. This is used to search for the iframe to resize.

obj - The H5P library object of the element

exitCallback - Callback function called when the user exits fullscreen mode.

Return value

None 

H5P.resizeIframe (contentId, height)

Resize iframe height to fit content. Called from within the iframe with calculated height data. This is done automatically, so this should not be called directly.

Parameters

contentId - H5P content id used to find the iframe to resize.

height - New height in pixels.

Return value

None 

H5P.getPath (path, contentId)

Gets the full URL to the provided content data. If path is already a full http address, it is returned as is. If it is a relative path it will get the path to the content data directory on the server prepended.

Parameters

path - path to resource, may be a full URL like "http://www.flickr.com/photos/77108378@N06/11039641303/" or a relative path like "img/image1.jpg"

contentId - the content id of the H5P being shown. This is used to find the H5P content folder if necessary.

Return value

Returns full URL to the requested resource. Like "http://localhost/sites/default/files/h5p/content/123/img/image1.jpg". 

H5P.getContentPath (DEPRECATED)

Old version of H5P.getPath. This version does not work with full URLs at all, and should not be used. Still included for compatibility with libraries that are not yet updated to use getPath.  

H5P.classFromName (name)

Used from libraries to construct instances of other library objects by name. 

Example from H5P.QuestionSet, used to load the library instance for each question. This gets the class by name from H5P core, then creates an instance of it with the question parameters and content id.

    var tmp = new (H5P.classFromName(libraryObject.machineName))(question.params, contentId);
    questionInstances.push(tmp);

Parameters

name - Full machineName of the library (ie. "H5P.MultiChoice" or "H5P.Blanks")

Return value

Returns the class constructor method for the given library. 

H5P.libraryFromString (library)

Parse full library string and return an object with the library name, and version data.

Parameter

library - Full library definition string, like "H5P.QuestionSet 1.0"

Return value

Returns an object of the following form:

{
  "machineName": "H5P.QuestionSet",
  "majorVersion": 1,
  "minorVersion": 0
} 

H5P.getLibraryPath (machineName)

Proxies the H5PIntegration.getLibraryPath. Returns the path to the library location on the server. Used to get library content data.

Parameter

machineName - Internal name for the library including version. ie. H5P.Blanks-1.0

Return value

Return the full path to the library files on the server, which can be used to prefix library specific files, like CSS, images, etc.

H5P.jsLoaded (path)

Check if javascript path/key is loaded.

Parameter

path - Path of javascript to check.

Return value

Boolean, true if loaded, false otherwise. 

H5P.cssLoaded (path)

Check if style path/key is loaded.

Parameter

path - Path of css file to check.

Return value

Boolean, true if loaded, false otherwise.

H5P.cloneObject (object, recursive)

Clone the given object, recursively if wanted. Works with objects and arrays.

Parameters

object - The object to clone

recursive - Whether to clone recursively (deep). (optional, default is false).

Return value

Returns the object clone.

H5P.trim (value)

Remove all empty spaces before and after the value.

Parameter

value - String to trim.

Return value

The trimmed string.

H5P.shuffleArray (array)

Shuffle an array in place. Used to be an extension to the Array type, but moved into a helper function to avoid conflicts with faulty array iterating code.

Parameter

array - The array to shuffle

Return value

Returns the shuffled array. Since the array is shuffled in place, using the return value is optional, the original is shuffled.

H5P.setFinished (contentId, points, maxPoints)

Post finished results for user.

Parameters

contentId - The content id for the H5P that posts its results

points - The points scored in the H5P

maxPoints - the maximum number of points available in the H5P.

Return value

None 

Javascript native Prototype extensions

String.trim

Adds a trim funciton to the Javascript String class for browsers that lack it. Calls the H5P.trim() function.

Array.indexOf

Adds an indexOf() funciton to browsers that lack them.