包括jQuery,如果不包括在内

Tho*_*son 9 javascript php jquery

我想将jquery包含在文件中,如果不包含在页面中的话.我怎样才能做到这一点?

我写了这个,但它给出了不想要的输出.

<script type="text/javascript">
if ('undefined' == typeof window.jQuery) {
    // jQuery not present
//  alert("Jquery Unavailable");
<?php echo '<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.1.min.js"></script>'; ?>
} else {
    // jQuery present
    alert("Jquery Available");
}
</script>
Run Code Online (Sandbox Code Playgroud)

Set*_*ath 16

你不能用PHP做到这一点.试试这个

<script type="text/javascript">

if(typeof jQuery == 'undefined'){
        document.write('<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.1.min.js"></'+'script>');
  }

</script>
Run Code Online (Sandbox Code Playgroud)


noo*_*oob 10

<script type="text/javascript">
    if(typeof jQuery == 'undefined'){
        var oScriptElem = document.createElement("script");
        oScriptElem.type = "text/javascript";
        oScriptElem.src = "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.1.min.js";
        document.head.insertBefore(oScriptElem, document.head.getElementsByTagName("script")[0])
    }
</script>
Run Code Online (Sandbox Code Playgroud)


小智 9

您可以尝试以下方法:

<script>window.jQuery || document.write('<script src="//ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.1.min.js"><\/script>')</script>
Run Code Online (Sandbox Code Playgroud)