The AddStartupScript method registers JavaScript
code that will be executed automatically whenever a new document is
loaded in the specified browser instance. The script is executed early
in the document initialization process, before the document's HTML
content has been parsed and before scripts defined by the document are
executed. This allows an application to initialize JavaScript
variables, define functions, modify browser behavior, or establish an
environment that will be available to scripts within the document.
Because startup scripts execute before the document has been
parsed, the document's elements may not yet be available. Scripts that
need to access or modify the document's content should register an
appropriate event handler, such as DOMContentLoaded, and
perform those operations after the DOM has been initialized.
Unlike the Execute and AsyncExecute methods, which
execute JavaScript in the context of the currently loaded document, a
startup script is registered with the browser and remains active for
subsequent navigations. The script will execute each time a new
document is created, including when navigating to another URL or
reloading the current document. Registering a startup script does not
cause it to execute in the document that is already loaded.
Multiple startup scripts may be registered with the same browser
instance. Each successful call returns a unique, nonzero script
identifier. This identifier can be passed to the RemoveStartupScript
method to remove the script when it is no longer needed. Registered
scripts remain associated with the browser instance until they are
explicitly removed or the browser instance is destroyed. The application
should consider the script identifier an opaque value and not make
any assumptions about the value or order in which identifiers are
returned.
Startup scripts execute within the JavaScript environment of the
document and are subject to the security restrictions imposed by the
browser. Because a registered script may execute when navigating to
different websites, developers should avoid including sensitive
information, such as passwords or authentication tokens, in startup
scripts. Care should also be taken when modifying global objects or
browser functionality, since these changes may affect scripts provided
by the loaded document.
Startup scripts may also execute within frames contained in the
document, including frames that load content from a different origin.
Developers should consider this behavior when writing scripts that
depend on the document's URL, origin, or security context.
When multiple startup scripts are registered with the same browser
instance, the order in which they are executed is not guaranteed.
Applications should not rely on scripts executing in the order they
were registered. If scripts must execute in a specific order, they
should be combined into a single startup script or otherwise designed
to explicitly manage their dependencies.
The method registers the JavaScript code but does not validate
its syntax or guarantee that it will execute successfully. JavaScript
errors that occur when the document is loaded do not cause the
original call to AddStartupScript to fail.
JavaScript must be enabled for startup scripts to execute. The
AllowScripting property must be set to True when the browser
is created. If scripting is disabled, this method will fail indicating
that the operation is not supported.