简体   繁体   English

实际上是php时,如何从服务器发送JS文件?

[英]How to send a JS file from my server when it actually is a php?

I need my server to send JS file but I need it to be actually a PHP generated file. 我需要服务器发送JS文件,但实际上需要它是PHP生成的文件。
I am using Apache with PHP. 我正在将Apache与PHP结合使用。

So that function named 所以那个函数命名

doSomething(){
};

can be

doSomething(){
         <?php
               if(registeredUser()){
                    echo "Doing something!";
               }
         ?> 
    };
  1. Give the file a php extension but load it like normal JavaScript 给文件一个php扩展名,但像普通JavaScript一样加载它

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

  2. Send the proper content-type in your PHP file: 在您的PHP文件中发送正确的内容类型:

    header("content-type: text/javascript");

This allows your server to create JavaScript with PHP. 这使您的服务器可以使用PHP创建JavaScript。

You don't need to do any magic. 您不需要做任何魔术。 The browser doesn't care what created the Javascript, so long as the Javascript is valid! 只要Javascript有效,浏览器就不在乎创建Javascript的原因!

That said, you should send the correct Content-type header: 也就是说,您应该发送正确的Content-type标头:

header('Content-type: text/javascript');

You can do this by simply using a PHP file as your script's src attribute: 您可以通过简单地使用PHP文件作为脚本的src属性来执行此操作:

<script src="myfile.php"></script>

However, having another PHP script run is not ideal from a resources point of view. 但是,从资源角度来看,运行另一个PHP脚本并不理想。 Another PHP process is triggered, you have to take care of caching, etc. 触发了另一个PHP进程,您必须注意缓存等问题。

Consider whether you can't have static code in your JS file, and set the condition you're checking for in your main file, where you have PHP running anyway. 考虑一下您的JS文件中是否不能包含静态代码,并在要运行PHP的主文件中设置要检查的条件。

In the JavaScript file (myfile.js): 在JavaScript文件(myfile.js)中:

function doSomething()
   { if (logged_in) do_something() }

In the main file's <head> , do: 在主文件的<head> ,执行以下操作:

<script>
<?php if (registeredUser()): ?>
 logged_in = true;
<?endif; ?>
<script>

and then include the static JS file as usual. 然后照常添加静态JS文件。

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

相关问题 如何在php中将文件从本地计算机发送到我的服务器? - How to send file from local computer to my server in php? 如何在PHP中发出HTTP POST XML请求实际上将XML数据发送到供应商的服务器? - How to make my HTTP POST XML request in PHP actually send XML data to my vendor's Server? 如何将本地存储从 JS 发送到 PHP 文件(?) - How to send localStorage from JS to PHP file (?) 当MX是另一台服务器时如何从我的Linux服务器将php mail()发送到user@domain.com - How send php mail() to user@domain.com from my linux server when mx is another server 如何将AJAX回调发送到我自己的服务器/ php文件 - How to send AJAX callback to my own server/php file 如何使用套接字将文件从PHP客户端发送到Java服务器 - How to send file from php client to java server with sockets 如何将文件从服务器发送到电报机器人php - how to send file from server to telegram bot php 如何将数据从托管在 Heroku 上的 Node.js 应用程序发送到托管在完全独立的(Cpanel)服务器上的 PHP 文件? - How can I send data from a Node.js application hosted on Heroku to a PHP file hosted on a completely separate (Cpanel) server? 如何将发布参数从我的android应用发送到php文件 - How to send a post parameter from my android app to php file 如何从安装程序将数据发送到php文件? - How do I send data to a php file from my installer?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM