# How to add Attackfee to a form created in JavaScript

Load `attackfee.js` from your own origin the same way you would on a static page. When your code adds a form to the page, set `data-attackfee="write"` (or `search`) on that form. After the script has loaded, it watches the document and binds forms with that attribute, including forms that were not in the first HTML. You still have to check the pass on the server.

```html
<script src="/attackfee/v1/attackfee.js" async></script>
```

```js
const form = document.createElement("form");
form.method = "post";
form.action = "/reviews";
form.setAttribute("data-attackfee", "write");
form.insertAdjacentHTML("beforeend", '<button type="submit">Post review</button><noscript>This form needs JavaScript.</noscript>');
document.body.append(form);
```

If the write is an in-page `fetch` instead of a form post, use `attackfee.fetch` with the same arguments you would pass to `fetch`. If the server answers `403` with `error` set to `attackfee_required` and a challenge, the helper earns a work pass and retries the request once, sending `Authorization: Attackfee`. `attackfee.getPass("write")` returns `{ pass, cls, exp, took_ms }` if you need the pass yourself. Set `{ fresh: true }` when you want a new pass instead of a valid one the script already has.

People never pay money. The script never reads or sends the fields in the form. Several forms on one page can share a work pass that covers them.

## Where Attackfee fits

See [How to add Attackfee to a form](/learn/concepts/how-to-add-attackfee-to-a-form), [How to handle a refused write in your own client](/learn/concepts/how-to-handle-a-refused-write-in-your-own-client), and the [widget docs](/docs/widget).

- [Attackfee](/)
- [Learn](/learn)
- [Concepts](/learn/concepts)
- [How to add Attackfee to a form created in JavaScript](/learn/concepts/how-to-add-attackfee-to-a-form-created-in-javascript)

## Related

- [How to add Attackfee to a form](/learn/concepts/how-to-add-attackfee-to-a-form)
- [What does attackfee.js do?](/learn/concepts/attackfee-js-widget)
- [How to handle a refused write in your own client](/learn/concepts/how-to-handle-a-refused-write-in-your-own-client)
- [Several forms on one page](/learn/concepts/several-forms-on-one-page)
- [Protecting docs feedback and helpful buttons](/learn/write-surfaces/docs-feedback-and-helpful-buttons)
- [Widget](/docs/widget)
- [Server](/docs/server)

## Questions



### "Do I have to call a bind function when I inject the form?"

"No. After attackfee.js has loaded, a form added to the page with data-attackfee is bound automatically."

### "What if the write is a fetch, not a form post?"

"Use attackfee.fetch for that request. On a 403 with attackfee_required and a challenge, it earns a pass and retries the request once. You still check the pass on the server."

### "Can I ask the script for a pass directly?"

"Yes. attackfee.getPass(action) returns the pass, class, and expiry. The default action is write."

### "Does this replace the server check?"

"No. The page script is not enough. Check the pass on the route that accepts the write."
