Prevent running . js on mobile

Asked

Viewed 49 times

0

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

Next I’m wanting to prevent a file . js to be read on mobile, I want it to run on desktop only.

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

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

It is possible to do something similar 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 {
//
}

1 answer

0


I think your problem is in the structure you thought for the cache, believe me, I have a structure similar to yours that I use on my clients' websites, what I did to solve was add a suffix when mobile and other when desktop, so avoid loading the back-end cache in different user-agent type

That is, the solution of the suffix serves to differentiate the static for cellular and for Desktop. Of course you can make an approach via Javascript, here I’m just presenting how to solve what you already have.

The other suggestion would be to use media queries and solve everything via CSS, but in this case would have to remodel the HTML too.

It became 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 the sha1 there was only example, just as the md5 the sha1 can also conflict, the idea is only a basic example to understand the logic, you can future adapt this.

Pages receive this:

<?php

require_once 'global.php';

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

$sulfixo = '.desktop.html';  

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

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

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

But it is only to illustrate, in case you can improve it or adapt to your need, using the pipe separator | in regular expression.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.