howto eval() my jquery code
I'm loading my pages into divs with Ajax. Everything is fine, except I dont know how to eval the output so I can write javascript in the loaded pages. If anyone knows how, please tell me. tThanks!
This is my code:
var bustcachevar = 1 //bust potential caching of external pages after initial request? (1=yes, 0=no)
var loadedobjects = ""
var rootdomain = "http://" + window.location.hostname
var bustcacheparameter = ""
function ajaxpage(url, containerid) {
var page_request = false
if (window.XMLHttpRequest) // if Mozilla, Safari etc
page_request = new XMLHttpRequest()
else if (window.ActiveXObject) { // if IE
try {
page_request = new ActiveXObject("Msxml2.XMLHTTP")
}
catch (e) {
try {
page_request = new ActiveXObject("Microsoft.XMLHTTP")
}
catch (e) {}
}
}
else return false page_request.onreadystatechange = function() {
loadpage(page_request, containerid)
}
if (bustcachevar) //if bust caching of external page
bustcacheparameter = (url.indexOf("?") != -1) ? "&" + new Date().getTime() : "?" + new Date().getTime()
page_request.open('GET', url + bustcacheparameter, true)
page_request.send(null)
}
function loadpage(page_request,开发者_StackOverflow containerid) {
if (page_request.readyState == 4 && (page_request.status == 200 || window.location.href.indexOf("http") == -1)) document.getElementById(containerid).innerHTML = page_request.responseText eval(responseText);
}
function loadobjs() {
if (!document.getElementById) return for (i = 0; i < arguments.length; i++) {
var file = arguments[i]
var fileref = ""
if (loadedobjects.indexOf(file) == -1) { //Check to see if this object has not already been added to page before proceeding
if (file.indexOf(".js") != -1) { //If object is a js file
fileref = document.createElement('script')
fileref.setAttribute("type", "text/javascript");
fileref.setAttribute("src", file);
}
else if (file.indexOf(".css") != -1) { //If object is a css file
fileref = document.createElement("link")
fileref.setAttribute("rel", "stylesheet");
fileref.setAttribute("type", "text/css");
fileref.setAttribute("href", file);
}
}
if (fileref != "") {
document.getElementsByTagName("head").item(0).appendChild(fileref)
loadedobjects += file + " " //Remember this object as being already added to page
}
}
}
RESPONSE TEXT:
<div id="mySingleMenu"><?php include("single-menu.php"); ?></div>
<div id="mySingleContent">
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php the_title(); ?>
<?php the_excerpt(); ?>
<?php endwhile; ?>
<?php endif; ?>
</div>
<script>
$('#mySingleMenu').hide();
</script>
FIXED : http://www.javascriptkit.com/script/script2/ajaxpagefetcher.shtml
try this first..
insert the div into the page with an id that you control in js.. say id="1234" and then do the following.. note your script tag should be within this div
var d =
document.getElementById("1234").getElementsByTagName("script")
var t = d.length
for (var x=0;x<t;x++){
var newScript = document.createElement('script');
newScript.type = "text/javascript";
newScript.text = d[x].text;
document.getElementById('divContents').appendChild (newScript);
else the apprach should be somewaht like below:
// Suppose your response is a string:
// { html: "<p>add me to the page</p>, script:"alert('execute me');" }
var obj = eval( "(" + response + ")" ) ;
eval( obj.script ) ;
so you get the idea right you basically need to strip out the script part from the code and then eval it..
either that or you could use a library like jquery in which case all you need to do is use the html() api and it will take care of executing the script for you..
the other way is to insert the script onto the page you can do that in the ffollowing way:
var headID = document.getElementsByTagName("head")[0];
var newScript = document.createElement('script');
newScript.type = 'text/javascript';
newScript.src = 'http://www.somedomain.com/somescript.js'; //newScript.innerHTML= ""; //your script code
headID.appendChild(newScript);
there is a hack for this too. read this: http://www.thedanglybits.com/2007/06/22/execute-javascript-injected-using-innerhtml-attribute-even-with-safari/
Hope this helps..
If you're getting properly formatted JSON back, then you'll want to use Douglas Crockford's JSON library: http://www.json.org/js.html
However... your entire code set is somewhat antiquated. There's a reason that the libraries have taken over, speeding development & reducing bugs.
In fact, here is all of your code, rewritten as jquery:
var bustcachevar = 1 //bust potential caching of external pages after initial request? (1=yes, 0=no) var loadedobjects = [];
function loadpage(page_request, containerid) {
page_request = bustcachevar ? page_request + Math.rand() : page_request;
$('#'+containerid).load(page_request);
// This assumes what is coming back is HTML.
//If you're getting back JSON, you want this:
// $.ajax({url:page_request, success:function(responseText){}}); }
// Note that responseText is actually a pre-eval'd object.
function loadobjs() {
if (!document.getElementById) return
for (var i = 0; i < arguments.length; i++) {
var file = arguments[i];
var fileref = "";
if ($.inArray(file, loadedobjects) < 0)
{
if (file.indexOf(".js") != -1)
{ //If object is a js file
fileref = $('<script>').attr('type', 'text/javascript').attr('src', file);
}
else
{
fileref = $('<link>').attr('rel', 'stylesheet').attr('type', 'text/css').attr('href', file);
}
$('head').append(fileref);
loadedobjects.push(file);
}
}
}
Although you may not be familiar with the particulars of the syntax, you should see quickly that it is JS and its brevity should make it fairly easy to read. I was a POJ (plain-old-javascript) guy for years, but I just can't see any argument for it these days unless you're writing a serious library yourself (which I've also done).
精彩评论