Prevent execution of .js on mobile

0

"It's not a duplicate because I do not want to measure the page size, I want to check the device through some function."

Next I want to prevent a .js file from being read on mobile, I want it to run only on the desktop.

The problem is that I'm caching the site and it generates static html files from the pages, and the content that is displayed on the desktop ends up appearing on the mobile.

Before I had control over this because the site had no cache, but as the visits went up it was necessary to use it for the server to handle.

Is it possible to do something like this using some function that can be used directly in html as javascript ?

This was the code used:

$iphone = strpos($_SERVER['HTTP_USER_AGENT'],"iPhone");
$ipad = strpos($_SERVER['HTTP_USER_AGENT'],"iPad");
$android = strpos($_SERVER['HTTP_USER_AGENT'],"Android");
$palmpre = strpos($_SERVER['HTTP_USER_AGENT'],"webOS");
$berry = strpos($_SERVER['HTTP_USER_AGENT'],"BlackBerry");
$ipod = strpos($_SERVER['HTTP_USER_AGENT'],"iPod");
$symbian =  strpos($_SERVER['HTTP_USER_AGENT'],"Symbian");

if ($iphone || $ipad || $android || $palmpre || $ipod || $berry || $symbian == true) {
// 
} else {
//
}
    
asked by anonymous 22.07.2018 / 16:30

1 answer

0

I think your problem is in the framework you intended for the cache, believe me, I have a structure similar to yours that I use on my clients' websites, what I did to resolve was to add a suffix when mobile and another when for desktop, so avoid loading the backend cache into the different user-agent type

That is, the suffix solution serves to differentiate between static and mobile to Desktop. Of course you can do a JavaScript approach, here I am just presenting how to solve what you already have.

The other suggestion would be to use media-queries and resolve everything via CSS, but in this case you would have to reshape HTML as well.

It was something like:

global.php

<?php

function is_mobile() {
   //Se não tiver user-agent definido retorna false/assume como desktop
   if (empty($_SERVER['HTTP_USER_AGENT'])) return false;

   //Com regex checa as palavras que possivelmente o usuário esta acessando via celular
   return preg_match('#iPhone|Android|webOS#', $_SERVER['HTTP_USER_AGENT']) > 0;
}

//Cache de 3600 segundos/mesmo que 1 hora de cache
$cachelimite = 3600;

//Sufixo para Desktop/padrão
$sufixo = '.desktop.html';  

if (is_mobile()) {
    //Troca o sufixo se for celular
    $sufixo = '.mobile.html';        
}

//Define a pasta aonde deve ser salvo os estáticos, você pode ajustar
$cachefolder = 'cachepages/';

//Captura a URL com querystring
$url = $_SERVER['PHP_SELF'];

//Gera o caminho do arquivo
$cachefile = realpath($cachefolder . strlen($url) . '_' . sha1($url) . $sufixo);

//verifica se o cache existe e se expirou o tempo limite
if (is_file($cachefile) && filemtime($cachefile) + $cachelimite > time()) {
    readfile($cachefile);
    exit;
}

//Inicia a captura do buffer
ob_start();

register_shutdown_function(function () use ($cachefile) {
    //gravar o buffer no arquivo estático
    file_put_contents($cachefile, ob_get_contents());
});
  

Note that sha1 there was just an example, just as md5 sha1 can also conflict, the idea is just a basic example to understand the logic, you can adapt later.

Pages receive this:

<?php

require_once 'global.php';

So what solves the problem of loading the cache is this:

$sulfixo = '.desktop.html';  

if (is_mobile()) {
    $sulfixo = '.mobile.html';        
}

Of course I did a simplified regex to detect the user-agent:

 return preg_match('#iPhone|Android|webOS#', $_SERVER['HTTP_USER_AGENT']) > 0;

But it's just to illustrate, in case you can improve it or adapt it to your need, using the pipe | tab in the regular expression.

    
22.07.2018 / 18:21