How to find out if JS code is executing in a browser or in development mode

I believe intellisense can solve a lot of time and errors. Lets say that I am making an ajax call and I know that I will get an array of cars.

$.ajax({ type: "POST", url: "GetCars.aspx", success: function (cars) { for(var i = 0; i < cars.length; i++) { var car = cars[i]; if(document.designMode) { car = { type: "", model: 0, colors: new Array() }; } // Intelissense wors great in here! // car. "the moment I type the '.' I see type, model, and colors } } }); 

I need something that will return true in the visual studio editor and false in the browser. if(document.designMode) returns true in VS and in the browser, therefore the line car = { type: "", model: 0, colors: new Array() }; always executed. I do not want the string car = { type: "", model: 0, colors: new Array() }; executed in browser only on VS, so I get intellisense . How can i do this?


Decision

Thanks @Shoms I came up with:

  var car = null; if (window.navigator.userAgent.length === 212) { car = { type: "", model: 0, colors: new Array() }; } // Intellizense works great :) 

Please note that your VS may need a different number. For me, I should try> 500, then 250, 125, etc., Until I find that it is a length of 212.

+5
source share
1 answer

You can check for userAgent and decide what to do on it. You can even set up a user agent in some browsers if you need it too.

 window.navigator.userAgent 

So, for example, your condition may be:

 if(window.navigator.userAgent.indexOf('Mozilla') !== -1) { // browser here } else { // VS here (or a non-Mozilla browser, but that not relevant for development) car = { type: "", model: 0, colors: new Array() }; } 
+3
source

All Articles