javascript 如何将javascript添加到php页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14198823/
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 20:59:11  来源:igfitidea点击:

How to add javascript to php page

javascript

提问by user1953708

I see a lot of script adding Javascript to their webpages in different ways and am trying to figure out the correct way to do it. For example, in the header of one of the php scripts I use it has this:

我看到很多脚本以不同的方式将 Javascript 添加到他们的网页中,我正试图找出正确的方法来做到这一点。例如,在我使用的其中一个 php 脚本的标题中,它包含以下内容:

<script type="text/javascript" src="/javascriptfile.js"></script>

<script type="text/javascript">
var stuff = "file.php";                                         
var ip_add = '32.42.42.442';
</script>

What I don't understand is why would you ever put the full javascript code in the header instead of just including it within a file. For example, why not move the javascript mentioned about into it's own file and just just use this in your header:

我不明白的是为什么你要把完整的 javascript 代码放在标题中,而不是仅仅将它包含在一个文件中。例如,为什么不将提到的 javascript 移动到它自己的文件中,而只需在标题中使用它:

<script type="text/javascript" src="/javascriptfile.js"></script>

<script type="text/javascript" src="/javascriptfile2.js"></script>

Are there certain times you should have the full javascript displayed in the page source instead of just linking to it in its own javascript file?

是否有某些时候您应该在页面源代码中显示完整的 javascript 而不是仅在其自己的 javascript 文件中链接到它?

回答by Quentin

What I don't understand is why would you ever put the full javascript code in the header instead of just including it within a file.

我不明白的是为什么你要把完整的 javascript 代码放在标题中,而不是仅仅将它包含在一个文件中。

It costs you caching.This is a long term penalty. The impact of that depends on how often the script will be used by the browser

它会花费你缓存。这是一个长期的惩罚。其影响取决于浏览器使用脚本的频率

It saves you an HTTP request.This is a short term bonus. It saves you a bit of time when loading the script in the first place.

它为您节省了一个 HTTP 请求。这是短期奖金。它首先在加载脚本时为您节省了一些时间。

This has nothing to do with PHP though. It applies to any HTML document.

不过这与 PHP 无关。它适用于任何 HTML 文档。

回答by Richard A.

Some of this is "legacy". At one point, you HAD to put <script>tags in the <head>portion of your markup, and so this is where most examples put it.

其中一些是“遗产”。在某一时刻,您必须在<script>标记的<head>部分中放置标签,因此这是大多数示例放置的位置。

If you add a srcreference to an external file, you can reuse the script as a resource on other pages that call for this. If you are using the same script all over the place, put it in a "js" directory and the browser won't fetch a new copy each time. This helps with bandwidth.

如果您添加对外部文件的src引用,您可以将该脚本作为其他页面上的资源重用。如果您到处使用相同的脚本,请将其放在“js”目录中,浏览器不会每次都获取新副本。这有助于带宽

If, however, you add the raw script to your page, the whole page (minus images and other "embedded" content) will arrive in one thread. This helps with load times.

但是,如果您将原始脚本添加到您的页面,整​​个页面(减去图像和其他“嵌入”内容)将在一个线程中到达。这有助于缩短加载时间

Unless you're expecting 10,000+ pageviews in a short space of time, I wouldn't worry too much either way.

除非您期望在短时间内获得 10,000 多次综合浏览量,否则我不会太担心。

Oh, and one other thing to consider: http://developer.yahoo.com/performance/rules.html#js_bottom-- why you should put your scripts at the bottomof your document.

哦,还有一件事要考虑:http: //developer.yahoo.com/performance/rules.html#js_bottom——为什么你应该把你的脚本放在文档的底部

回答by Nic

I totally agree with @Quentin. Additionally I would suggest putting your scripts in seperate .js files and include them - for reasons of structuring - not only in large projects.

我完全同意@Quentin。此外,我建议将您的脚本放在单独的 .js 文件中并包含它们 - 出于结构的原因 - 不仅在大型项目中。

One thing that could lead you to put the JS code into a .php file however could be if you need to generate code using PHP or if you want to use information that is e.g. pulled from a database directly like this:

可能导致您将 JS 代码放入 .php 文件的一件事可能是,如果您需要使用 PHP 生成代码,或者您想使用直接从数据库中提取的信息,如下所示:

<?php
    $foo = getSomeInformation();
?>

<script type="text/javascript">
   var someVar = <?=$foo?>;
</script>