Skip to content

Instantly share code, notes, and snippets.

@salsalabs
Created May 4, 2016 22:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save salsalabs/2302dcfa84bcea42652dd0cec2952e84 to your computer and use it in GitHub Desktop.
Save salsalabs/2302dcfa84bcea42652dd0cec2952e84 to your computer and use it in GitHub Desktop.
Script to (1) default the Country code to "US" if it's in the Salsa form, or (2) append a Country code field with a value of "US".
<script type="text/javascript">
// See https://help.salsalabs.com/entries/22876904
// Actor. Changes the value of any empty Country codes to "US".
// If there's not a country code, then one is appeneded to the Salsa
// <form> tag. The appended country code is also defaulted to "US".
function actor() {
var country = $('*[name=Country]');
if (country.length == 0) {
var form = $('.salsa form');
if (form.length > 0) {
form.append('<input type="hidden" name="Country" value="US"/>');
}
} else {
if (country.val().length == 0) {
country.val("US");
}
}
}
// If we're on the letter page, then invoke the actor.
// This gets called on a timer. The timer is cleared when
// after the actor runs.
var checkExist = null;
function recipientChecker() {
if ($('.recipient').length > 0) {
actor();
if (checkExist != null) {
clearInterval(checkExist);
}
}
}
$(document).ready(function() {
if (RegExp('action_KEY=\\d+').test(window.location.href)) {
// For blind targeted actions and petitions.
actor();
// For targeted actions. We must wait for the letter page to
// appear to invoke the actor.
checkExist = setInterval(recipientChecker, 200);
} else {
// Non-action pages.
actor();
}
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment