Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

223
Views
How do I skip or ignore not defined errors in javascript Line

I have javascript/Jquery files like this that is suppose to store javascript and Jquery functions for my entire website.

Issue: All the functions ID are not present in all the pages, so the script doesn't work and it stops at the line where it can't find ID reference and throw this error "Cannot set properties of null (setting 'innerHTML')"

This is an example that represents what the script flow

  //Code for page 1
  var page1redirect = document.getElementById("page1redirectafter").value;
  document.getElementById("page1redirectID").value = page1redirect;



  //Code for page 2
  var page2redirect = document.getElementById("page2redirectafter").value;
  document.getElementById("page2redirectID").value = page2redirect;



  //Code for page 3
  var page3redirect = document.getElementById("page3redirectafter").value;
  document.getElementById("page3redirectID").value = page3redirect;

Is there any way I can skip or ignore any error that doesn't run and continue the next function?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If the ids are correct and included in the HTML, those objects are probably null since by the time the script runs the DOM has still not been fully loaded. So you might want to delay the script, try more than once or ask the browser to inform you of any changes made to the DOM.

If you just want to ignore it when an object is null and do nothing, just check if it's null:

Instead of e.g.

var page3redirect = document.getElementById("page3redirectafter").value;
document.getElementById("page3redirectID").value = page3redirect;

Do

var page3redirect = document.getElementById("page3redirectafter");
if (page3redirect != null) {
    var page3redirectID = document.getElementById("page3redirectID");
    if (page3redirectID != null) {
        page3redirectID.value = page3redirect.value;
    };
};
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!