0

I have 2 files named index.php ad index.html in my hosting site.

Is there any way to detect the device from which user is making request and if it mobile device or width is leseer then the usual pc or laptop then load index.html or else if request is made from larger width i.e pc or Desktop load index.php instead??

Thanks in advance

mark22
  • 17
  • 7

1 Answers1

0

Yes, you can discern from user agent of request. Write the regex for that yourself or use a ready-made npm module device

code reference:

var device = require('device')
app.get('/', function(req, res) {
    var ua = req.headers['user-agent'];
    if(device.is('phone')){
       console.log('device is phone');
    }else if(device.is('tablet')){
       console.log('device is tablet');
    }else  if(device.is('desktop')){
       console.log('device is desktop');
    }else {
       console.log('device is something else');
    }
});
Shishir Arora
  • 4,257
  • 2
  • 22
  • 30
  • Noting that various browsers allow the user to set the user agent string to pretend to be some other browser. – nnnnnn Nov 08 '17 at 01:43
  • yes, but size of user device cannot be determined in server – Shishir Arora Nov 08 '17 at 01:44
  • @ShishirArora and how do i use this..should i copy and paste the code and name it index.js and place in same folder of index.html and index.php or should I paste the code within indext.html or index.php instead within tag?? – mark22 Nov 08 '17 at 01:52
  • This is nodejs server code. If you are using php and not js in server, then this will not help but use similar thing in php, if required – Shishir Arora Nov 08 '17 at 01:54