Javascript 如何在php中包含js文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4238353/
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
how to include js file in php?
提问by sebastian
I have a problem with an js file in php. if i include it like this:
我在 php 中有一个 js 文件的问题。如果我像这样包含它:
?> <script type="text/javascript" href="file.js"></script> <?php
the file isn't included and i get an error that the function isn't defined.
该文件未包含在内,我收到一个错误,指出该函数未定义。
When i try it this way:
当我这样尝试时:
<script type="text/javascript">
document.write('<script type="text/javascript" src="datetimepicker_css.js"></script>');
</script>
the first tag in the document.write function closes <script type="text/javascript">
document.write 函数中的第一个标签关闭 <script type="text/javascript">
what is the correct way to do this?
这样做的正确方法是什么?
thanks, sebastian
谢谢,塞巴斯蒂安
回答by Pekka
PHP is completely irrelevant for what you are doing. The generated HTML is what counts.
PHP 与您正在做的事情完全无关。生成的 HTML 才是最重要的。
In your case, you are missing the src
attribute. Use
在您的情况下,您缺少该src
属性。用
<script type="text/javascript" src="file.js"></script>
回答by T.J. Crowder
Pekka has the correct answer(hence my making this answer a Community Wiki): Use src
, not href
, to specify the file.
Pekka 有正确的答案(因此我将此答案设为社区 Wiki):使用src
,而不是href
指定文件。
Regarding:
关于:
When i try it this way:
<script type="text/javascript"> document.write('<script type="text/javascript" src="datetimepicker_css.js"></script>'); </script>the first tag in the document.write function closes
what is the correct way to do this?
当我这样尝试时:
<script type="text/javascript"> document.write('<script type="text/javascript" src="datetimepicker_css.js"></script>'); </script>document.write 函数中的第一个标记关闭
执行此操作的正确方法是什么?
You don't want or need document.write
for this, but just in case you ever doneed to put the characters </script>
inside a script
tag for some other reason: You do that by ensuring that the HTML parser (which doesn't understand JavaScript) doesn't see a literal </script>
. There are a couple of ways of doing that. One way is to escape the /
even though you don't need to:
您不想要或不需要这样做document.write
,但以防万一您确实出于其他原因需要将字符</script>
放入script
标签中:您通过确保 HTML 解析器(不理解 JavaScript)不会看到文字</script>
。有几种方法可以做到这一点。一种方法是逃避/
即使你不需要:
<script type='text/javascript'>
alert("<\/script>"); // Works, HTML parser doesn't see this as a closing script tag
// ^--- note the seemingly-unnecessary backslash
</script>
Or if you're feeling more paranoid:
或者,如果您感觉更偏执:
<script type='text/javascript'>
alert("</scr" + "ipt>"); // Works, HTML parser doesn't see this as a closing script tag
</script>
...since in each case, JavaScript sees the string as </script>
but the HTML parser doesn't.
...因为在每种情况下,JavaScript 都将字符串视为,</script>
而 HTML 解析器则不会。
回答by Veger
回答by RobertPitt
I have never been a fan of closing blocks of PHP to output content to the browser, I prefer to have my output captured so if at some point within my logic I decide I want to change my output (after output has already been sent) I can just delete the current buffer.
我从来不喜欢关闭 PHP 块以将内容输出到浏览器,我更喜欢捕获我的输出,所以如果在我的逻辑中的某个时刻我决定要更改我的输出(在输出已经发送之后)我可以只删除当前缓冲区。
But as Pekka said, the main reason you are having issues with your javascript inclusion is because your using href
to specify the location of the js file where as you should be using src
.
但正如 Pekka 所说,您在包含 javascript 时遇到问题的主要原因是因为您href
使用src
.
If you have a functions file with your functions inside then add something like:
如果您有一个包含函数的函数文件,则添加如下内容:
function js_link($src)
{
if(file_exists("my/html/root/" . $src))
{
//we know it will exists within the HTTP Context
return sprintf("<script type=\"text/javascript\" src=\"%s\"></script>",$src);
}
return "<!-- Unable to load " . $src . "-->";
}
The n in your code without the need for closing your blocks with ?>
you can just use:
代码中的 n 无需关闭块?>
即可使用:
echo js_link("jquery/1.6/main.js");
回答by DevelopingChris
Its more likely that the path to file.js from the page is what is wrong. as long as when you view the page, and view-source you see the tag, its working, now its time to debug whether or not your path is too relative, maybe you need a / in front of it.
从页面到 file.js 的路径更有可能是错误的。只要当您查看页面和查看源代码时,您就会看到标签,它正在工作,现在是时候调试您的路径是否太相关了,也许您需要在它前面加一个 / 。
回答by JAL
There is no difference between HTML output by PHP and a normal HTML page in this regard.
PHP 输出的 HTML 与普通 HTML 页面在这方面没有区别。
You should definitely not have to do the document.write
. Your JS is not related to your PHP, so there must be some other error (like the file path) in the first example.
您绝对不必执行document.write
. 您的 JS 与您的 PHP 无关,因此在第一个示例中肯定有其他一些错误(如文件路径)。
On a side note, you'll have problems using </script>
within a script. The HTML parser sees the closing script tag when scanning the JS before parsing it, and becomes confused. You see people doing things like '<'+'/script'
for this reason.
附带说明一下,您</script>
在脚本中使用时会遇到问题。HTML 解析器在解析之前扫描 JS 时看到关闭脚本标记,并变得混乱。你会看到人们'<'+'/script'
因为这个原因做这样的事情。
Edit:
编辑:
As T.J. Crowder pointed out, actually you need to change 'href
to src
in the <script>
tag. Oops, actually Pekka pointed that out in his answer itself, so actually adding it here is totally superfluous.
正如 TJ Crowder 指出的那样,实际上您需要在标签中更改'href
为。糟糕,实际上 Pekka 在他的回答中指出了这一点,所以实际上在这里添加它是完全多余的。src
<script>
回答by user2172337
If you truly wish to use PHP, you could use
如果你真的想使用 PHP,你可以使用
include "file.php";
or
或者
require "file.php";
and then in file.php, use a heredoc & echo it in.
然后在 file.php 中,使用 heredoc 并将其回显。
file.php contents:
$some_js_code <<<_code
function myFunction()
{
Alert("Some JS code would go here.");
}
_code;
At the top of your PHP file, bring in the file using either include or require then in head (or body section) echo it in
在 PHP 文件的顶部,使用 include 或 require 引入文件,然后在 head(或 body 部分)中将其回显
<?php
require "file.php";
?>
<html>
<head>
<?php
echo $some_js_code;
?>
</script>
</head>
<body>
</body>
</html>
Different way but it works. Just my $.02...
不同的方式,但它的工作原理。只是我的 $.02...
回答by nick
I tried this, I've got something like
我试过这个,我有类似的东西
script type="text/javascript" src="createDiv.php?id=" script
script type="text/javascript" src="createDiv.php?id=" script
AND In createDiv.phpI Have
并且在createDiv.php我有
document getElementbyid(imgslide).appendchild(imgslide5).innerHTML = 'php echo $helloworld; ';
document getElementbyid(imgslide).appendchild(imgslide5).innerHTML = 'php echo $helloworld; ';
And I got supermad because the php at the beginning of the createDiv.phpI made the $helloWorld
php variable was formatted cut and paste from the html page
我变得超级疯狂,因为在createDiv.php开头的$helloWorld
php我制作的php 变量被格式化为从 html 页面剪切和粘贴
But it wouldn't work cause Of whitespaces was anyone gonna tell anyone about the whitespace problem cause my real php whitespace still works but not this one.
但它不起作用,因为空格是任何人都会告诉任何人有关空格问题的原因,因为我真正的 php 空格仍然有效,但不是这个。