将authorize.net商家印章放置在augular js视图文件中

vij*_*iji 1 javascript asynchronous authorize.net angularjs

我有带有付款功能的 Angular js 应用程序。我想插入authorize.net印章代码(由authorize.net为经过验证的商家提供。我将我的网站添加到authorize.net服务器上的经过验证的商家印章域列表):

<!-- (c) 2005, 2014. Authorize.Net is a registered trademark of CyberSource Corporation --> <div       class="AuthorizeNetSeal"> <script type="text/javascript" language="javascript">var ANS_customer_id=my_customer_id;</script> <script type="text/javascript" language="javascript" src="//verify.authorize.net/anetseal/seal.js" ></script> <a href="http://www.authorize.net/" id="AuthorizeNetText" target="_blank">Merchant Services</a> </div>
Run Code Online (Sandbox Code Playgroud)

当我插入到我的视图页面之一时,它显示以下错误

Failed to execute 'write' on 'Document': It isn't possible to write into a document from an asynchronously-loaded external script unless it is explicitly opened.
Run Code Online (Sandbox Code Playgroud)

据我了解,加载视图后它无法写入密封图像(bcoz seal.js 包含 document.write 代码)。但我不知道如何解决这个问题。

如果我把这段代码放在主index.html 中,它就可以正常工作(显然)。但我需要它在我的视图文件之一中。请帮我解决这个问题。

预先感谢。

Sim*_*ton 6

问题是 document.writeln() 不会从异步加载的 seal.js 脚本运行,因为页面已经加载。不可能使 document.writeln() 和 document.write() 异步工作,因此您必须解决它。

有两种解决方案...

第一个解决方案是下载 seal.js 脚本并将其托管在您的网站上,用字符串替换所有 document.writeln() 函数。

我在这里完成了此操作(seal.js 文件位于 js 选项卡中): http://codepen.io/anon/pen/WbxJEd

替换document.write()document.writeln()

var htmlString = '';
htmlString += '<img src="blabla"/>';
Run Code Online (Sandbox Code Playgroud)

第二种解决方案(如果您不想托管js,也许是更好的选择)是用您自己的....覆盖document.write()和函数。document.writeln()

http://codepen.io/anon/pen/JoKvyg

document.writeOld = document.write;
document.writelnOld = document.writeln;

//Getting the element we are rendering too and setting string and custom function
var placeholder = document.getElementById('sealPlaceholder'),
    sealHtml = '',
    customWriteFnc = function(html) {
        sealHtml += html;
        placeholder.innerHTML = sealHtml;
    }

//Overwriting document.write and document.writeln with our own function
document.write = customWriteFnc;
document.writeln = customWriteFnc;

//Loading the script after the page has loaded
setTimeout(function(){
    var head = document.getElementsByTagName('head')[0],
        script = document.createElement('script');
   script.type= 'text/javascript';
   script.src= '//verify.authorize.net/anetseal/seal.js';
   head.appendChild(script);
}, 500);
Run Code Online (Sandbox Code Playgroud)