Web Vuser Functions (WEB) > Alphabetical List of Web Vuser Functions > web_js_run

web_js_run

Runs the specified JavaScript.

C Language

int web_js_run("Code=<JS code>" | "File=<file_name>",["Domain=domain",] ["ResultParam=parameter name",] [SOURCES,["File=file_name" | "Code=code", ENDITEM,]+ ] LAST)
Example: web_js_run and web_js_reset Miscellaneous Functions
Code The main code argument contains literal JavaScript code.
File The main code File argument contains a JavaScript file to be loaded.
Domain Where the JavaScript code contains a relative URL, the domain is the base URL.
ResultParam Name of the parameter in which to store the result of the JavaScript call.
SOURCES A delimiter that starts this section containing arguments containing source code and the location of JavaScript files.
File A JavaScript file to be loaded either because it contains a function referenced by a Code argument or it provides required objects or data.
Code Literal JavaScript code.
ENDITEM

A delimiter that separates one set of values from the next, where a set of values defines one compound argument. Sometimes a set contains a single value. Do not pass ENDITEM except to separate definitions.

LAST Required marker for the end of the argument list.

web_js_run runs the specified JavaScript.

One of either the main Code argument or the main File argument is required. Do not pass both.

Files referenced with File arguments must be present in the VUser directory. If the files are in a subdirectory, pass the relative path. For example, "js_scripts/code.js", where js_scripts is a subdirectory of the VUser directory.

In the SOURCES section, place any items that have to be evaluated before the main Code or File is evaluated. Arguments in the SOURCES section are evaluated in the order in which they appear. The entire SOURCES section is evaluated before the main File or Code argument.

If any JavaScript code contains a relative URL, use the Domain argument to specify the base. A relative URL is any URL that does not contain "://". The base URL in the Domain argument must include the protocol, for example, http:// or ftp://.

Example of use of the Domain argument:

web_js_run(“Code=”
    “var request = new XMLHTTPRequest();”
    “request.open(‘GET’, ‘page.html’, false);” // Relative URL
    // more code…
    ,
    “Domain=http://www.mysite.com/abc”, // Base URL
    LAST);

This request resolves to http://www.mysite.com/abc/page.html.

web_js_run cannot run script directly from a URL, for example: "URL=http://www.domain.com/file.js". If you need to run code from a web server, download the page with web_url, save the response to a parameter, and use the parameter in the web_js_run call.

To run web_js_run and web_js_reset, Javascript must be enabled in the run time settings under Internet Protocol > Preferences > Options… > Web Javascript > Enable running Javascript code.

When the code called in a web_js_run step returns, the context is not discarded. Therefore, functions, objects, and variables remain in context and can be used in subsequent steps of web_js_run.

For example, if a function is defined in one web_js_run step, it can be used in a subsequent step without redefining it. In addition, if the function in a web_js_run step defines, calculates, or fetches some data, that data can be used again in a subsequent web_js_run step.

The convenience of preservation of context is offset by an increase in memory consumption. Since the JavaScript context is saved per Vuser, the memory consumption is not trivial when there are many web_js_run calls. You can call web_js_reset to clear this context for the Vuser, thereby recovering the memory. You can also call web_js_reset if you need to clear the values for logical reasons.

If the run-time setting Simulate a new user on each iteration is set, web_js_reset is called automatically at the start of each iteration. If Simulate a new user on each iteration is not set, avoid excessive memory consumption by inserting web_js_reset calls in your script at points where you no longer need the saved context.

Several TruClient functions can be used in the code of a web_js_run call. These functions are documented under Documentation Library > VuGen > Protocols > Ajax TruClient Protocol > TruClient Functions. These functions have equivalent utility functions, as shown in the following table:

TruClient Function Equivalent VuGen Utility Function
LR.advanceParam lr_advance_param
LR.setParam lr_save_string
LR.getParam lr_eval_string
LR.log lr_debug_message
LR.userDataPoint lr_user_data_point
LR.freeParam lr_free_parameter

Return Values

This function returns LR_PASS (0) on success, and LR_FAIL (1) on failure.

Parameterization

You cannot use standard parameterization for any arguments in this function. You can refer to script parameters by using “LR.getParam()” and “LR.setParam()” from within your JavaScript code.