te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>How to output a javascript file from a PHP file that can be referenced from a script include tag - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to output a javascript file from a PHP file that can be referenced from a script include tag - Stack Overflow

programmeradmin3浏览0评论

I am looking for a solution in PHP that will allow me to reference a PHP file that outputs javascript as a script includes in the header of my html page. The reason i'm looking to do this is because I have some dynamically generated javascript which I want the browser to cache.

I have done this before in ASP.NET by using a .ashx handler, but i'm not sure how to do it in PHP.

Here's a more detailed breakdown of what i'm trying to achieve.

<!DOCTYPE html>
<html lang="en">
        <script type="text/javascript" src="javascriptHandler.php"></script>
<head>
</head>

In the above example I want the javascriptHandler.php to respond with a javascript file once it's requested. The javascriptHandler.php needs to check the ining request headers and determine whether the file already exists on the client and return the appropriate response.

I'm looking for any clean solution that will do what is described above or any links that will point me in the right direction. If there's a better way to include and cache dynamically generated javascript please post it here.

I am looking for a solution in PHP that will allow me to reference a PHP file that outputs javascript as a script includes in the header of my html page. The reason i'm looking to do this is because I have some dynamically generated javascript which I want the browser to cache.

I have done this before in ASP.NET by using a .ashx handler, but i'm not sure how to do it in PHP.

Here's a more detailed breakdown of what i'm trying to achieve.

<!DOCTYPE html>
<html lang="en">
        <script type="text/javascript" src="javascriptHandler.php"></script>
<head>
</head>

In the above example I want the javascriptHandler.php to respond with a javascript file once it's requested. The javascriptHandler.php needs to check the ining request headers and determine whether the file already exists on the client and return the appropriate response.

I'm looking for any clean solution that will do what is described above or any links that will point me in the right direction. If there's a better way to include and cache dynamically generated javascript please post it here.

Share Improve this question asked Mar 2, 2015 at 8:27 Jako BassonJako Basson 1,53113 silver badges21 bronze badges 6
  • Why don't you just add some php code to that file? E.g. <?php if( /* condition /* ) { ?> <script> //JAVASCRIPT </script> <?php } ?> – ksbg Commented Mar 2, 2015 at 8:31
  • I need the browser to cache the javascript. Currently the javascript is being referenced inline, but that is exactly what i'm trying to avoid. – Jako Basson Commented Mar 2, 2015 at 8:32
  • Also if the javascript files gets requested after the page is already rendered, you'll need AJAX to do that. – ksbg Commented Mar 2, 2015 at 8:32
  • The idea is that the javascriptHandler.php is requested exactly the same way a normal .js file is requested by the browser. Like i've said in my question, i've done this before with asp, but i could not find a PHP solution for this. – Jako Basson Commented Mar 2, 2015 at 8:47
  • 2 Your Code-Snippet is correct for your solution. Make sure you send the right Headers in your javascriptHandler.php (for cache-control and mime-type). – RobSky Commented Mar 2, 2015 at 9:24
 |  Show 1 more ment

2 Answers 2

Reset to default 12

Put JavaScript Headers in PHP file

Keep your HTML page the same. Call it as a .php file:

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

In your javascriptHandler.php file, add this to the very top of the file:

<?php
header("Content-Type: application/javascript");
header("Cache-Control: max-age=604800, public");
?>

Then you can put your regular javascript below. All together it will look like:

<?php
header("Content-Type: application/javascript");
header("Cache-Control: max-age=604800, public");
?>

function bob(){
    alert('hello')
}
bob();

and your browser will treat your javascriptHandler.php file like a JavaScript resource. Normal caching rules will apply.

URL Rewrites on the web server:

This will rewrite the requested .js filename to read your .php file instead.

This is known as an 'Internal' Rewrite so the web-browser will not know the file is actually .php. It will believe it is the .js file it requested.

Apache

If you are using an Apache webserver, you can make an .htaccess file in the same directory as your javascriptHandler.php file.

Inside .htaccess, put the following:

RewriteEngine On    # Turn on the rewriting engine
RewriteRule    javascriptHandler.js$  javascriptHandler.php [PT]

Nginx

You can do this with an Nginx web-server also, but you have to do it inside of your /etc/nginx/sites-available/yoursite config file for your server.

I am not going into how to do rewrites on Nginx because it is much more plicated than Apache. But it is basically the following:

server{rewrite javascriptHandler.js$  javascriptHandler.php last;}

Just to Mention it

After these changes, you will call the file in your HTML page as you would normally in order to cache it:

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

On your webserver, you will save the file as .php. This will allow you to use PHP to build dynamic JavaScript within the file.

/path/to/javascriptHandler.php

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论