JavaScript Javascript errors effect on browsers

I provide some additional services to websites that add my script to their site. Technically my script does not interact with actual functionality of the site, the most is does is read some information and the main part of the code only runs only after the event (think something like Google Analytics).

Recently a bug caused an "X is undefined" error to occur in some circumstances and the client insists that this error is crashing their site. They sent me a screenshot that shows a blank page and a console screenshot that shows the error but they removed my code so I was not able to see it in real time.

My question is if there is any possibility that these types of errors can affect the website. For example assume this code runs on the website:

var tmp = Sizzle('h1');
tmp[0].innerHTML = "test";

Assume that for some reason Sizzle is not defined when the code runs or that there weren't any H1 elements on the page so tmp is empty. Could the resulting error under any circumstances affect the outside environment. e.g the page the code runs on? Obviously if I had overwritten the body element then yes it will affect the website but my question is only about "x is undefined" or syntax errors.

Answer:1

An Error will break the execution of the current function-stack.

For example if you have a onclick event, that calls a function and you have an error at the beginning it will not execute anything after that code. But that doesn't mean that any JavaScript on your Website won't work anymore. Everything will still be fine and run. Of cause if there was something important that was not executed after an Error it would change the logic of your runtime.

You can also avoid Error to go down the function-stack with a try-catch block:

function a() {
  var x = new DoesNotExist()
  alert("test a") // this will not alert() as there is an error before
}

function b() {
  try {
    var x = new DoesNotExist()
  } catch (e) {}
  alert("test b") // this will alert because the error is catched
}

alert("code is running")
a() 
b() // this will not execute as a() broke this script
// however pressing the button b will still alert in b() as it's in a new stack
alert("code finished")
<input onclick="a()" value="a" type="button" />
<input onclick="b()" value="b" type="button" />
Answer:2

Yes, there are circumstances that error of the type "X is undefined" affects the whole website.

If the code which produces "X is undefined" error is:

  • inside a function or inside an object method - it will stop this current function from execution but likely will proceed with the script below it.
  • written as a row by row Javascript statements it will stop on this current point and won't proceed with else script.

Probably your client has more Javascript statements (loading content of the page) below the crashing pont, these statements are not executed and the content is not loaded.

You can simply add to your script:

    if (typeof variable !== 'undefined')
    {
        // else part of the code
    }
Answer:3

I use jQuery & PDO to dynamic a pagination When I create some button which have id = xx Then use jQuery try get one of button id , It isn't work This is my code: $records = $...

I use jQuery & PDO to dynamic a pagination When I create some button which have id = xx Then use jQuery try get one of button id , It isn't work This is my code: $records = $...

I have the following data structure { "advisors" : { "0ea9bab6-415e-4900-8698-ac03a1ef4518" : { "description" : "", "price" : 0, "title" : "" }, "cc31c353-ca6a-440d-...

I have the following data structure { "advisors" : { "0ea9bab6-415e-4900-8698-ac03a1ef4518" : { "description" : "", "price" : 0, "title" : "" }, "cc31c353-ca6a-440d-...

  1. query data with python
  2. query data with transact sql
  3. query data with database functions
  4. query data with sql
  5. query data with pandas
  6. query data with mongodb
  7. query data with
  8. query data with mysql
  9. data query with firebase
  10. query data in excel
  11. query data in s3
  12. query data from elasticsearch
  13. query data from dynamodb
  14. query data in r
  15. query data in access
  16. query data from influxdb
  17. query data in power bi
  18. query data from two tables sql
  19. query data in redis
  20. query data from google analytics

I want to display jquery yearly calendar as shown in image. now I displayed only monthly calendar JSFiddle $(function () { $("#datepicker").datepicker({ changeMonth: true, ...

I want to display jquery yearly calendar as shown in image. now I displayed only monthly calendar JSFiddle $(function () { $("#datepicker").datepicker({ changeMonth: true, ...

I have setup a file using OpenPGP.js which encrypts a message. I need to set the PGP blocks to a javascript variable so i can pass it to the encrypt/decrypt functions. When I paste it in normally it ...

I have setup a file using OpenPGP.js which encrypts a message. I need to set the PGP blocks to a javascript variable so i can pass it to the encrypt/decrypt functions. When I paste it in normally it ...