如何执行一些字符串的JavaScript?
function ExecuteJavascriptString()
{
var s = "alert('hello')";
// how do I get a browser to alert('hello')?
}
Run Code Online (Sandbox Code Playgroud) 我想将DOM节点甚至整个序列化为windowJSON.
例如:
>> serialize(document)
-> {
"URL": "http://stackoverflow.com/posts/2303713",
"body": {
"aLink": "",
"attributes": [
"getNamedItem": "function getNamedItem() { [native code] }",
...
],
...
"ownerDocument": "#" // recursive link here
},
...
}
Run Code Online (Sandbox Code Playgroud)
JSON.stringify(window) // TypeError: Converting circular structure to JSON
Run Code Online (Sandbox Code Playgroud)
问题是JSON默认不支持循环引用.
var obj = {}
obj.me = obj
JSON.stringify(obj) // TypeError: Converting circular structure to JSON
Run Code Online (Sandbox Code Playgroud)
window和DOM节点有很多.window === window.window将如此document.body.ownerDocument === document.
此外,JSON.stringify不序列化函数,所以这不是我想要的.
`dojox.json.ref.toJson()` can easily serialize object with …Run Code Online (Sandbox Code Playgroud) 有没有办法可以将函数作为json字符串传递(使用JSON.stringify进行转换),将其发送到另一个函数,解析json然后执行json中的函数?我正在使用jquery和javascript.
如何将javascript/jquery中的字符串转换为函数?
我正在尝试使用JSON参数列表来初始化函数.但是,其中一个参数是一个函数,我将其存储为字符串,当我尝试使用eval()返回函数时出现错误.
例如,如果我的JSON是:
json = { "one": 700, "two": "function(e){alert(e);}" }
Run Code Online (Sandbox Code Playgroud)
然后在我的代码中:
parameters = eval(json);
$('myDiv').addThisFeature({
parameter_1: json.one,
parameter_2: eval(json.two) // <= generates error
})
Run Code Online (Sandbox Code Playgroud) 我在javascript中有一个对象:
admins: {
articles: {
path: '/admins/articles',
template: '/views/admins/articles.html',
link: function() {
return path; // !!! how to reference the 'path'?
}
}
}
Run Code Online (Sandbox Code Playgroud)
我有很多像这样的对象,每个对象都有一个path字段和一个link函数.我想用领域path中link,但我不能随便使用path.
我该怎么办?
是否有注释或其他方式告诉Jackson序列化String变量的值而不带引号.序列化程序应仅序列化一个字段的值而不带引号.
我正在寻找类似于以下内容的回报:
{"name": "nameValue", "click" : function (e){console.log("message");}}
Run Code Online (Sandbox Code Playgroud)
代替
{"name": "nameValue", "click" : "function (e){console.log("message");}"}
Run Code Online (Sandbox Code Playgroud)
以上是外部java脚本库如何需要数据,因此如果没有办法,我必须在Object Mapper将其转换为JSON后手动更改字符串.
我正在尝试 HTML5 LocalStorage。
在示例中,我只看到简单的 JSON 数据对象,但我添加了一些自定义数据对象类,其中添加了各种方法(如 addChild、cleanup 等)。
是否可以将这些自定义对象的实例直接存储在 LocalStorage 中,或者我是否理解 LocalStorage 的整个概念完全错误?
我正在使用Javascript创建一个基于Web的小型RPG,不幸的是,一旦实现我的保存和加载功能,我的代码的关键部分就完全失败了.
截至目前,我将所有玩家数据保存在我的播放器对象中:
var player = {
stats: {
level: 1,
healthBase: 50,
health: 50,
strength: null,
strengthBase: 7,
strengthMods: {
},
},
};
Run Code Online (Sandbox Code Playgroud)
使用这两个功能:
function save() {
localStorage.setItem("playerData", JSON.stringify(player));
}
function load() {
player = JSON.parse(localStorage.getItem('playerData'));
}
Run Code Online (Sandbox Code Playgroud)
这对一切都很有效,除了一小部分.在某些情况下,我需要在我的统计数据中添加修饰符来改变它们,我这样做
player.stats.strengthMods.mods = () => ( 5 );
Run Code Online (Sandbox Code Playgroud)
但是,如果我保存然后加载我的游戏,请致电
player.stats.strengthMods.mods
Run Code Online (Sandbox Code Playgroud)
控制台只显示一个空对象.我无法理解为什么我的浏览器拒绝加载箭头功能,其他一切都完全加载.
知道为什么吗?
我试图在JSON响应中发送一个函数,并试图在js中提醒该函数.下面是代码.
JS:
$('input').keyup(function(){
$.ajax({
type: "POST",
url: "sample.php",
dataType : 'json',
data: 'val='+$('input').val(),
success: function(response){
var json = transport.responseText.evalJSON();
alert(json.function()); // => **should alert 'foo bar' - but not**
}
});
});
Run Code Online (Sandbox Code Playgroud)
PHP:
<?php
// Our sample array
$foo = array(
'string' => 'bar',
'function'=> 'function(){return "foo bar";}'
);
$value_arr = array();
$replace_keys = array();
foreach($foo as $key => &$value){
// Look for values starting with 'function('
if(strpos($value, 'function(')===0){
// Store function string.
$value_arr[] = $value;
// Replace function string …Run Code Online (Sandbox Code Playgroud) 我想从源代码中隐藏一段Javascript.我想到的方法是使用PHP包含脚本文件,但这似乎没有用.
有人对我有什么建议吗?如果您需要我的脚本副本,请询问.
在此先感谢Callum