0

I need to change my push notifications supplier, and want to run some basic tests on my main live site, to make sure things fit in with my live code.

Obviously I don't want the site users to see the tests. I was wondering if there is a way that you can have js code running for a specific machine? eg get my machines ip address, and set the js up so that if the ip equals that address, run the test js, otherwise run the normal live js?

Or is there a standard way of running this type of testing on a live site?

Thanks for your time and help.

Shaun
  • 2,043
  • 3
  • 27
  • 36

1 Answers1

1

Warning: As stated on the comments of your question, it is recommended for you to split your code/config between production and development. But if you really want to do this, the answer is as it follows:

You'll most likely need to use a service to check you public ip

If you're using jQuery you can do something like this:

$.get('//freegeoip.net/json/', function(data) {
  if (data.ip === '<Your IP>') {
    // Your specific code goes here
  }
});

For a Vanilla JavaScript answer:

fetch('//freegeoip.net/json/').then(function(response) {
  return response.json()
}).then(function(data) {
  if (data.ip === '<Your IP>') {
    // Your specific code goes here
  }
});

Note: Beware of the API's rate limits, they're displayed on the headers, you might need to implement your own API if you get too much traffic.

enter image description here

Nihey Takizawa
  • 797
  • 5
  • 8