Inline javascript is getting stripped off from the AJAX response by Prototype JS Ajax.Request method

Inline javascript is getting stripped off from the AJAX response by Prototype JS Ajax.Request method

October 18, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Inline javascript is getting stripped off from the AJAX response by Prototype JS Ajax.Request method

The Mystery of Stripped JavaScript in Prototype JS Ajax.Request

You’re encountering an intriguing issue while working on your Magento store’s widget using the Prototype.js framework - inline JavaScript is getting stripped from the AJAX response despite configuring the evalJS parameter to true.

Understanding the Problem

Your grid.js file has a well-structured loadTabContent function that makes an AJAX request. You’re correctly using Ajax.Request with the evalJS: true option in the parameters, which should allow for the evaluation of any JavaScript code embedded within the response body.

However, when you inspect the updated content on the client side, the inline JavaScript snippet is missing from the Elements tab in your Chrome developer tools. This indicates that while the data is being loaded correctly, the script tags are somehow being filtered out during the rendering process.

Analyzing the Code

Let’s break down the relevant sections of your code:

  1. The loadTabContent function: You’re making an AJAX GET request to the tabContentLoadUrl, passing parameters for id and type. The success handler is correctly bound to the onTabContentLoad function.
request = new Ajax.Request(
    this.tabContentLoadUrl,
    {
        method:'get',
        onSuccess: this.onTabContentLoad.bind(tab),
        onFailure: this.ajaxFailure.bind(this),
        evalJS: true,
        parameters: {
            id: entityId,
            type: tabType
        }
    }
);
  1. The onTabContentLoad function: Here, you’re updating the content of the specified div element with the response text. If there’s a successful transport and a valid responseText property, you attempt to parse it and update the contents.
$('tab-' + tabType + '-' + entityId + '-contents').update(response);
  1. The server-side rendering: Your renderTabContentAction is correctly registering the parameters, generating the HTML content for the tabs, and setting the response headers before returning the generated HTML as the body of the response.
Mage::register('current_entity_id', $entityId);
Mage::register('current_tab_type', $tabType);

$tabHtml = $this->_getTabsHtml($entityId, $tabType);

$this->getResponse()->setHeader('HTTP/1.0', '200', true);
$this->getResponse()->setBody($tabHtml);
  1. The response structure: Your JSON response is correctly structured with the desired HTML content and an inline script tag at the end.
<script type="text/javascript">
     bulkOrderGrid.initVerticalTabs();
     bulkOrderGrid.activateTab('2441', 'VERTICAL');
</script>

Possible Culprit: The Element.update Function

The issue seems to arise when you use the Element.update function to refresh the section of the page with the new content. This function is likely stripping out any script tags it encounters during the parsing process.

You’ve found that commenting out the entire stripScripts part in the Prototype.js library (around line 2048) allows the inline JavaScript code to be evaluated correctly. However, this approach might have wider implications for your application’s security and performance.

content.stripScripts();

Potential Solutions

  1. Use Ajax.Updater instead of Element.update: The Ajax.Updater function is a high-level wrapper around the low-level Element.update method that handles cross-domain requests, preserves event handlers, and evaluates inline scripts more reliably. Try replacing your current usage of Element.update with an equivalent call to Ajax.Updater.

  2. Manually insert the script: Instead of relying on inline scripts in the response HTML, consider generating separate script elements and appending them to the document after updating the content.

var script = document.createElement('script');
script.type = 'text/javascript';
script.innerHTML = 'bulkOrderGrid.initVerticalTabs();\nbulkOrderGrid.activateTab(\'2441\', \'VERTICAL\');';
document.body.appendChild(script);
  1. Evaluate scripts separately: After updating the content, use eval or new Function() to manually evaluate any inline JavaScript code you expect in your response.
var script = response.match(/<script[^>]*>([\S\s]*?)<\/script>/i);
if (script) {
    eval(script[1]);
}

Conclusion

In conclusion, the issue lies in how Prototype.js handles and strips scripts during the parsing process when using the Element.update function. To resolve this, consider switching to Ajax.Updater, manually inserting script elements after updating content, or evaluating scripts separately.

Remember that handling inline scripts directly can have security implications, so always ensure you trust the source of your responses.

For more insights on managing JavaScript in Magento applications and best practices for working with AJAX responses, check out our comprehensive guide at LaravelCompany.com.

Happy coding!

Admin Configuration Errors JavaScript

Our network