Skip to content

Instantly share code, notes, and snippets.

@angellromero
Last active April 18, 2017 18:40
Show Gist options
  • Save angellromero/0c31c8d3d1153d019395bedfdff072bc to your computer and use it in GitHub Desktop.
Save angellromero/0c31c8d3d1153d019395bedfdff072bc to your computer and use it in GitHub Desktop.
// Notes
// A first recommendation is to provide some closure surrounding the introduced JS code to protect globl scope.
// Secondly, leverage load/onload detection so that we don't clobber window.onload
// Lastly, let's get this in a file and load asynchourlys through admin content area.
/*
(function(d, t) {
var g = d.createElement(t),
s = d.getElementsByTagName(t)[0];
g.src = 'https://third-party.com/resource.js';
s.parentNode.insertBefore(g, s);
}(document, 'script'));
*/
!function Prop65() {
function init() {
window.addEventListener ?
window.addEventListener('load', checkDOM, false) :
window.attachEvent && window.attachEvent('onload', checkDOM);
}
function checkDOM(){
// TODO: I would recommend an interval here
if (document.getElementById("address-province") && document.getElementById("address-province").length > 0){
var stateSelect = document.getElementById("address-province");
_bindStateSelectEvent(stateSelect);
return;
}
setTimeout( checkDOM, 100);
}
function _bindStateSelectEvent(stateSelect){
// We can movie this stateValue check within the insertMessage() function and pass in state value
if(stateSelect.value === "California") {
_insertMessage();
};
stateSelect.onchange = function(){
if(stateSelect.value === "California") {
_insertMessage();
}
};
}
function _insertMessage(){
var submitButton = document.querySelector('#checkout-shipping-continue');
var span = document.createElement('div');
span.className = 'prop65-message';
span.setAttribute('style', 'display: block; padding: 10px 0; border: 1px solid #989898; text-align: center; margin-bottom: 30px;');
span.innerHTML = 'Prop 65 Warning Message lorem ipsem ';
submitButton.parentNode.insertBefore(span, submitButton);
}
// Initialize
init()
}()
window.onload=function(){
if(document.getElementsByClassName("optimizedCheckout-header").length > 0){
checkDOM();
return;
}
};
function checkDOM(){
if (document.getElementById("address-province") && document.getElementById("address-province").length > 0){
var stateSelect = document.getElementById("address-province");
_bindStateSelectEvent(stateSelect);
return;
}
setTimeout( checkDOM, 100);
}
function _bindStateSelectEvent(stateSelect){
if(stateSelect.value === "California") {
_insertMessage();
};
stateSelect.onchange = function(){
if(stateSelect.value === "California") {
_insertMessage();
}
};
}
function _insertMessage(){
var submitButton = document.querySelector('#checkout-shipping-continue');
var span = document.createElement('div');
span.className = 'prop65-message';
span.setAttribute('style', 'display: block; padding: 10px 0; border: 1px solid #989898; text-align: center; margin-bottom: 30px;');
span.innerHTML = 'Prop 65 Warning Message lorem ipsem ';
submitButton.parentNode.insertBefore(span, submitButton);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment