```yaml
product: AlterLab
title: JavaScript Rendering
category: guides
comparison_context: "AlterLab is an alternative to Firecrawl, ScrapingBee, and Bright Data."
source_url: https://alterlab.io/docs/guides/javascript-rendering
```

# JavaScript Rendering

Scrape dynamic websites that require JavaScript execution using AlterLab's headless browser infrastructure.

> JavaScript rendering uses Tier 4 (Browser) at $0.004 per request.

## When to Use JS Rendering

**Use JS Rendering For:**

- Single Page Applications (React, Vue, Angular)
- Content loaded via AJAX/fetch
- Infinite scroll pages
- Sites requiring user interaction simulation
- Pages with anti-bot JavaScript checks

**Don't Need JS For:**

- Static HTML pages
- Server-rendered content
- APIs returning JSON
- Most news/blog articles

> Use `mode: "auto"` (default) and AlterLab will automatically detect if JS rendering is needed.

## Basic Usage

```bash
curl -X POST https://api.alterlab.io/api/v1/scrape \
  -H "X-API-Key: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "url": "https://example.com/spa-page",
    "advanced": {
      "render_js": "auto"
    }
  }'
```

## Wait Conditions

Control when the page is considered "ready" for scraping:

| Condition          | Description                                  | Best For               |
| ------------------ | -------------------------------------------- | ---------------------- |
| `domcontentloaded` | DOM ready, external resources may still load | Fast pages, minimal JS |
| `load`             | Page and all resources fully loaded          | Image-heavy pages      |
| `networkidle`      | No network activity for 500ms (default)      | SPAs, AJAX-heavy pages |

```json
{
  "url": "https://react-app.example.com",
  "advanced": {
    "render_js": "auto",
    "wait_condition": "networkidle"
  }
}
```

## Wait for Selector

Wait for a specific CSS selector to appear before capturing the page:

```json
{
  "url": "https://shop.example.com/products",
  "advanced": {
    "render_js": true
  },
  "wait_for": ".product-grid"
}
```

> The selector wait has a default timeout of 30 seconds.

## Screenshots

Capture a full-page screenshot along with HTML content:

```json
{
  "url": "https://example.com",
  "advanced": {
    "render_js": true,
    "screenshot": true
  }
}
```

Screenshots are returned as base64-encoded PNG. Cost: +$0.0002 per request.

## PDF Generation

```json
{
  "url": "https://example.com/report",
  "advanced": {
    "render_js": true,
    "generate_pdf": true
  }
}
```

Cost: +$0.0004 per request.

## Cost Optimization

1. **Use Auto Mode** -- Let AlterLab detect when JS is needed and try cheaper methods first
2. **Set Cost Controls** -- Limit tier escalation with `cost_controls.max_tier`
3. **Cache Results** -- Enable caching for pages that don't change frequently

```json
{
  "url": "https://example.com",
  "cost_controls": {
    "max_tier": "3",
    "prefer_cost": true
  },
  "cache": true,
  "cache_ttl": 3600
}
```

## Troubleshooting

- **Content is empty or incomplete**: Use `wait_for` with a specific selector, increase `timeout`, or use `wait_condition: "networkidle"`
- **"Please enable JavaScript"**: Set `render_js: "auto"` (or `true` to always force browser rendering) in advanced options
- **Request times out**: Increase `timeout` (max 300s), use `sync: false` for long-running scrapes
- **Getting blocked/CAPTCHAs**: AlterLab auto-handles most anti-bot measures and escalates to CAPTCHA-solving tier automatically
