最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - PHP require_once() .js file - Stack Overflow

programmeradmin2浏览0评论

Okay, so I'm using require_once to load the contents of a .js file. However, there seems to be a 1 added to the end of the file after the require is done.

PHP:

echo require_once("test.js");

JS:

var newFunc = function() {
    console.log('yay!');
};

rendered html when the PHP file is loaded:

var newFunc = function() {
    console.log('yay!');
};1

Okay, so I'm using require_once to load the contents of a .js file. However, there seems to be a 1 added to the end of the file after the require is done.

PHP:

echo require_once("test.js");

JS:

var newFunc = function() {
    console.log('yay!');
};

rendered html when the PHP file is loaded:

var newFunc = function() {
    console.log('yay!');
};1
Share Improve this question asked Jun 24, 2014 at 9:16 CJT3CJT3 2,9087 gold badges33 silver badges45 bronze badges 8
  • 1 The way you are doing it seems pletely contrary to best practices. You should add your scripts by using script tags. – Ermir Commented Jun 24, 2014 at 9:20
  • Objection noted, but this is for a document loaded via Ajax. I have a function that appends JS code to a script tag after the request has loaded. – CJT3 Commented Jun 24, 2014 at 9:23
  • 1 require_once is closely same as include and is mainly targeted for including php-scripts. Do not use it to include js. – Fdr Commented Jun 24, 2014 at 9:23
  • Seems to me you are leaving your site wide open to a JS injection vulnerability. Maybe you can see some other way you could achieve your goals? – Ermir Commented Jun 24, 2014 at 9:27
  • 1 Place your different scripts in different files, and then get only the URL of the script that you need. This way, you can then just create a script element in HTML and set the src attribute as the URL you just got. What this achieves is guaranteeing that only code written in predefined script files can be loaded, so it's safer. To really secure such as system, make sure to activate Same Origin Policy – Ermir Commented Jun 24, 2014 at 9:59
 |  Show 3 more ments

3 Answers 3

Reset to default 9

require_once() returns 1 if the include was successful. So you're echoing the return value, which is not what you thought it was.

Use file_get_contents() instead, although this is odd usage, you're probably heading in the wrong direction with whatever you're trying to do with this...

If you want to import a script "on the fly" and want it to be only loaded once, we can "bine" require_once with a script, like so:

<script>
  <?php require_once('your-script.js'); ?>
</script>
<div>Foo</div>

Now, let's you have a ponent with that structure. If some of your templates includes that ponent many times, your script will only be embedded once, in the first one.

You should remove echo from "echo require_once("test.js");"

you can include the js file by following method

<script src="test.js" language="javascript"> </script>
发布评论

评论列表(0)

  1. 暂无评论