简体   繁体   English

使用document.write异步运行外部JS函数

[英]Running external JS function with document.write asyncronously

We have a website that is supposed to be loading a logo provided by a 3rd party (the logo is a link that allows users to see that our site has been verified by that 3rd party.) 我们有一个网站,该网站应该加载由第三方提供的徽标(徽标是一个链接,允许用户查看我们的网站已被该第三方验证。)

To get this to work, we're told to include a short script in the head 为了使其正常工作,我们被告知在头中包含一个简短的脚本

<script type="text/javascript"> 
    //<![CDATA[ 
    var tlJsHost = ((window.location.protocol == "https:") ? 
        "https://secure.comodo.com/" : "http://www.trustlogo.com/");
    document.write(unescape("%3Cscript src='" + tlJsHost + 
        "trustlogo/javascript/trustlogo.js' 
        type='text/javascript'%3E%3C/script%3E"));
    //]]>
</script>

And another script in the body: 正文中的另一个脚本:

<script language="JavaScript" type="text/javascript">
    TrustLogo("https://ourfakesite.com/logo.png", "CL1", "none");
</script>

This all worked fine, initially: the external script is loaded, the function is run, the logo shows up. 最初,这一切工作正常:加载了外部脚本,运行了功能,显示了徽标。 Perfect. 完善。

The problem occurred when the remote site got really slow...all of our pages that load this logo suddenly became very slow as well, since the script is running synchronously. 当远程站点真的变慢时发生了问题...加载脚本的所有页面也突然变得非常慢,因为脚本正在同步运行。

Ideally, I'd like this to work as if it were designed as an ajax type call...load the page, and after the page is loaded, attempt to load the extra content. 理想情况下,我希望它能够像设计为ajax类型调用一样工作...加载页面,并在页面加载后尝试加载额外的内容。

I've tried some combinations of async/defer and using things like ajax, but it seems that because the JS is using a document.write, if the page is fully loaded, the document.write blows away the existing document before writing the new data; 我已经尝试了async / defer和ajax之类的组合,但由于JS使用的是document.write,因此如果页面已完全加载,则document.write会在写入新文件之前吹走现有文件。数据; the page loads...and then disappears and the logo appears. 页面加载...然后消失,徽标出现。 (I've seen some commentary explaining that this is expected behavior when document.write is used after the page is loaded.) (我看到一些评论解释说这是在加载页面后使用document.write时的预期行为。)

Is there a way to do this? 有没有办法做到这一点? Is there an alternate path I'm not considering? 是否有我未考虑的替代途径?

Looking at https://secure.comodo.com/trustlogo/javascript/trustlogo.js , the TrustLogo function itself uses document.write (indirectly, the code is minified, but eventually it does), which means you can't use those scripts asynchronously. 综观https://secure.comodo.com/trustlogo/javascript/trustlogo.js ,该TrustLogo功能本身使用document.write (间接,代码已经过压缩,但最终它),这意味着你不能使用这些异步脚本。 If you make the first script asynchronous and append that JavaScript file another way, then you have to make the second script asynchronous, and that would mean document.write (within the TrustLogo function) would be called after the main HTML parsing is complete, which in turn means that there'd be an implicit document.open , which would erase your page. 如果使第一个脚本异步并以另一种方式附加该JavaScript文件,则必须使第二个脚本异步,这意味着将在主HTML解析完成后调用document.write (在TrustLogo函数中)。依次意味着会有一个隐式document.open ,这将擦除您的页面。 :-( :-(

Of course...you could put all of that in an iframe on your main page, so that only the iframe , not your whole page, is impacted. 当然...你可以把所有这些都包含在iframe主页上,这样只有iframe ,而不是你的整个页面,会受到影响。 Provided that's not a violation of the terms of use of the logo (obviously, you'd use a relative path for the iframe so their code sees the right domain and such). 前提是不违反徽标使用条款(显然,您将为iframe使用相对路径,以便其代码能看到正确的域等)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM