javascript 包含 jQuery,如果尚未包含

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10371211/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 09:38:31  来源:igfitidea点击:

Include jQuery, if not included already

javascriptphpjquery

提问by Thompson

I want to include the jquery in file, if not included already in the page. How can I do this?

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

I wrote this, but it's giving undesired output.

我写了这个,但它给出了不需要的输出。

<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>

回答by Sethunath

you cant do that in PHP. Try this instead

你不能在 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>

回答by noob

<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>

回答by px4n

You could try the below:

你可以试试下面的:

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

回答by Quentin

Your PHP is running on the server and outputting some text.

您的 PHP 正在服务器上运行并输出一些文本。

The browser then interprets that text as HTML and JavaScript.

然后浏览器将该文本解释为 HTML 和 JavaScript。

You can't run PHP on the server, in a document it has already sent to the browser, in response to logic running in the browser.

你不能在服务器上运行 PHP,在它已经发送到浏览器的文档中,以响应浏览器中运行的逻辑。

You either need to this entirely in JavaScript or, more sanely, sort out your server side logic so you track if jQuery has been included or not.

您要么需要完全在 JavaScript 中执行此操作,要么更明智地整理您的服务器端逻辑,以便跟踪是否已包含 jQuery。

回答by webicy

If you want to do it with PHP, which i think is better, you can do this

如果你想用 PHP 来做,我认为这更好,你可以这样做

  • before you call the load() function create a variable like :

    var load_script = 'no';

  • the load() function takes a optional parameter that you can use to send data to the server. Use it to post the load_scriptvariable you created earlier like this

    $('#receiver_div').load('mypage.html',{'load_script':load_script},function(html){

    //stuff to do after the page is load

    })

  • Then in the page you are loading, catch the posted value like this

    $load_script = $_POST('load_script');

  • Then you can do a if statement to exclude the script you want like this

  • 在调用 load() 函数之前创建一个变量,如:

    var load_script = '否';

  • load() 函数采用一个可选参数,您可以使用该参数将数据发送到服务器。使用它来发布您之前创建的load_script变量,如下所示

    $('#receiver_div').load('mypage.html',{'load_script':load_script},function(html){

    //页面加载后要做的事情

    })

  • 然后在您正在加载的页面中,像这样捕获发布的值

    $load_script = $_POST('load_script');

  • 然后你可以做一个 if 语句来排除你想要的脚本

if($load_script != 'no') {

echo "<script src="jquery.js"></script>";

}

如果($load_script != '否'){

echo "<script src="jquery.js"></script>";

}