【问题标题】:HTML and JS debugging with YQL query使用 YQL 查询进行 HTML 和 JS 调试
【发布时间】:2017-06-21 20:28:00
【问题描述】:

谁能帮我调试一下?它应该用当前筹款金额的值更新温度计。它在不再支持 YQL html 表之前工作。我根据在这里找到的答案更新了 REST 查询,现在可以看到数据,但它没有更新温度计。任何建议表示赞赏!

<div style="text-align:center;"><a href="http://www.coolfundraisingideas.net/" alt="Fundraising Thermometer"><img id="thermometer" border="0" src="http://www.coolfundraisingideas.net/thermometer/thermometer.php?currency=dollar&goal=100&current=25&color=red&size=large"></a>
    <p style="font-size:.8em; color:#999">Provided by <a href="http://www.coolfundraisingideas.net/" rel="nofollow" style="display:block; text-decoration:none; font-size:.8em; color:#999">CoolFundraisingIdeas.net</a></p>
</div>
<script>
    function therData() {
        var current = arguments[0].query.results.body.root.eventfundraisingtotals_collection.eventfundraisingtotals.eventverifiedtotalcollected;
        var goal = arguments[0].query.results.body.root.eventfundraisingtotals_collection.eventfundraisingtotals.eventverifiedfundraisinggoal;
        document.getElementById('thermometer').src = 'http://www.coolfundraisingideas.net/thermometer/thermometer.php?currency=dollar&goal=' + goal + '&current=' + current + '&color=red&size=large';
        document.getElementsByTagName('head')[0].removeChild(document.getElementById('therScript'));
    }
    var therScript = document.createElement('script');
    therScript.setAttribute('id', 'therScript');
    therScript.src =
        "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20htmlstring%20where%20url%3D'http%3A%2F%2Fmy.e2rm.com%2Fwebgetservice%2Fget.asmx%2FgetEventFundraisingTotals%3FeventID%3D223256%26loginOrgID%3DASOSEW%26locationExportID%3D%26Source%3D'&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=therData";
    document.getElementsByTagName('head')[0].appendChild(therScript);
</script>

【问题讨论】:

  • 您在 img src 属性中硬编码了参数http://www.coolfundraisingideas.net/thermometer/thermometer.php?currency=dollar&amp;goal=100&amp;current=25
  • 是的,但它不应该在 document.getElementbyId 行中更新它吗?

标签: javascript html yql


【解决方案1】:

这是对您的脚本的更改,但使用一些狡猾的 JavaScript,它可以工作。

工作小提琴:https://jsfiddle.net/kay6jpqx/

HTML:

<div style="text-align:center;">
  <a href="https://www.coolfundraisingideas.net/" alt="Fundraising Thermometer">
  <img id="thermometer" border="0" src="https://www.coolfundraisingideas.net/thermometer/thermometer.php?currency=dollar&goal=100&current=50&color=red&size=large"></a>
  <p style="font-size:.8em; color:#999">Provided by <a href="https://www.coolfundraisingideas.net/" rel="nofollow" style="display:block; text-decoration:none; font-size:.8em; color:#999">CoolFundraisingIdeas.net</a>
  </p>
</div>

JavaScript:

function therData(arguments){
  //console.log(arguments.query.results.result);
  var res = arguments.query.results.result;

  //get the current amount from the result
  var startPos = res.indexOf("eventverifiedtotalcollected")+28;
  var endPos = res.indexOf("/eventverifiedtotalcollected")-1;
  var evtVerTotal = res.substring(startPos, endPos);
  console.log(evtVerTotal);

  //get the goal amount from the result
  startPos = res.indexOf("eventverifiedfundraisinggoal")+29;
  endPos = res.indexOf("/eventverifiedfundraisinggoal")-1;
  var evtVerGoal = res.substring(startPos, endPos);
  console.log(evtVerGoal);

  var current = evtVerTotal;
  var goal = evtVerGoal
  document.getElementById('thermometer').src = 'http://www.coolfundraisingideas.net/thermometer/thermometer.php?currency=dollar&goal=' + goal + '&current=' + current + '&color=red&size=large';
}

var therScript = document.createElement('script');
therScript.setAttribute('id', 'therScript');
therScript.src = "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20htmlstring%20where%20url%3D'http%3A%2F%2Fmy.e2rm.com%2Fwebgetservice%2Fget.asmx%2FgetEventFundraisingTotals%3FeventID%3D223256%26loginOrgID%3DASOSEW%26locationExportID%3D%26Source%3D'&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=therData";
document.getElementsByTagName('head')[0].appendChild(therScript);

【讨论】:

  • 很高兴为您提供帮助,请将此标记为答案,以便其他人在遇到类似问题时可以找到它。
猜你喜欢
  • 1970-01-01
  • 2015-02-17
  • 2019-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多