使用JavaScript而不是PHP执行脚本标签

huangapple go评论52阅读模式
英文:

Execute Script tag using JS instead of PHP

问题

在上面提供的代码中,您想要在特定情况下执行Adsense标签,即在用户代理不是BOT的情况下。同时,您还希望在某些情况下通过JS执行它。下面是相关部分的翻译:

// 辅助函数
function detectBottypes() {
    $userAgent = strtolower($_SERVER['HTTP_USER_AGENT']);
    if (!empty($userAgent) && preg_match('~(bot|crawl|google|lighthouse|spider|feedparser|crawler|pinterest)~i', $userAgent)) {
        return true;
    }
    return false;
}
// 视图中的条件
@if( Request::is('photo/*') && detectBottypes() == false )
    <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js" crossorigin="anonymous"></script>
@endif
// JavaScript 部分
window.onload = function () {
    var agent = navigator.userAgent.toLowerCase();
    if (agent.indexOf('bot') != -1) {
        // 在这里执行
    }
    else {
        // 在这里处理其他情况
    }
}

这段代码根据用户代理是否包含"bot"来决定是否执行特定的操作。如果用户代理中包含"bot",则执行相应的操作。希望这有所帮助。

英文:

I have a case in php, where I execute &lt;script&gt; tag of Adsense, if the userAgent is not BOT, but for some good reason I want to execute it using JS.

Helper Function:

function detectBottypes() {
        $userAgent = strtolower($_SERVER[&#39;HTTP_USER_AGENT&#39;]);
        if(!empty($userAgent) and preg_match(&#39;~(bot|crawl|google|lighthouse|spider|feedparser|crawler|pinterest)~i&#39;, $userAgent)) {
            return true;
        }
        return false;
}

in View:

    @if( Request::is(&#39;photo/*&#39;) &amp;&amp; detectBottypes()==false )

        &lt;script async src=&quot;https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js&quot; crossorigin=&quot;anonymous&quot;&gt; 
        &lt;/script&gt;

    @endif

Above, if request is photo/* and not bot then it is rendered in view, but I want it to be rendered in either of cases but only executed for the specific case.
I have the case of JS

       window.onload = function () {
            var agent = navigator.userAgent.toLowerCase();
            if (agent.indexOf(&#39;bot&#39;) != -1) { 
              // ******* Execute here ********
            }
            else {
               
            }
       }

Reason why I want: I cache the view file to skip the load on server, so if the page is first crawled by Bot(Google) it is cached without the above case of Adsense Script ( Ad is not loaded to Bot) but since it is cached if later it is viewed by real user, the cached version without Ads is shown which I do not want, so preferred to be with JS

答案1

得分: 2

你可以像下面这样动态加载一个脚本:

window.onload = function () {
    var agent = navigator.userAgent.toLowerCase();
    if (agent.indexOf('bot') != -1) { 
        var scriptTag = document.createElement('script'); 
        scriptTag.src = 'https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js';
        scriptTag.async = true;
        scriptTag.type = 'text/javascript';
        scriptTag.crossorigin = 'anonymous';
        document.head.prepend(scriptTag);
    } else {
    
    }
}

这会导致浏览器下载并运行该脚本。然而,关于缓存的使用,还有一个更广泛的问题。如果可能的话,根据用户代理(UA)缓存两个版本的内容,并根据UA提供其中一个版本可能会更简单。

英文:

You can dynamically load a script with something like:

window.onload = function () {
    var agent = navigator.userAgent.toLowerCase();
    if (agent.indexOf(&#39;bot&#39;) != -1) { 
       var scriptTag = document.createElement(&#39;script&#39;); 
       scriptTag.src = &#39;https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js&#39;;
       scriptTag.async = true;
       scriptTag.type = &#39;text/javascript&#39;;
       scriptTag.crossorigin = &#39;anonymous&#39;;
       document.head.prepend(scriptTag);
    } else {
    
    }
}

This should cause the browser to download and run the script. However there's a broader question on your use of caching. It may be simpler if you cache two versions of the content and serve each one based on the UA, if that is an option.

huangapple
  • 本文由 发表于 2023年2月10日 14:54:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75407802.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定