简体   繁体   English

动态加载JavaScript函数

[英]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 JS代码具有简单的警报检出功能,例如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. 如果您对对象进行JSON.stringify ,则您的代码将按预期工作。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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