> For the complete documentation index, see [llms.txt](https://docs.apryse.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.apryse.com/web/what-is-webviewer/wv-inside.md).

# Inside WebViewer: instantiation, iframe & loading from another domain

Learn how to use the WebViewer package to render an app inside an iframe or web component. Instantiation steps, iframe usage, and accessing objects within the iframe explained. Explore more in our Usa

When you download the WebViewer package, it contains three main components:

![](https://3532544125-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FX9YnTSKIHvV7m0A36LbO%2Fuploads%2Fgit-blob-7c124061124ccb0107fb4c27e5540f2a0307beda%2Fc3dd119dbf606e8312c95d271f215ce1d54292ba-1066x323.png?alt=media)

`webviewer.js` is the main entry point of this package that uses [UI](/web/what-is-webviewer/usage.md#ui-namespace) and [Core](/web/what-is-webviewer/usage.md#core-namespace) namespaces to render the WebViewer app inside an iframe or a web component. So, after importing `webviewer.js`, the WebViewer app (composed of UI and Core) can be instantiated by using the PDFTron.WebViewer constructor. If you look at the [viewing sample](/web/get-started/guides/samples/viewing.md#viewing), you will see that the app is created inside an iframe.

![](https://3532544125-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FX9YnTSKIHvV7m0A36LbO%2Fuploads%2Fgit-blob-9f2a9cf656db844aed9be9575d7382d922c84f63%2Fce6b978deaf6a68f807f6d8eb8248c2c96f9f5ac-521x369.png?alt=media)

## Details of instantiation

The instantiation can be summarized in one sentence.

> Instantiation === Webviewer creating the iframe-wrapped app based on the options

For example, for this code:

{% tabs %}
{% tab title="JavaScript" %}
{% code lineNumbers="true" %}

```js
WebViewer({
  path: '/WebViewer/lib',
  initialDoc: 'mydoc.pdf',
}, viewerElement)
  .then(instance => {
    // API functions are now ready to be called on instance
  });
```

{% endcode %}
{% endtab %}
{% endtabs %}

the following steps will be executed:

1. The constructor is telling `webviewer.js` the `path` to load the library files containing the UI and Core
2. The constructor then mounts the UI to the `viewerElement` provided
3. The UI sees the `initialDoc` option and calls the `loadDocument` function from Core
4. Core processes the `myDoc.pdf` document and renders it in the UI

WebViewer also has [other constructor options](https://sdk.apryse.com/api/web/global.html#WebViewerOptions__anchor) for configuring the viewer.

## WebViewer's iframe

It's important to remember that WebViewer's iframe is a completely separate window, isolated from your own HTML page. This means all WebViewer objects and namespaces are only available in the iframe. Then how do you access objects inside the iframe to call functions or to listen to events? You can do that with the `instance` that's returned from the constructor's promise.

{% tabs %}
{% tab title="JavaScript" %}
{% code lineNumbers="true" %}

```js
// Your script
WebViewer(...)
  .then(instance => {
    const { UI, Core } = instance;
    ...
  });
```

{% endcode %}
{% endtab %}
{% endtabs %}

The `instance` argument that's returned from the promise is an object that contains objects and namespaces from the iframe as well as helpful APIs. So you can:

* call `instance.Core.documentViewer.addEventListener('documentLoaded', documentLoadedHandler)` to run functions when a document is loaded
* access `instance.Core.Annotations` to see different annotations classes available
* or invoke `instance.UI.setCurrentPageNumber(5)` to navigate the viewer to page 5.

Refer to our [Usage](/web/what-is-webviewer/usage.md) guide or [WebViewer class API](https://sdk.apryse.com/api/web/WebViewerInstance.html) for more details.

## Loading WebViewer from another domain

In some scenarios, it may be necessary to put the WebViewer library files on a different domain. Although this is completely possible, it does change the WebViewer iframe source to point to another domain and with this, comes with additional considerations.

Check out the following [guide](/web/advanced/remote-lib.md) for more details!


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.apryse.com/web/what-is-webviewer/wv-inside.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
