Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an event when a form button is clicked #79

Merged
merged 3 commits into from
Nov 17, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,15 @@ And include the styling in your template:
### Javascript
By loading Resources/public/js/cookie_consent.js the cookie consent will be submitted via ajax and the cookie consent will be shown on top of your website while pushing down the rest of the website.

### Events
When a form button is clicked, the event of cookie-consent-form-button-click is created. Use the following code to listen to the event and add your custom functionality.
```javascript
document.addEventListener('cookie-consent-form-button-click', function (e) {
// ... your functionality
// ... e.detail is available to see which button is clicked.
}, false);
```

### Template Themes
You can override the templates by placing templates inside your poject:

Expand Down
8 changes: 7 additions & 1 deletion Resources/public/js/cookie_consent.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@ document.addEventListener("DOMContentLoaded", function() {
xhr.open('POST', cookieConsentForm.action);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.send(serializeForm(cookieConsentForm, event.target));
// Clear all styles from body to prevent the white margin at the end of the page

let buttonEvent = new CustomEvent("cookie-consent-form-button-click", {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let buttonEvent = new CustomEvent("cookie-consent-form-button-click", {
let buttonEvent = new CustomEvent('cookie-consent-form-button-click', {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use var instead of let to keep it consistent with the other variables.

detail: event.target
});
document.dispatchEvent(buttonEvent);

// Clear all styles from body to prevent the white margin at the end of the page
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this will prompt the user to re-submit any forms (if they have sent any POST data recently), which is not an optimal UX. I think window.location.href = window.location.href; should work. Also, the document styles below will become redundant because they will not happen.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good suggestion, I can use that in the event listener. For now it creates a custom event, instead of a forced refresh after a button click, so everyone can use another functionality if required. (see new commit)

document.body.style.marginBottom = null;
document.body.style.marginTop = null;
}, false);
Expand Down