What does a JavaScript plugin consist of?


In this tutorial, we will discuss plugins in JavaScript. So what is a plugin?

A plugin is a JavaScript code written in a file. This file supplies various methods that we can use for various purposes.

A plugin has an entry in the array. An entry has following properties −

  • name − The name is the plugin name.

  • filename − filename is the executable file loaded to install the plugin with the extension.

  • description − description is the developer's(manufacturer's) description of the plugin.

  • mimeTypes − mimeTypes is an array with one entry for each MIME type that the plugin supports.

  • length − The length specifies the number of available MIME types. It is a read-only property of the plugin.

To get the details of MIME types, we can use the methods of the plugin object.

Methods

nameOrIndexOfMime

The nameOrIndexOfMime method returns the Mime type object that represents the available Mime types by name or index. It is an integer that specifies the position or a string that denotes the name of the Mime type.

item(indexOfMime)

The item(indexOfMime) method returns the Mime type object, representing an associated Mime type by index. This integer specifies the position of the Mime type.

namedItem(nameOfMime)

The namedItem(nameOfMime) method returns the Mime type object representing an associated Mime type by name. It is a string that denotes the name of the Mime type.

Users can follow the syntax below to work with a plugin.

Syntax

navigator.mimeTypes
mimeTypes.enabledPlugin

These are the properties that reference the plugin object.

var pluginInfo = navigator.plugins
pluginInfo.item (index)
pluginInfo.namedItem (name)

These are the methods that return the plugin object.

window.navigator.plugins.refresh([refresh])

The window.navigator.plugins.refresh( [ refresh ] ) method updates the lists of supported plugins and MIME types for the current page and reloads the page if the list has some changes.

window.navigator.plugins.length

This property returns the number of plugins.

plugin = window.navigator.plugins.item(index) window.navigator.plugins[index]

The syntax above returns the Plugin object.

plugin = window.navigator.plugins.item(name) window.navigator.plugins[name]

The syntax above returns the Plugin object for the plugin with the given name.

window.navigator.mimeTypes.length

This property returns the number of MIME types.

mimeType = window.navigator.mimeTypes.item(index) window.navigator.mimeTypes[index]

The syntax above returns the specified MimeType object.

mimeType = window.navigator.mimeTypes.item(name) window.navigator.mimeTypes[name]

The syntax above returns the MimeType object for the given MIME type.

plugin.length

The plugin length returns the number of MIME types.

mimeType = plugin.item(index)

plugin[index] returns the MimeType object.

mimeType = plugin.item(name)

plugin[name] returns the MimeType object for the given MIME type.

mimeType.type

This property returns the MIME type.

mimeType.description

This property returns the MIME type's description.

mimeType.suffixes

This property returns the MIME type's file extensions, in a comma-separated list.

mimeType.enabledPlugin

This property returns the Plugin object that implements this MIME type.

window.navigator.javaEnabled

This property returns true if a plugin supports the MIME type "application/x-java-vm".

We can use the plugin interface to add new functionalities to a plugin.

Example 1

In this program, we access the plugin using navigator.plugins in the browser. Using the plugin object, we display the plugin name, plugin description, and the Mime types.

<html> <head> <script type="text/javascript"> function getPluginInfo() { var plugTable = document.getElementById("plugTable"); var plugins = navigator.plugins; for (var i = 0; i < plugins.length; i++) { plugTable.insertRow(i); plugTable.rows[i].insertCell(0); plugTable.rows[i].cells[0].innerHTML = plugins[i].name; plugTable.rows[i].insertCell(1); plugTable.rows[i].cells[1].innerHTML = plugins[i].description; plugTable.rows[i].insertCell(2); var mimes = ""; for (var j = 0; j < plugins[i].length; j++) { mimes += plugins[i][j].description + " (type: " + plugins[i][j].type + ") < br / > "; } plugTable.rows[i].cells[2].innerHTML = mimes; } } </script> </head> <body onload="getPluginInfo ()"> <h2>JavaScript program to <i>get installed plugin details from the browser</i></h2> <table border="1px"> <thead style="font-weight: bold;"> <tr> <td>Name</td> <td>Description</td> <td>Associated MIME types</td> </tr> </thead> <tbody id="plugTable"></tbody> </table> </body> </html>

Example 2

In this example, we access the plugin object from the navigator. Using this object, we display the total plugins, an item with an index, an item with a name, mine type, and a java activation flag.

<html> <head> <script type="text/javascript"> function getPluginProp() { var plugBtnWrap = document.getElementById("plugInBtnWrap"); var plugOutEl = document.getElementById("pluginDispDom"); var plugins = navigator.plugins; var pluginStr = ""; var totalPlugin = plugins.length; pluginStr += "Total Plugins: " + totalPlugin + "<br>"; pluginStr += "Plugin 1: " + plugins[0] + "<br>"; pluginStr += "Plugin Named Item: " + plugins.namedItem('name') + "<br>"; pluginStr += "Plugin Item: " + plugins.item(0) + "<br>"; pluginStr += "MimeType 1: " + navigator.mimeTypes[0] + <br>"; pluginStr += "Java Enabled: " + (window.navigator.javaEnabled()); plugOutEl.innerHTML = pluginStr; } </script> </head> <body> <h2>Getting installed plugin details from the browser</h2> <div id="plugInBtnWrap"> <p>Click the button to view the plugin properties</p> <button onclick="getPluginProp()">Click Me</button> </div> <div id="pluginDispDom"></div> </body> </html>

In this tutorial, we discussed JavaScript plugin properties. Plugins serve various purposes. With the help of the navigator plugin object, we can access the plugin properties in the browser.

Updated on: 31-Oct-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements