简体   繁体   English

如何在Script标记内执行PHP函数?

[英]How Can I execute a PHP function inside a Script tag?

So I have as function for my css that looks like this 所以我的css具有如下功能

function include_css($css="") {
include(SITE_ROOT.DS.'includes'.DS.'stylesheets'.DS.$css);
}

And in the header I use this to call it 在标题中,我用它来称呼它

<style type="text/css"><?php include_css('public.css'); ?></style>

I would like to do a similar function to call a javascript file to load in ckeditor 我想做一个类似的功能来调用javascript文件加载到ckeditor中

function include_javascript($js="") {
include(SITE_ROOT.DS.'includes'.DS.'ckeditor'.DS.$js);
}

But this bit of code doesn't work 但是这段代码不起作用

<script type="text/javascript"><?php include_javascript('ckeditor.js'); ?></script>

The tool bar doesn't show up when doing it this way. 这样做时工具栏不会显示。 It will show up when I use the website. 当我使用该网站时,它将显示出来。

<script src="//cdn.ckeditor.com/4.5.11/standard/ckeditor.js"></script>

Why doesn't this work the same way as the Style tag? 为什么这与Style标签不一样? Since this doesn't work is there a similar way to put PHP into a Script tag? 由于这不起作用,有没有类似的方法可以将PHP放入Script标签?

I put php code inside of script tags frequently for instance to set the initial value of a javascript variable. 我经常将php代码放在脚本标签内,例如,以设置javascript变量的初始值。 There is no particular issue with inserting php tags inside of a script tag. 在脚本标签内插入php标签没有特别的问题。

Normally you would load javascript separately to speed page load like this: 通常,您将单独加载javascript以加快页面加载,如下所示:

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

So something like this should work: 所以这样的事情应该工作:

function include_javascript($js="") {
    if ($js)
        echo "<script src=\"/js-path/$js\" type=\"text/javascript\"></script>";
}

If you are trying to embed the javascript code into your html what you are doing ought to work. 如果您尝试将javascript代码嵌入到html中,那么您应该在做什么。 I noticed that you are setting a default value for your parameter. 我注意到您正在为参数设置默认值。 Make sure that something is actually passed and that the file actually exists. 确保确实传递了某些东西,并且文件确实存在。 If the result is 如果结果是

<script></script> 

then one of these is most likely the problem. 那么其中之一很可能是问题所在。

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

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