开发者

Dynamically loading javascript function

 
Heyy guys this is my new code 

<html> <head> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script> <script> $(document).ready(function(){ $.ajax({ url: 'getInfo.php', success: function(data) { var myJSONObject = {"bindings": [{"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"},{"ircEvent": "PRIVMSG", "method": "deleteURI", "regex": "^delete.*"},{"ircEvent": "PRIVMSG", "method": "randomURI", "regex": "^random.*"}]}; trip_progress='<div><img src="red.png" onmouseover="myclick('+myJSONObject+')" /></div>'; var headID = document.getElementsByTagName("head")[0]; var newScript = document.createElement('script'); newScript.type = 'text/javascript'; newScript.src = 'js/callclick.js'; headID.appendChild(newScript); 开发者_如何学Python document.getElementById("timepass").innerHTML=trip_progress; } }); }); </script> </head> <body>

The JS code has a simple alert check out like this http://www.vaishakhthayyil.com/trip/js/callclick.js

I am not getting why the object is being not passed in function .


You could potentially use closures to call a function that will generate a dynamic function that you can then call, though i've never used this capability. You can find more info about closures here. They are a feature of many functional programming languages.


Please check out my edit. If you JSON.stringify your object, then your code will work as you expect.

var gmarkers = {key: 'value'};

var ss = document.createElement('script');
var scr = "function caller(){ alert("+JSON.stringify(gmarkers)+") ;}"; //alert(scr); var tt1 = document.createTextNode(scr); ss1.appendChild(tt1); var hh1 = document.getElementsByTagName('head')[0]; hh1.appendChild(ss1); caller()
var tt = document.createTextNode(scr);
ss.appendChild(tt);
var hh = document.getElementsByTagName('head')[0];
hh.appendChild(ss);

caller();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜