Had a nice PHP/HTML/JS prototype working on my personal Linode, then tried to throw it into a work machine.
The page adds a script tag dynamically with some JavaScript. It's a bunch of Google charts that update based on different timeslices. That code looks something like this:
// jQuery $.post to send the beginning and end timestamps
$.post("channel_functions.php", data_to_post, function(data){
// the data that's returned is the javascript I want to load
var script = document.createElement('scri开发者_JAVA百科pt');
var head= document.getElementsByTagName('head')[0];
var script= document.createElement('script');
var text = document.createTextNode(data);
script.type= 'text/javascript';
script.id = 'chart_data';
script.appendChild(text);
// Adding script tag to page
head.appendChild(script);
// Call the function I know were present in the script tag
loadTheCharts();
});
function loadTheCharts() {
// These are the functions that were loaded dynamically
// By this point the script tag is supposed be loaded, added and eval'd
function1();
function2();
}
Function1() and function2() don't exist until they get added to the dom, but I don't call loadTheCharts() until after the $.post has run so this doesn't seem to be a problem.
I'm one of those dirty PHP coders you mother warned you about, so I'm not well versed in JavaScript beyond what I've read in the typical go-to O'Reilly books. But this code worked fine on my personal dev server, so I'm wondering why it wouldn't work on this new machine.
The only difference in setup, from what I can tell, is that the new machine is running on port 8080, so it's 192.168.blah.blah:8080/index.php instead of nicedomain.com/index.php.
I see the code was indeed added to the dom when I use webmaster tools to "view generated source" but in Firebug I get an error like "function2() is undefined" even though my understanding was that all script tags are eval'ed when added to .
My question: Given what I've laid out, and that the machine is running on :8080, is there a reason anyone can think of as to why a dynamically loaded function like function2() would be defined on the Linode and not on the machine running Apache on 8080?
jQuery supports javascript responses:
$.post("channel_functions.php", data_to_post,
function (data, textStatus, xhr) {loadTheCharts()},
'script');
However, a dataType
of "script" will turn a cross-domain POST into a GET, as per the documentation.
The main problem with eval
is the eval-ed code inherits the scope the eval
is in. Instead, you can use jQuery.globalEval
. Try something like:
$.post("channel_functions.php", data_to_post,
function (data, textStatus, xhr) {
/* data might have errors, which will cause an exception.
We'll let the default exception handler catch & log it.
*/
$.globalEval(data);
loadTheCharts();
});
Why don't you just eval the responseText? I don't see the need to create a new script node.
精彩评论