简体   繁体   中英

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); 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM