简体   繁体   English

如何在php中包含js文件?

[英]how to include js file in php?

I have a problem with an js file in php.我在 php 中有一个 js 文件的问题。 if i include it like this:如果我像这样包含它:

?> <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谢谢,塞巴斯蒂安

PHP is completely irrelevant for what you are doing. PHP 与您正在做的事情完全无关。 The generated HTML is what counts.生成的 HTML 才是最重要的。

In your case, you are missing the src attribute.在您的情况下,您缺少src属性。 Use

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

In your example you use the href attribute to tell where the JavaScript file can be found.在您的示例中,您使用href属性来告诉在哪里可以找到 JavaScript 文件。 This should be the src attribute:这应该是src属性:

?> <script type="text/javascript" src="file.js"></script> <?php

For more information see w3schools .有关更多信息,请参阅w3schools

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"> <script type="text/javascript">\n    document.write('<script type="text/javascript" src="datetimepicker_css.js"></script>'); document.write('<script type="text/javascript" src="datetimepicker_css.js"></script>');\n</script> </脚本>

the first tag in the document.write function closes document.write 函数中的第一个标签关闭
what is the correct way to do this?这样做的正确方法是什么?

You don't want or need document.write for this, but just in case you ever do need 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> .为此,您不需要或不需要document.write ,但以防万一您确实需要将字符</script>放在script标签中,因为其他原因:您通过确保 HTML 解析器(它不会'不懂 JavaScript) 看不到文字</script> There are a couple of ways of doing that.有几种方法可以做到这一点。 One way is to escape the / even though you don't need to:一种方法是逃避/即使您不需要:

<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 解析器不会。

I found a different solution that I like:我找到了一个我喜欢的不同解决方案:

<script>
    <?php require_once("/path/to/file.js");?>
</script>

Also works with style-tags and .css-files in the same way.也以同样的方式处理 style-tags 和 .css-files。

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来指定 js 文件的位置,而您应该使用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");

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 .您绝对不必执行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.您的 JS 与您的 PHP 无关,因此第一个示例中肯定存在其他错误(例如文件路径)。

On a side note, you'll have problems using </script> within a script.附带说明一下,在脚本中使用</script>会遇到问题。 The HTML parser sees the closing script tag when scanning the JS before parsing it, and becomes confused. HTML 解析器在解析之前扫描 JS 时看到关闭脚本标记,并变得混乱。 You see people doing things like '<'+'/script' for this reason.出于这个原因,您会看到人们在做诸如'<'+'/script'的事情。

Edit:编辑:

As TJ Crowder pointed out, actually you need to change 'href to src in the <script> tag.正如 TJ Crowder 指出的那样,实际上您需要将<script>标记中的'href更改为src Oops, actually Pekka pointed that out in his answer itself, so actually adding it here is totally superfluous.糟糕,实际上 Pekka 在他的回答中指出了这一点,所以实际上在这里添加它是完全多余的。

Its more likely that the path to file.js from the page is what is wrong.从页面到 file.js 的路径更有可能是错误的。 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.只要当您查看页面和查看源代码时您看到标签,它就可以工作,现在是时候调试您的路径是否过于相关,也许您需要在它前面加上 / 。

I tried this, I've got something like我试过这个,我有类似的东西

script type="text/javascript" src="createDiv.php?id=" script

AND In createDiv.php I Have并且在createDiv.php我有

document getElementbyid(imgslide).appendchild(imgslide5).innerHTML = 'php echo $helloworld; ';

And I got supermad because the php at the beginning of the createDiv.php I made the $helloWorld php variable was formatted cut and paste from the html page我变得超级疯狂,因为在createDiv.php开头的 php 我制作了$helloWorld 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 空格仍然有效,但不是这个。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM