Holiday Notice: Support will be provided on a limited scale from December 24th, 2024, to January 2nd, 2025. Happy holidays and a wonderful New Year!


Topic: Js code snippets errors

demzl25 priority asked 2 years ago


Expected behavior

Fix for Js code to be possible to minify js file

Actual behavior

I have issues with minify js because Vs code show errors i have fixed a lot of them but how to fix the remaining errors is beyond my knowledge.

Link to the image that shows errors: https://ibb.co/F5pBsf5

Resources (screenshots, code snippets etc.)

Link to the js code snippet:

https://mdbootstrap.com/snippets/standard/demzl25/4150002#js-tab-view


rachelgomez123 free answered 2 years ago


Syntax errors, also called parsing errors, occur at compile time in traditional programming languages and at interpret time in JavaScript.

Greeting, Rachel Gomez


Grzegorz Bujański staff answered 2 years ago


Try this:

/* jshint esversion: 6 */
// Hamburger menu icon
const el = document.querySelector('.hamburger');

el.onclick = function () {
  el.classList.toggle('cross');
};

// Get the button - Back to top
const mybutton = document.getElementById('btn-back-to-top');

// When the user scrolls down 20px from the top of the document, show the button
window.onscroll = function () {
  scrollFunction();
};

function scrollFunction() {
  if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
    mybutton.style.display = 'block';
  } else {
    mybutton.style.display = 'none';
  }
}

// Example starter JavaScript for disabling form submissions if there are invalid fields
(() => {
  // Fetch all the forms we want to apply custom Bootstrap validation styles to
  const forms = document.querySelectorAll('.needs-validation');

  // Loop over them and prevent submission
  Array.prototype.slice.call(forms).forEach((form) => {
    form.addEventListener(
      'submit',
      (event) => {
        if (!form.checkValidity()) {
          event.preventDefault();
          event.stopPropagation();
        }
        form.classList.add('was-validated');
      },
      false
    );
  });
})();

// Form Icon color
document.querySelectorAll('.form-outline').forEach((form) => {
  const input = form.querySelector('.form-control');
  const icon = form.querySelector('i');
  input.addEventListener('focusin', () => {
    icon.classList.add('active');
  });
  input.addEventListener('focusout', () => {
    icon.classList.remove('active');
  });
});

// Google maps
const map = document.getElementById('mymap');
const maplistner = () => {
  const frame = document.createElement('iframe');
  frame.src = this.getAttribute('data-src');
  map.appendChild(frame);
  map.removeEventListener('mouseover', maplistner);
};
map.addEventListener('mouseover', maplistner);

window.addEventListener('load', () => {
  setTimeout(() => {
    const map = document.getElementById('mymap');
    const frame = document.createElement('iframe');
    frame.src = map.getAttribute('data-src');
    map.appendChild(frame);
  }, 3000);
});

const mdb = window.mdb;

// My Popup Modal and Gdpr Modal
const myPopupModal = document.getElementById('myModal');
const modal = new mdb.Modal(myPopupModal);
setTimeout(() => modal.show(), 10000); // wait 10 seconds
const gdprModalBasic = document.getElementById('gdpr_basic');
const gdprModalBasicInstance = new mdb.Modal(gdprModalBasic);
const gdprModalAdjust = document.getElementById('gdpr_adjust');
const gdprModalAdjustInstance = new mdb.Modal(gdprModalAdjust);

document.addEventListener('DOMContentLoaded', () => {
  if (document.cookie.indexOf('gdpr_mandatory=') === -1) {
    gdprModalBasicInstance.show();
  }
});

document.querySelector('#gdpr_btn_full_agree').addEventListener('click', (e) => {
  e.preventDefault();
  let date = new Date();
  date = new Date(date.getTime() + 1000 * 60 * 60 * 24 * 365);
  document.cookie = `gdpr_mandatory=1; expires=${date.toGMTString()}; path=/`;
  document.cookie = `gdpr_functional=1; expires=${date.toGMTString()}; path=/`;
  document.cookie = `gdpr_ga=1; expires=${date.toGMTString()}; path=/`;
  document.cookie = `gdpr_fbp=1; expires=${date.toGMTString()}; path=/`;
  gdprModalBasicInstance.hide();
});

document.querySelector('#gdpr_btn_adjust').addEventListener('click', () => {
  gdprModalAdjustInstance.show();
});

document.querySelector('#gdpr_adjust_form').addEventListener('submit', (e) => {
  e.preventDefault();
  e.target.querySelectorAll('input[type="checkbox"]').forEach((element) => {
    const fieldName = element.dataset.name;
    const fieldValue = element.checked ? 1 : 0;
    let date = new Date();
    date = new Date(date.getTime() + 1000 * 60 * 60 * 24 * 365);
    document.cookie = `${fieldName}=${fieldValue}; expires=${date.toGMTString()}; path=/;`;
  });
  // eslint-disable-next-line no-undef
  const instance = mdb.Toast.getInstance(document.getElementById('toast'));
  instance.show();
  gdprModalAdjustInstance.hide();
});

const toasts = ['toast'];
const triggers = ['basic-primary-trigger'];

triggers.forEach((trigger, index) => {
  // eslint-disable-next-line no-undef
  const basicInstance = mdb.Toast.getInstance(document.getElementById(toasts[index]));
  document.getElementById(trigger).addEventListener('click', () => {
    basicInstance.show();
  });
});

// <!--Global site tag(gtag.js) - Google Analytics-->
window.dataLayer = window.dataLayer || [];
const dataLayer = window.dataLayer;

function gtag(...arg) {
  dataLayer.push(arg);
}
gtag('js', new Date());

gtag('config', 'UA-xxxxxxxxx-x');

demzl25 priority commented 2 years ago

Great, thanks now is ok!

Grzegorz Bujański staff answered 2 years ago


Line 151 is an HTML comment. When copying it to the JS file you have to modify it, or comment this line in JS. Try this:

// <!--Global site tag(gtag.js) - Google Analytics-->

demzl25 priority commented 2 years ago

Thanks this solves some errors.

Some errors still remain: https://ibb.co/6NLTjpD



Please insert min. 20 characters.

FREE CONSULTATION

Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.

Status

Answered

Specification of the issue

  • ForumUser: Priority
  • Premium support: Yes
  • Technology: MDB Standard
  • MDB Version: MDB5 4.2.0
  • Device: Pc
  • Browser: Chrome 97.0.4692.99
  • OS: Windows 10
  • Provided sample code: No
  • Provided link: Yes