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

181
Views
JS error after setAttribute on SVG element is not catchable

I am working on a web tool, which lets you change attributes of SVG elements. Currently I am trying to give the user feedback on any invalid attributes or values.

When calling setAttribute (or setAttributeNS) on an SVG element with an unexpected value, a javascript error occurs. I tried to catch this by using try/catch or window.onerror, but could not find a way to do so.

Example code:

    <svg id="svg" width="300" height="100">
      <linearGradient id="gradient" x1="10" y1="10" x2="100" y2="100">
        <stop offset="0" stop-color="#000000"></stop>
        <stop offset="1" stop-color="#969695"></stop>
      </linearGradient>
    </svg>
    try {
       document.querySelector('#gradient').setAttribute('x1', 'This error is not catchable');
    } catch (error) {
       console.log('never triggered');
    }

https://jsfiddle.net/wr3tne2y/

tested in Chromium v91 and firefox:

js error slipping through try/catch

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

0

You would need to check the value before setting it.

let val = 'This error is not catchable';
if (!isNaN(parseInt(val, 10)))
{
  document.querySelector('#gradient').setAttribute('x1', val);
}
else
{
  console.log('ERROR!');
}
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!