javascript 如何在 Prestashop - 1.6 的顶部添加广告脚本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25603817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 04:43:29  来源:igfitidea点击:

How to add ad script in top- header in Prestashop - 1.6?

javascriptheaderprestashopadsprestashop-1.6

提问by Bhavesh Patel

I am using prestashop 1.6 . I want to add google ads in top of the header and bottom in footer. I tried many ways but all are not succeed. Please how can i add script in my prestashop website? Thanks in advance.

我正在使用 prestashop 1.6 。我想在页眉顶部和页脚底部添加谷歌广告。我尝试了很多方法,但都没有成功。请问如何在我的 prestashop 网站中添加脚本?提前致谢。

回答by belford

You need to find header.tpl file: https://github.com/PrestaShop/PrestaShop/blob/develop/themes/default-bootstrap/header.tpl

您需要找到 header.tpl 文件:https: //github.com/PrestaShop/PrestaShop/blob/develop/themes/default-bootstrap/header.tpl

<head>
    {$HOOK_HEADER}
    <link rel="stylesheet" href="http{if Tools::usingSecureMode()}s{/if}://fonts.googleapis.com/css?family=Open+Sans:300,600&amp;subset=latin,latin-ext" type="text/css" media="all" />
    <!--AdWords Code-->
</head>

Remember to disable CCC options for JS (especially moving JavaScript to the end): enter image description here

请记住禁用 JS 的 CCC 选项(尤其是将 JavaScript 移到最后): 在此处输入图片说明

Anything within {literal}{/literal}tags is not interpreted, but displayed as-is

{literal}{/literal}标签内的任何内容都不会被解释,而是按原样显示

{literal}
<script type="text/javascript">
// ...
</script>
{/literal}

{ldelim}and {rdelim}are used for escaping template delimiters, by default {and }:

{ldelim}{rdelim}用于转义模板分隔符,默认情况下{}

<script type="text/javascript">
function foo() {ldelim}
    // ...
{rdelim}
</script>

gives:

给出:

<script type="text/javascript">
function foo() {
    // ...
}
</script>

If you still have a problem you may try to override Media Class:

如果您仍然有问题,您可以尝试覆盖 Media Class:

https://gist.github.com/hereswhatidid/8c8edef106ee95138b03

https://gist.github.com/hereswhatidid/8c8edef106ee95138b03

 <p>Some HTML goes here</p>
 <script type="text/javascript" data-keepinline="true">
 // this script will remain here when rendered
 alert( "hello!" );
 </script>
 <script type="text/javascript">
 // this script will be forced to the bottom of the page
 alert( "hello again!" );
 </script>

Media.php:

媒体.php:

<?php
Class Media extends MediaCore
{
    public static function deferScript($matches)
    {
        if (!is_array($matches))
            return false;
        $inline = '';

        if (isset($matches[0]))
            $original = trim($matches[0]);

        if (isset($matches[1]))
            $inline = trim($matches[1]);

        /* This is an inline script, add its content to inline scripts stack then remove it from content */
        if (!empty($inline) && preg_match('/<\s*script(?!.*data-keepinline)[^>]*>/ims', $original) !== 0 && Media::$inline_script[] = $inline)
            return '';

        /* This is an external script, if it already belongs to js_files then remove it from content */

        preg_match('/src\s*=\s*["\']?([^"\']*)[^>]/ims', $original, $results);
        if (isset($results[1]) && (in_array($results[1], Context::getContext()->controller->js_files) 
            || in_array($results[1], Media::$inline_script_src)))
            return '';

        /* return original string because no match was found */
        return $original;
    }
}

回答by Prescol

The correct way should be using a module. Also check if the function htmlpurifier is blocking your scripts tags.

正确的方法应该是使用模块。还要检查函数 htmlpurifier 是否阻止了您的脚本标签。

回答by Johan Mendez

A little late, but it is solved by using {literal} //script here {/literal}. It's supposed to be used only if there are curly brackets in your script, but it works.

有点晚了,但它是通过使用解决的{literal} //script here {/literal}。只有在脚本中有大括号时才应该使用它,但它确实有效。