JavaScript API
JavaScript API
The snippet from the Appearance tab is all most sites need. Reach for the JavaScript API when you want to set appearance from code, open the survey from your own button, react to events or change the widget while the page is open.
The command queue
Everything goes through one function, window.rawquotes(command, ...arguments). Before the widget
script has loaded, calls to it are collected in a queue and replayed in order once it has. You
define the function yourself with this stub, so you can call it at any point on the page:
<script>
window.rawquotes = window.rawquotes || function () {
(window.rawquotes.q = window.rawquotes.q || []).push(arguments)
}
</script>
Two things about order:
- Put
oncalls beforeinit, or thereadyevent is already gone when you start listening. - A
<script src=".../loader.js" data-survey-id="…">tag callsinitfor you. If you also want your owninitwith options, load the script withoutdata-survey-id, or put your owninitbefore the loader tag (see the next section).
One view per survey
A page shows at most one view of a given survey. The first init for a surveyId wins, whether
it was queued before the script loaded or called after. A later init for the same survey mounts
nothing and writes a line starting with [rawquotes] to the console. To change how the view looks,
use update. Different surveys mount independently. Once the widget is removed from the page (for
example by a single-page app navigating away), a new init for that survey mounts again.
init options
rawquotes('init', { surveyId: 'YOUR-SURVEY-ID', autoOpen: false })
| Option | Type | Meaning |
|---|---|---|
surveyId | string | The survey's identifier. The snippet from the Appearance tab contains it. |
container | CSS selector | The element on your page to draw into. Without it the widget is always floating. |
mode | 'floating' or 'inline' | Wins over appearance.placement.mode and over the Appearance tab. |
autoOpen | boolean | Floating only: the card is open from the start (the opened event fires). |
appearance | object | Any subset of the appearance fields. See Appearance options. |
Commands
| Command | Example | Effect |
|---|---|---|
init | rawquotes('init', { surveyId: '…' }) | Mounts the widget. |
on | rawquotes('on', 'completed', fn) | Listens for an event. See Events. |
show | rawquotes('show') | Floating: opens the card. Inline: shows the container. |
hide | rawquotes('hide') | Floating: closes the card (the launcher stays). Inline: hides the container. |
update | rawquotes('update', { appearance: { … } }) | Changes appearance or language without a reload. |
Open and close from your own code
show | hide | |
|---|---|---|
| Floating | Opens the card; the opened event fires. | Closes the card; dismissed (if a survey was in progress) and closed fire. The launcher stays. |
| Inline | Removes display: none from the container. | Sets display: none on the container. |
show and hide do nothing until the widget has the survey and has drawn something. In floating
mode the widget tells you when that moment comes with the launcher_ready event, so wait for it:
<button id="feedback">Tell us what you think</button>
<script>
window.rawquotes = window.rawquotes || function () {
(window.rawquotes.q = window.rawquotes.q || []).push(arguments)
}
window.rawquotes('on', 'launcher_ready', function () {
document.getElementById('feedback').addEventListener('click', function () {
window.rawquotes('show')
})
})
window.rawquotes('init', {
surveyId: 'YOUR-SURVEY-ID',
appearance: { accentColor: '#4f46e5' }
})
</script>
<script src="https://intake.rawquotes.io/widget/loader.js" async></script>
In inline mode there is no launcher_ready; show and hide work from the rendered event on. If
the survey is not available (render_error), nothing is drawn and neither command does anything.
Change the widget while it is on the page
update merges the appearance you give it into what the widget already holds, so you only send
what changes:
rawquotes('update', { appearance: { colorScheme: 'dark', language: 'de' } })
- Colours, shape, font, language,
colorScheme,dark,icon,choiceSize,choicesAlign, texts,placement.cornerandplacement.offsetchange live. placement.mode,container,surveyIdandmodeare fixed atinit. Inupdatethey are ignored with a console warning.- The visitor keeps their place: answers, the current question and typed text stay after an update.
- An
updatesent before the survey has loaded is applied to the first paint. One sent before anyinitis ignored.
When an option is rejected
The widget never throws on your page. A field with a wrong type, an unknown name or a value outside its allowed set is dropped, the rest of the object still applies, and the browser console gets one line per dropped field:
[rawquotes] appearance.accentColor must be a #RRGGBB colour
An unknown colorScheme in init is rejected the same way as in update: the console gets a line
and the survey's own setting stays. The same goes for a placement.corner other than bottom_left
or bottom_right.
If the colours or the language do not change, open the console and look for lines starting with
[rawquotes].