在ajax完成之后,加载<script>标签,但只加载具有特定id </script>的标签

时间:2013-09-16 23:48:30

标签: javascript php jquery ajax

我目前正在使用无限滚动来加载更多内容,内容包含需要运行的<script>个标记。所以我将以下代码用作ajax-callback

JS 加载ajax回调:

function ajaxLoadedCallback() {
      // contentElem is the HTML element you've loaded via ajax
      scriptx = document.getElementsByTagName("script");

      // Load scripts into new array because 
      // scriptx seems to change as scripts execute, at least in some browsers
    scripts = new Array();
    for (var idx=0; idx<scriptx.length; idx++) {

        if (jQuery(scriptx[idx]).is("#inline-comments")) {
                scripts[idx] = scriptx[idx].text;
        }

    }

        console.log (scripts[idx]);
      // execute each script in turn
      for(idx=0; idx<scripts.length; ++idx) {
         if (scripts[idx].length!=0) {
            try {
              // create a function for the script & execute it
              f = new Function(scripts[idx]);
              f();
            } catch(se) {

            } // end try-catch
         } // end if
      } // end for

}

每个内容帖子都有我要加载的脚本标记:

<script id="inline-comments" >
    console.log ('<?php echo $post->ID; ?>' + 'has loaded...');
    var tid_<?php echo $post->ID; ?> = setInterval( function () {
    if ( document.readyState !== 'complete' ) return;
        clearInterval( tid_<?php echo $post->ID; ?> );       
        inline_comments_ajax_load(<?php echo $post->ID; ?>)
    }, 100 );   
</script>

获取未捕获的TypeError:无法读取此行中未定义错误的属性“长度”:if (scripts[idx].length!=0) {

ajax回调在正确的时间加载,但因为我认为scripts[idx]为空而中断。但为什么?当我使用console.log()时,它显示scriptx[idx]有id和id的名称。

更新谢谢PSL! console.log表明它正在查找脚本,但scripts.length返回零

function ajaxLoadedCallback() {
    scriptx = document.getElementsByTagName("script");


    scripts = new Array();
    for (var idx=0; idx<scriptx.length; idx++) {

        if (jQuery(scriptx[idx]).is(".inline-comments-script")) {
            console.log (".inline-comments-scriptfound!");
            console.log ("####### .text #######");
            console.log (scriptx[idx].text);
            console.log ("####### .innerHTML ######");
            console.log (scriptx[idx].innerHTML);
            scripts.push = scriptx[idx].innerHTML;
        }

    }
    console.log ("####### END ######");
    console.log ("Found "+scripts.length+ " script(s)");

      // execute each script in turn
      for(idx=0; idx<scripts.length; ++idx) {
        var content = scripts[idx];
            if (content.length) {
                try {
              // create a function for the script & execute it
              f = new Function(content);
              f();
            } catch(se) {

            } // end try-catch
         } // end if
      } // end for

}

立即行动!请参阅下面的PSL答案 - 如果你有wordpress,你也可以看到它:https://github.com/MattMcFarland/inline-ajax-comments - 效果很好!

1 个答案:

答案 0 :(得分:3)

这里有几个问题。

for (var idx=0; idx<scriptx.length; idx++) {

        if (jQuery(scriptx[idx]).is("#inline-comments")) {
                scripts[idx] = scriptx[idx].text;
        }

    }

你正在递增循环迭代变量,尽管没有匹配,你不想将任何项目推送到数组。因此,如果第一个脚本与您的条件不匹配scripts[0]将是未定义的,因此当您在条件length

中访问未定义的属性if (scripts[idx].length!=0) {时,您会收到错误

另一个问题是scriptx[idx].text此处的文本是一个函数,因此您需要元素的真实文本而不是函数引用,因此您应该编写jQuery(scriptx[idx]).text()scriptx[idx].innerHTML。由于你使用id和id不应该重复,你可以简单地写为:

     var txt = jQuery('#inline-comments').text();
     if (txt.length) {
        try {
          // create a function for the script & execute it
          f = new Function(scripts[idx]);
          f();
        } catch(se) {

        } // end try-catch
     } // end if

如果您打算将inline-comments转换为来自ID的类,并且您希望访问其中的多个,那么这应该可以。

 scripts = [];
    for (var idx=0; idx<scriptx.length; idx++) {
        if (jQuery(scriptx[idx]).is(".inline-comments")) {
                scripts.push(scriptx[idx].innerHTML);
        }
     }
     // execute each script in turn
      for(idx=0; idx<scripts.length; ++idx) {
          var content = scripts[idx];
         if (content.length) {
            try {
              // create a function for the script & execute it
              f = new Function(content);
              f();
            } catch(se) {

            } // end try-catch
         } // end if
      } // end for