2008-03-31的存档

动态嵌入 javascript 和 css

2008-03-31 10:24 am

动态嵌入 css 代码

var headID = document.getElementsByTagName(”head”)[0];
var cssNode = document.createElement(’link’);
cssNode.type = ‘text/css’;
cssNode.rel = ’stylesheet’;
cssNode.href = ‘FireFox.css’;
cssNode.media = ’screen’;
headID.appendChild(cssNode);

动态嵌入 javascript 代码

var headID = document.getElementsByTagName(”head”)[0];
var newScript = document.createElement(’script’);
newScript.type = ‘text/javascript’;
newScript.src = ‘http://www.somedomain.com/somescript.js’;
headID.appendChild(newScript);

Ajax 就加入 javascript

function ajaxObject(url, callbackFunction) {

var that=this;

this.updating = false;

this.update = function(passData,postMethod) {

if (that.updating==true) { return false; }

that.updating=true;

var AJAX = null;

if (window.XMLHttpRequest) {

AJAX=new XMLHttpRequest();

} else {

AJAX=new ActiveXObject(”Microsoft.XMLHTTP”);

}

if (AJAX==null) {

return false;

} else {

AJAX.onreadystatechange = function() {

if (AJAX.readyState==4) {

that.updating=false;

that.callback(AJAX.responseText,AJAX.status,AJAX.responseXML);

delete AJAX;

}

}

var timestamp = new Date();

if (postMethod==’POST’) {

var uri=urlCall+’?'+timestamp.getTime();

AJAX.open(”POST”, uri, true);

AJAX.setRequestHeader(”Content-type”, “application/x-www-form-urlencoded”);

AJAX.send(passData);

} else {

var uri=urlCall+’?'+passData+’&timestamp=’+(timestamp*1);

AJAX.open(”GET”, uri, true);

AJAX.send(null);

}

return true;

}

}

var urlCall = url;

this.callback = callbackFunction || function () { };

}

function attachScript(responseText, responseStatus) {

// This function is called by the ajaxObject when the server has finished

// sending us the requested script.

if (responseStatus==200) {

eval(responseText);

}

}

// ajaxObject is an object constructor, pass it the server url you want it to call

// and the function name you want it to call when it gets the data back from the server.

// Use the .update() method to actually start the communication with the server.

// The first optional argument for update is the data you want to send to the server.

// ajaxvar.update(’id=1234&greed=good&finish=true’);

// The second optional argument for update is ‘POST’ if you want to send the data

// as a POST instead of the default GET (post can handle larger amounts of data and

// the data doesn’t show up in your server logs).

// ajaxvar.update(’id=1234&greed=good&finish=true’,'POST’);

var getScriptViaAjax=new ajaxObject(’http://mydomain.com/somescript.js’,attachScript);

getScriptViaAjax.update();

var anotherScript = new ajaxObject(’http://mydomain.com/anotherScript.php’,attachScript);

anotherScript.update(’userId=4323′,’POST’);

flickr tag ajax 搜索

<script type=”text/javascript”>

function jsonFlickrFeed(feed){

z=”;

for (x=0; x<feed.items.length; x++) {

tmp=feed.items[x].media.m;

tmp=tmp.replace(/_m\.jpg/g,’_s.jpg’);

z+=’<img src=”‘+tmp+’” mce_src=”‘+tmp+’” alt=”some img” width=”75px” height=”75px” style=”margin: 2px;”>’;

}

document.getElementById(’pics’).style.display=’block’;

document.getElementById(’pics’).innerHTML=z;

}

function searchFlickr() {

var headID = document.getElementsByTagName(”head”)[0];

var newScript = document.createElement(’script’);

tagID = escape(document.getElementById(’tags’).value);

document.getElementById(’tags’).value=”;

newScript.type = ‘text/javascript’;

newScript.src = ‘http://flickr.com/services/feeds/photos_public.gne?tags=’ + tagID + ‘&format=json’;

headID.appendChild(newScript);

document.getElementById(’pics’).style.display=’block’;

document.getElementById(’pics’).innerHTML=”Loading…”;

return false;

}

</script>

<form action = “#” onsubmit=”return searchFlickr();”>

<input type=’text’ size=’40′ id=’tags’>   <input type=’submit’>

</form>

<div style=’border: 1px solid black; width: 100%; display: none;’ id=’pics’></div>

via http://www.hunlock.com/blogs/Howto_Dynamically_Insert_Javascript_And_CSS

推荐(0)
收藏
得到OpenID
使用OpenID提供商
35OpenID 35OpenID MyOpenID MyOpenID Flickr Flickr
Google Google Yahoo Yahoo! AOL AOL
Blogger Blogger LiveJournal LiveJournal Verisign Verisign
ClaimID ClaimID Technorati Technorati Vidoop Vidoop
OpenID OpenID 帮助
您还没有登录,请登录后继续操作。
提示:您必需打开Cookie才能使用本系统
请输入您的 OpenID OpenID 登录:
例如:http://yourname.openid.35.com
close