简体   繁体   English

Javascript,可以检查HTML中是否存在函数<script> tags?

[英]Javascript, possible to check whether function exists in HTML <script> tags?

I have the following setup: a Javascript file (containing a google visualization function which Ajax will put into a new Div) and a vbscript file, containing implementation function for the Ajax call, which writes google visualisation javascript within tags. 我有以下设置:一个Javascript文件(包含Ajax将放入新Div中的google可视化功能)和一个vbscript文件,其中包含Ajax调用的实现功能,该功能在标记内写入google可视化javascript。

In the Ajax call in the javascript file, I put the content of the Ajax call into a new div and append this to the container div. 在javascript文件的Ajax调用中,我将Ajax调用的内容放入新的div中,并将其附加到容器div中。 I then call one of the google javascript functions, which is the new content the Ajax call is adding to the html. 然后,我调用Google javascript函数之一,这是Ajax调用添加到html的新内容。

If i have an alert or setTimeOut() before the google javascript function call, it works. 如果我在Google javascript函数调用之前有警报或setTimeOut(),它会起作用。 If i just immediately call the function it doesnt. 如果我只是立即调用该函数,则不会。 So it appears there is some delay before the newly Ajax-added javascript code can be recognised. 因此,似乎无法识别新添加的Ajax的javascript代码。

Is there any elegant way to call the function without a timeout? 有没有什么优雅的方法可以在没有超时的情况下调用该函数? I did write a while loop to keep looping until the new appended node was found, but the function call was still not recognised. 我确实编写了一个while循环来保持循环,直到找到新的附加节点为止,但仍无法识别该函数调用。

    function ProcessAjaxCall(DivID, ResponseText)
    {
        var NewDiv = document.createElement("div"); 
        NewDiv.id = "myscript";
        NewDiv.innerHTML = ResponseText;
        document.getElementById(DivID).appendChild(NewDiv);
        //alert('Function below only works when this outputs');
        MyGoogleJSFunction('Arg');
    }

So MyGoogleJSFunction is part of the code being written via the Ajax call and is within NewDiv. 因此,MyGoogleJSFunction是通过Ajax调用编写的代码的一部分,位于NewDiv中。

This is because the parser cannot rerender the current page when JavaScript is executed (at least not yet). 这是因为执行JavaScript(至少现在还没有)时解析器无法重新呈现当前页面。 You'll have to give control back to let the browser load and execute the included script. 您必须将控制权交还给浏览器,并执行其中包含的脚本。 One way of doing this, is using setTimeout (eg with a delay of 0 ); 一种方法是使用setTimeout (例如,延迟为0 ); another is by closing and reopening the current script element, like Google Analytics does: 另一种方法是关闭并重新打开当前脚本元素,就像Google Analytics(分析)一样:

<script type="text/javascript">
    var _gaq=_gaq||[];
    _gaq.push(['_setAccount','UA-XXXXXXX-X']);
    _gaq.push(['_trackPageview']);
    (function(){
        var ga=document.createElement('script');
        ga.type='text/javascript';
        ga.async=true;ga.src='http://www.google-analytics.com/ga.js';
        var s=document.getElementsByTagName('script')[0];
        s.parentNode.insertBefore(ga,s);
    })();
    _qoptions={qacct:"p-c1rF4kxgLUzNc"};
    /* now close this script element, so we can use functions included
     * by the GA script */
</script>
<script>
    // …
</script>

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

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