【问题标题】:Getting error while display html content on page在页面上显示 html 内容时出错
【发布时间】:2017-03-07 14:04:16
【问题描述】:

我想在谷歌广告等其他网站上展示我的网站广告,但我收到了错误

Uncaught SyntaxError: Unexpected token

我使用了 codeigniter 框架和 jquery,它将内容作为变量。有没有办法解决这个问题?

这是我的脚本代码:

(window.jQuery || document.write("<script src=\"//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js\"><\/script>"))

(function() {
  var x = document.createElement("script");
  x.type = "text/javascript";
  x.async = true;
  x.src = "http" + ("https:" == document.location.protocol ? "s" : "") + "://domain/ads/Ads/demo_ad";
  var s = document.getElementsByTagName("script")[0];
  s.parentNode.insertBefore(x, s)
})();

还有我的 PHP 代码:

function demo_ad()
    {
        $ad_var = "<a href='http://www.example.com'><div style='position:absolute; width:560px; height:300px; z-index:10000;'><img width='600' height='200' src='http://www.w3schools.com/css/trolltunga.jpg'></div></a>";
        echo $ad_var;
    }

【问题讨论】:

    标签: php jquery codeigniter


    【解决方案1】:

    在这里执行:

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    
    <script type="text/javascript">
        (function()
        {
            var x = document.createElement("script"); 
            x.type = "text/javascript"; 
            x.async = true; 
            x.src = "http" + ("https:"==document.location.protocol?"s":"") + "://domain/ads/Ads/demo_ad"; 
            var s = document.getElementsByTagName("script")[0]; 
            s.parentNode.insertBefore(x, s) 
        })();
    </script>
    

    PHP:

    function demo_ad()
    {
        $ad_var = "<a href='http://www.aqua.deals'><div><img width='600' height='200' src='http://www.w3schools.com/css/trolltunga.jpg'></div></a>";
        return $ad_var;
    }
    

    像这样调用函数demo_ad()

    【讨论】:

    • 请解释一下你做了什么
    • @mplungjan 我刚刚使用 echo 语句回显了 html
    • 您做了哪些更改以使脚本正常工作?除了不回显之外,我看不到 demo_ad() 在哪里做任何新的事情
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 2015-05-27
    • 1970-01-01
    • 2012-02-27
    • 2015-09-13
    • 2014-08-17
    • 1970-01-01
    相关资源
    最近更新 更多