php 动态加载JavaScript文件或者css文件

 

1. 动态加载JS文件

 

第一种方法:

 

test.php

 

<script language="JavaScript" src="test6.php?str=i love bainiangzi"></script>
<script>tester();</script>

 

test6.php

 

<?php
header('Content-Type: application/x-javascript; charset=UTF-8');
$str = $_GET["str"];
?>

// javascript document

alert('<?php echo $str; ?>');

function tester(string)
{
	string ? alert(string) : alert('you call a function named tester');
}

 

 

 第二种方法:

 

源于:http://www.phper.org.cn/?post=44

 

test.php

 

<script>
function loadjs(url,callback){
	var head = document.getElementsByTagName("head")[0];
	var script = document.createElement('script');
	script.onload = script.onreadystatechange = script.onerror = function (){
		if (script && script.readyState && /^(?!(?:loaded|complete)$)/.test(script.readyState)) return;
		script.onload = script.onreadystatechange = script.onerror = null;
		script.src = '';
		script.parentNode.removeChild(script);
		script = null;
		callback();
	}
	script.charset = "gb2312";
	script.src = url;
	try {
		head.appendChild(script);
	} catch (exp) {}
}

function loadmultijs(url,callback){
	if(Object.prototype.toString.call(url)==='[object Array]'){	//是否数组
		this.suc = 0;			//加载计数
		this.len = url.length;	//数组长度
		var a = this;
		for(var i = 0;i < url.length;i++){
			loadjs(url[i],function(){ a.suc++; if(a.suc == a.len) try{callback();}catch(e){} });
		}
	}
	else if(typeof(url) == 'string') loadjs(url,callback);
}

loadjs("test5.php?return=value",function(){ alert(value); tester(value); });
</script>

 

 

 

test5.php

 

var value="this is value.";

 

 

加载多JavaScript文件的实例:

 

var url = [
		'ajax.php?ajax=1',
		'functions.js'
	];
loadmultijs(url,function(){ alert("加载完毕。"); /* 这里可以调用动态加载的JS文件的数据或方法 */ });
 

 

2. 动态加载css文件

 

 

test.php

 

<style type="text/css" media="screen">@import "body.css";</style>
<style type="text/css" media="screen">@import "div.php?w=300&h=400";</style>
<link rel="stylesheet" type="text/css" href="fonts.php?s=24&c=red">

<body>
	<div>
		this document has a #e4e4e4 background, a 300px/400px div, and a arial/24px/red words.	
	</div>
</body>

 

 

div.php

 

<?php
// declare the output of the file as CSS
header('Content-type: text/css');

// include the script 
//include('others.php');

$width  = $_GET['w'];
$height = $_GET['h'];
?>

div{width:<?=$width?>px;height:<?=$height?>px;border:blue 1px solid;}

 

 

fonts.php

 

<?php
// declare the output of the file as CSS
header('Content-type: text/css');

// include the script 
//include('others.php');

$size   = $_GET['s'];
$color  = $_GET['c'];
?>

body{font-family:arial;font-size:<?=$size?>px;color:<?=$color?>}

 

 

 转载注明出处: http://justcoding.iteye.com/blog/1017497

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
浏览器会自动将 JavaScript 文件CSS 文件、图像等缓存到本地磁盘上的“磁盘缓存”中,以便下次访问同一页面时可以更快地加载资源。但是,可以通过设置 HTTP 响应头来控制浏览器缓存的行为。 以下是一些常用的 HTTP 响应头: 1. Cache-Control:指定缓存策略,如 no-cache、max-age 等。 2. Expires:指定过期时间,如 Expires: Fri, 30 Oct 2020 14:19:41 GMT。 3. ETag:指定资源的唯一标识符,用于验证资源是否已经过期。 4. Last-Modified:指定资源的最后修改时间,用于验证资源是否已经过期。 具体的设置方法可以参考以下示例: 1. 对于静态资源,如 CSS 文件JavaScript 文件、图像等,可以在服务器端设置缓存策略和过期时间。例如,在 Apache 服务器上,可以使用 .htaccess 文件来设置缓存策略和过期时间: ``` <FilesMatch "\.(css|js|png|jpg|jpeg|gif|ico)$"> Header set Cache-Control "max-age=86400, public" Header set Expires "Thu, 01 Jan 2099 00:00:00 GMT" </FilesMatch> ``` 上述代码会将所有以 .css、.js、.png、.jpg、.jpeg、.gif、.ico 结尾的文件缓存一天,并且在 2099 年过期。 2. 对于动态资源,如 HTML 文件,可以在服务器端设置 HTTP 响应头来控制浏览器缓存。例如,在 PHP 中,可以使用以下代码来设置 HTTP 响应头: ``` header('Cache-Control: max-age=86400, public'); header('Expires: ' . gmdate('D, d M Y H:i:s', time() + 86400) . ' GMT'); ``` 上述代码会将当前页面缓存一天,并且在 24 小时后过期。 需要注意的是,缓存策略和过期时间的设置应该根据具体需求来确定,不应该过于激进,否则可能会导致缓存不起作用或者缓存时间过长而无法及时更新。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值