我正在尝试根据情况更改http/https的链接。正在运行的代码如下
<script>
var _comscore = _comscore || [];
_comscore.push({ c1: "2", c2: "7109919" });
(function () {
var s = document.createElement("script"), el = document.getElementsByTagName("script")[0]; s.async = true;
s.src = (document.location.protocol == "https:" ? "https://sb" : "http://b") + ".scorecardresearch.com/beacon.js";
el.parentNode.insertBefore(s, el);
})();
</script>
<noscript>
<img src="http://b.scorecardresearch.com/p?c1=2&c2=7109919&cv=2.0&cj=1" />
</noscript>我尝试做的是更改“noscript”部分。在'noscript‘部分,代码需要根据http/https协议进行更改;
如果协议是http,则链接需要以'b.scorecardresearch.com‘开头;如果协议是https,则链接需要以'sb.scorecardresearch.com’开头
这是我的代码,我试图像这样填充img的源代码。
{# Comscore Tag #}
<noscript>
<img id="iScorecardresearch" src="" />
</noscript>
<script>
var _comscore = _comscore || [];
_comscore.push({ c1: "2", c2: "7109919" });
(function () {
var s = document.createElement("script"), el = document.getElementsByTagName("script")[0]; s.async = true;
document.getElementById("iScorecardresearch").src = (document.location.protocol == "https:" ? "https://sb" : "http://b") + ".scorecardresearch.com/p?c1=2&c2=7109919&cv=2.0&cj=1";
s.src = (document.location.protocol == "https:" ? "https://sb" : "http://b") + ".scorecardresearch.com/beacon.js";
el.parentNode.insertBefore(s, el);
var ns = document.createElement("noscript"), elns = document.getElementsByTagName("noscript")[0];
})();
</script> 但是我得到一个错误,它是‘未捕获TypeError:无法设置属性'src’of null‘’
由于我是新的前端和JavaScript,我被困在这个错误,任何人可以建议任何解决方案来解决这个错误?提前感谢
发布于 2017-02-20 08:32:07
iScorecardresearch隐藏在noscript的内部。浏览器将无法访问它。
https://stackoverflow.com/questions/42334673
复制相似问题