Uniform resource locator (URL) protocols provide a means for locating and accessing resources that are available on the Internet and on intranets. Plug-ins can request and receive the data associated with URLs of any type that the browser can handle, including HTTP, FTP, news, mailto, and gopher.
To retrieve a URL and display it on a specified target page, use the NPN_GetURL, NPN_GetURLNotify, and NPP_URLNotify functions. This section describes the methods and procedure used for getting the URL and displaying the page.
The plug-in uses the NPN_GetURL function to ask the browser to display data retrieved from a URL in a specified target window or frame, or deliver it to the plug-in instance in a new stream. This is the way that plug-ins provide hyperlinks to other documents or retrieve data from the network.
If the browser cannot locate the URL and retrieve the data, it does not create a stream for the instance; in this case, the plug-in receives notification of the result. To request a stream and receive notification of the result in all cases, use NPN_GetURLNotify.
Note that NPN_GetURL is typically asynchronous: it returns immediately and only later handles the request, such as displaying the URL or creating the stream for the instance and writing the data. For this reason as well, calling NPN_GetURLNotify may be more useful than NPN_GetURL; the plug-in is notified upon either successful or unsuccessful completion of the request.
The target parameter represents the destination where the URL will be displayed, a window or frame. If target refers to the window or frame containing the plug-in instance, it is destroyed and the plug-in may be unloaded. If the target parameter is set to null, the application creates a new stream and delivers the data to the plug-in instance, through calls to NPP_NewStream, NPP_WriteReady and NPP_Write, and NPP_DestroyStream.
Make sure that the target matches the URL type sent to it. For example, a null target does not make sense for some URL types (such as mailto). For some recommendations to help you with target parameter choice, see the reference entry for NPN_GetURL.
The NPN_GetURLNotify method acts like NPN_GetURL. Both request the creation of a new stream with the contents of the specified URL, and, in addition, NPN_GetURLNotify notifies the plug-in of the successful or unsuccessful completion of the request. The browser notifies the plug-in by calling the plug-in's NPP_URLNotify function and passing it the notifyData value, which may be used to track multiple requests.
NPN_GetURLNotify handles the URL request asynchronously. It returns immediately and only later handles the request and calls NPP_URLNotify. Without this notification, the plug-in cannot tell whether a request with a null target failed or a request with a non-null target was completed.
The instance, url, and target parameters have the same definitions as those of NPN_GetURL. The notifyData parameter contains private plug-in data that can be used to associate the request with the subsequent NPP_URLNotify call (which returns this value) and/or to pass a pointer to some request-related payload.
If a request is not completed successfully (for example, because the URL is invalid or a HTTP server is down), the browser should call NPP_URLNotify as soon as possible. If a request completes successfully, and the target is non-null, the browser calls NPP_URLNotify after it has finished loading the URL. If the target is null, it calls NPP_URLNotify after calling NPP_DestroyStream to close the stream.
Both the NPN_GetURLNotify and NPN_PostURLNotify functions call the NPP_URLNotify method to notify the plug-in of the result of a request. Both functions pass the notifyData value to NPP_URLNotify, which tells the plug-in that the URL request was completed and the reason for completion.
The instance and url parameters have the same definitions as those of NPN_GetURL. The notifyData parameter contains the private plug-in data passed to the corresponding call to NPN_GetURLNotify and NPN_PostURLNotify.
To retrieve a URL and display it on a specified target page, you use the NPN_GetURL and NPN_GetURLNotify functions. The URL can be displayed in the same window or frame, a new window, or a different window or frame, depending on the value of the target parameter. Specify the display target with one of these special target names:
- _blank or _new: Load the URL in a new blank unnamed window. Safest target, even though, when used with a mailto or news URL, this creates an extra blank the browser instance.
- _self or _current: Load the URL into the same window the plug-in instance occupies. If this target refers to the window or frame containing the instance, the instance is destroyed and the plug-in may be unloaded.
- _parent: Load the URL into the immediate FRAMESET parent of the plug-in instance document. If the plug-in instance document has no parent, the default is _self.
- _top: Load the URL into the plug-in instance window. The default is _self, if the plug-in instance document is already at the top. Use for breaking out of a deep frame nesting.
- NPN_PostURL writes data from a file or buffer to the URL and either displays the server response in the target window or delivers it to the plug-in.
- NPN_GetURL reads data from the URL and either displays it in the target window or delivers it to the plug-in.
You can use NPN_PostURL to post data to a URL from a memory buffer or file. The result from the server can also be sent to a particular the browser window or frame for display, or delivered to the plug-in instance in a new stream. Plug-ins can use this capability to post form data to CGI scripts using HTTP or upload files to a remote server using FTP.
The browser resolves this method as the HTTP server method POST , which transmits data to the server. The data to post can be contained either in a local temporary file or a new memory buffer. To post a file, set the flag file to true, the buffer buf to the path name string for a file, and len to the length of the path string. The file-type URL prefix "file://" is optional.
Possible URL types include http (similar to an HTML form submission), mailto (sending mail), news (posting a news article), and ftp (uploading a file). For protocols in which the headers must be distinguished from the body, such as http , the buffer or file should contain the headers, followed by a blank line, then the body. If no custom headers are required, simply add a blank line ('\n') to the beginning of the file or buffer.
NOTE: You cannot use NPN_PostURL to specify headers (even a blank line) in a memory buffer. To do this, use NPN_PostURLNotify for this purpose. § The NPN_PostURLNotify function has all the same capabilities and works like NPN_PostURL in most ways except that (1) it supports specifying headers when posting a memory buffer, and (2) it calls NPP_URLNotify upon successful or unsuccessful completion of the request. NPN_PostURLNotify is typically asynchronous: it returns immediately and only later handles the request and calls NPP_URLNotify.
The parameters of this function have the same definitions as those of NPN_PostURL. The notifyData parameter contains plug-in-private data passed by NPP_URLNotify and may be used for tracking multiple posts.
Plug-ins can use NPN_PostURL or NPN_PostURLNotify to upload files to a remote server using FTP. This example uploads a file from the root of the local file system to an FTP server and displays the response in a frame named response:
The example starts by defining the mail message, myData, and its length, myLength. It sends myData and myLength to the mailto URL mailto:firstname.lastname@example.org. The target window for displaying the message is null in the example. Normally, using a null target window causes the response to be delivered from the server to the plug-in instance in a new stream, but no response is expected for a mailto URL.