duandanbeng1829 2013-05-17 20:21
浏览 586
已采纳

为什么file_get_contents()Javascript输出不执行?

Backstory: I'm trying to use the Newsletter Widget from SendGrid but they currently don't offer it using SSL. So any requests to https:// just redirect to http:// and then browsers complain about insecure content on my secure site.

Ok fine, so I implement something like this in PHP:

$output = file_get_contents('http://sendgrid.com/newsletter/getSubscriptionWidget?p=xxx');

And then in my view have this:

 <script type="text/javascript">
    <?php echo($output); ?>
 </script>

Viewing the source of this page after execution shows that it pulls the javascript widget code in just fine. BUT, it doesn't work. By "not working" I mean the javascript code never executes.

If I load it (in a non-https development environment) using the script tag like:

<script type="text/javascript" src="http://sendgrid.com/newsletter/getSubscriptionWidget?p=xxx">

Then it works just fine!

TL;DR; What would cause javascript to execute fine when loaded under the src attribute of the script element and not work when echoed as content inside script tags?

P.S. You can view the SendGrid widget source here.

  • 写回答

1条回答 默认 最新

  • doushi5913 2013-05-17 20:49
    关注

    SOLVED

    Turns out the SendGrid code checks to ensure the script tag is pointing at the script using this:

    // replace each instance
    $('script').each(function (wIdx, wElem) {
    var tag, src, table, trSubmit, tdSubmit, form, emailInput, message, params;
    
    tag = $(this);
    src = tag.attr('src');
    params = RegExp('p=' + '(.+?)(&|$)').exec(src);
    // check if corret script
    if ($.isArray(params) && params.length > 1 && key === params[1]) {
      form = $('<form />', {
        'class': 'SG_widget_form',
        'method': 'POST',
        'action': postURL,
        'accept-charset': 'UTF-8'
      });
      form.insertBefore(tag);
      tag.remove();
      message = $('<span />').insertBefore(form).hide();
    

    ... there is more after that, but that is the important piece. By adding the src attribute to the tag and pointing it at the correct location, the browser will block loading it normally, but will then execute the code I echoed with PHP inside of it.

    Probably fairly specific to my problem, but hopefully it will help someone else.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c