ignore syntax errors in javascript

3

Currently I have a code that works for all browsers except internet explorer. I would like to find a way to ignore this code when I'm on the internet explorer, that is, I will not be shown any errors. what I want is to enclose my code in a kind of if or try catch and not run. What can I do?

I get this error: SCRIPT1028: Se esperaba un identificador, una cadena o un número

var nombresColumnas=["data0","data1","data2","data3"];

var data= {
 [nombresColumnas[0]]: '#33E789',   //error here..!
 [nombresColumnas[1]]: '#00CCFF',
 [nombresColumnas[2]]: '#FFC33E',
 [nombresColumnas[3]]: '#384858'
}
    
console.log(data)
    
asked by yavg 02.05.2018 в 17:50
source

3 answers

4

What you're using is called Computed Property Names

const myKey = 'Dynamic'
let obj = { [myKey]: 'myValue' }

You can check their compatibility in different browsers .

As you'll see, in IE NO está soportado .

You can not ignore runtime errors, the simplest thing would be to rewrite the code as Pablo says or use tools that do it for you when developing as Babel .

    
answered by 02.05.2018 / 18:14
source
1

As other users tell you, it would be better if you could make code compatible with all browsers without having specific code for any of them (which is a symptom of problems in the code).

But ... if you want to have something specific for IE, you can apply a solution similar to what is suggested in the SpiderCode solution in StackOverflow , which applied to your code would look like this:

var nombresColumnas=["data0","data1","data2","data3"];
var ua = window.navigator.userAgent;
var msie = ua.indexOf("MSIE ")

if (msie < 0) {
  var data= {
    [nombresColumnas[0]]: '#33E789',   //error here..!
    [nombresColumnas[1]]: '#00CCFF',
    [nombresColumnas[2]]: '#FFC33E',
    [nombresColumnas[3]]: '#384858'
  }
}    
console.log(data)
    
answered by 02.05.2018 в 20:54
1

I have a function that verifies the browser, it seems to me that you can use it to execute a certain script depending on the browser;

function Verificar_navegador() {
        	var detectarNavegador = window.navigator.userAgent;
        	var chrome = /Chrome/;
        	var firefox = /Firefox/;
        	var opera = /OPR/;
        	var edge = /Edge/;
        	var safari = /Safari/;
        	var navegador;
        	if (chrome.test(detectarNavegador) && !(opera.test(detectarNavegador)) && !(edge.test(detectarNavegador))) {
        		navegador = 'Google Chrome';
        	} else if (firefox.test(detectarNavegador)) {
        		navegador = 'Firefox Mozilla';
        	} else if (safari.test(detectarNavegador) && !(chrome.test(detectarNavegador))) {
        		navegador = 'Apple Safari';
        	} else if (opera.test(detectarNavegador)) {
        		navegador = 'Opera';
        	} else if (edge.test(detectarNavegador)) {
        		navegador = 'Microsoft Edge';
        	} else {
        		navegador = 'Desconocido';
        	}
        	
        return navegador;
        }
        
   console.log(Verificar_navegador());

Extra:

You can check if the user does not have javascript enabled, if not enabled, you can use the following to redirect it somewhere you want (This goes in the head html, not in your javascript);

<noscript>
    <META HTTP-EQUIV="Refresh" CONTENT="0;URL=index_incompatible.html">
</noscript> 
    
answered by 02.05.2018 в 20:11