0

How to stop debugging my js code from browser

function ViewSeatInfo(busNo, scheduleId, seatFare, tripType, dDate, time, numberOfSeat, seatUpdate, seatId) {
  totalPrice = 0;
  schedule_Id = scheduleId;
  bus_No = busNo;
  trip_Type = tripType;
  d_date = dDate;
  _time = time;

  mainSeatId = seatId;

  window.numberOfSeat = numberOfSeat;
  window.seatUpdate = seatUpdate;
  currentSell = 0;

  var seats = "";
  var data;
  $.ajax({
    type: 'post',
    contentType: 'application/json; charset=utf-8',
    url: "SellTicketOnline.aspx/ShowBusAndSeatInfo",
    data: "{'busNo':'" + busNo + "'," + "'scheduleId':'" + scheduleId + "'}",
    async: false,
    success: function(response) {
      data = response;
    },
    error: function() {
      alert("error");
    }
  });

  seatFare = data.d.BusMainFare;
  seat_Fare = seatFare;
  baseFare = seatFare;
  ShowBusSeatAllocation(data, seatFare);
  LoadStationByRouteId();
  LoadBoardingPoint();
}
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Raju Ahmed
  • 1,282
  • 5
  • 15
  • 24
  • 1
    I'm not sure exactly what you're asking, but I assume you want to stop people viewing and amending your JS code? If so, that's not possible. Also note that, you should provide an object to `data`, not a hacked together JSON string and you should really remove `async: false`. You'll even be getting browser console warnings telling you not to use it. – Rory McCrossan Jan 19 '17 at 11:00

1 Answers1

1

JavaScript for webpages runs in the browser (client-side), means the browser needs to download the file and executing so it must have access to the "original" JS file. So you cannot really stop another developer to look at your code.

What you can do is to make more difficult for peoples to understand and debug your application.

Some possibilities available are:

  • Various techniques for code obfuscation.
  • Code hiding using web-socket or similar.
  • Disable debugger tools like console.log() by overwrite them, example:

(function() {
  // disables the use of `console.log`, `console.info`, `console.error`, `console.warn` and others by replacing them with empty functions,
  // this makes the use of the debugger harder.
  var empty = function() {};
  Object.keys(window.console).forEach(function(prop) {
window.console[prop] = empty;
  });
  console.log('log');
  console.info('info');
  console.error('error');
  console.warn('warn');
})(window);

Some interesting projects and articles:

https://github.com/javascript-obfuscator/javascript-obfuscator

http://www.jsfuck.com/

How to prevent your JavaScript code from being stolen, copied, and viewed?

Community
  • 1
  • 1
GibboK
  • 71,848
  • 143
  • 435
  • 658