How to execute a JavaScript function when I have its name as a string

I have the name of a function in JavaScript as a string. How do I convert that into a function pointer so I can call it later? Depending on the circumstances, I may need to pass various arguments into the method too. Some of the functions may take the form of namespace.namespace.function(args[...]) . Don't use eval unless you absolutely, positively have no other choice. As has been m

当我将其名称作为字符串时如何执行JavaScript函数

我有一个JavaScript函数的名称作为字符串。 如何将其转换为函数指针,以便稍后调用它? 根据具体情况,我可能也需要将各种论据传递给该方法。 一些函数可能采用namespace.namespace.function(args[...]) 。 除非你绝对没有其他选择,否则不要使用eval。 正如已经提到的那样,使用这样的东西将是最好的方式来做到这一点: window["functionName"](arguments); 但是,这不适用于命名空间的功能: window["My.Namespace.

Javascript replace " ' " with " '' "

I'm trying to replace the "'" character with the "''" string using the replace method, like this: temp.replace("'", "''"); but it seems that it is not replacing anything. how should I write the command in order to replace the wanted character with the wanted string (I'm trying to replace it in the next xml): <Module ForceTabs="1"> <Events&g

Javascript将“'”替换为“''”

我试图用替换方法替换“'”字符与“''”字符串,如下所示: temp.replace("'", "''"); 但它似乎并没有取代任何东西。 我应该如何编写命令以便用想要的字符串替换想要的字符(我试图在下一个xml中替换它): <Module ForceTabs="1"> <Events> <Event Value="onafter_moduleinit_beforedraw()" Type="onafter_moduleinit_beforedraw"/> </Events> <Section Id="Header"

What are useful JavaScript methods that extends built

What are your most useful, most practical methods that extends built-in JavaScript objects like String, Array, Date, Boolean, Math, etc.? String format trim padding replaceAll & replaceAll Array indexOf Date toMidnight Note : Please post one extended method per answer. 字符串全部替换: String.prototype.replaceAll = function(search, replace) { //if replace is not sent

什么是扩展构建的有用的JavaScript方法

什么是最有用,最实用的方法,它扩展了String,Array,Date,Boolean,Math等内置JavaScript对象? 串 格式 修剪 填充 replaceAll&replaceAll 排列 指数 日期 toMidnight 注意:请为每个答案发布一个扩展方法。 字符串全部替换: String.prototype.replaceAll = function(search, replace) { //if replace is not sent, return original string otherwise it will //replace search string with '

What is the preferred syntax for defining enums in JavaScript?

What is the preferred syntax for defining enums in JavaScript? Something like: my.namespace.ColorEnum = { RED : 0, GREEN : 1, BLUE : 2 } // later on if(currentColor == my.namespace.ColorEnum.RED) { // whatever } Or is there a more preferable idiom? Since 1.8.5 it's possible to seal and freeze the object, so define the above as: var DaysEnum = Object.freeze({"monday":1,

在JavaScript中定义枚举的首选语法是什么?

在JavaScript中定义枚举的首选语法是什么? 就像是: my.namespace.ColorEnum = { RED : 0, GREEN : 1, BLUE : 2 } // later on if(currentColor == my.namespace.ColorEnum.RED) { // whatever } 还是有更好的成语? 由于1.8.5可以密封和冻结物体,因此将上述定义为: var DaysEnum = Object.freeze({"monday":1, "tuesday":2, "wednesday":3, ...}) 要么 var DaysEnum = {"monday":1, "tuesday":2, "w

Collisions when generating UUIDs in JavaScript?

This relates to this question. I am using this answer to generate UUID in JavaScript: 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8); return v.toString(16); }); This solution appeared to be working fine, however i am getting collisions. Here's what i have: A web-app running in Google Chrome.

在JavaScript中生成UUID时发生冲突?

这涉及到这个问题。 我正在使用这个答案在JavaScript中生成UUID: 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8); return v.toString(16); }); 这个解决方案似乎工作正常,但是我碰到了碰撞。 这是我的: 在Google Chrome中运行的网络应用程序。 16位用户。 这些用户在过去2个月内已生成约4000个UUID。 我得到了

Graph Goat Teleportation Data with Google Vis. API via Chrome Extension

I just checked my Chromium task manager and noticed that my Goats Teleported count is abnormally high. I thought it might be a good idea to create a browser extension that runs the goat teleportation data through Google Visualizations API and generates some nice reports so I can track it easily and make sure it doesn't get out of hand. alt text http://img820.imageshack.us/img820/4885/scree

使用Google Vis查看山羊传送数据。 API通过Chrome扩展

我刚刚检查了我的Chromium任务管理器,发现我的Goats Teleported计数异常高。 我认为创建一个浏览器扩展可能是一个好主意,它可以通过Google Visualizations API运行山羊传送数据并生成一些漂亮的报告,以便我可以轻松跟踪并确保它不会失控。 替代文字http://img820.imageshack.us/img820/4885/screenshottaskmanagerch.png 是否可以通过Chrome扩展API以编程方式访问Goats Teleported计数? 你认为我应该如何格式化报告?

Javascript SQL database

I'm wanting to deliver some data from the server-side to some client-side Javascript which can use said data to construct a database/table, and then query it using some user-input SQL. Persistence of data isn't what I'm looking for, so HTML5 stuff like localStorage isn't relevant, I'm only wanting to be able to create something like a mini-database in Javascript to query.

Javascript SQL数据库

我想从服务器端传递一些数据给一些客户端的Javascript,它可以使用这些数据来构造数据库/表,然后使用一些用户输入的SQL来查询它。 数据的持久性不是我正在寻找的,所以像localStorage这样的HTML5东西是不相关的,我只希望能够在Javascript中创建类似于迷你数据库的内容来进行查询。 有没有任何Javascript库有这种能力? 谢谢。 看看TrimQuery。 我会看看TaffyDB。 这是一个使用JavaScript实现的数据库,您可以使用类似

Replace text with html formatting using text location (text span) in Javascript

I have a string and I want to replace text with html font colors and I need to replace using a dictionary containing the start of the span (key) and the length of the span (value). Any idea on how to do this in JS so that all my text is replaced correctly with html? str = "Make 'this' become blue and also 'that'." // color_dict contains the START of the span and the LENGTH of the word. // i.e.

用Javascript中的文本位置(文本范围)替换html格式的文本

我有一个字符串,我想用HTML字体颜色替换文本,我需要使用包含跨度(键)开始和跨度(值)长度的字典进行替换。 任何想法如何在JS中做到这一点,以便所有的文本被正确替换为HTML? str = "Make 'this' become blue and also 'that'." // color_dict contains the START of the span and the LENGTH of the word. // i.e. this & that are both size 4. color_dict = {6: "4", 34: "4"}; console.log(str.slice(6, 10));

How can I handle a list with several hundred items in HTML? (Using Javascript)

I am creating a web page which is designed for mobile safari, and I create a list dynamically. The list is created from a text file in which I receive information and I append this information to the list. It works fine when there are about 200 items, but when the file is very large, (I have tried with up to 4000 items) the page becomes very slow, scrolling and selecting this items is very hard

我如何处理一个包含数百个HTML项目的列表? (使用Javascript)

我正在创建一个专门用于移动Safari的网页,并且我动态创建了一个列表。 该列表是从我接收信息的文本文件创建的,我将这些信息附加到列表中。 当有大约200个项目时它工作正常,但是当文件非常大时(我尝试了多达4000个项目)页面变得非常慢,滚动和选择这些项目非常困难。 我知道我不应该创建这么多的HTML元素,但我正在寻找一种方法来创建一个较短的列表,并根据您所做的滚动次数来替换列表元素上的信息。 有任何想法吗?

What is the best way to detect a mobile device in jQuery?

Is there a solid way to detect whether or not a user is using a mobile device in jQuery? Something similar to the CSS @media attribute? I would like to run a different script if the browser is on a handheld device. The jQuery $.browser function is not what I am looking for. Instead of using jQuery you can use simple JavaScript to detect it: if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IE

在jQuery中检测移动设备的最佳方式是什么?

有没有一种可靠的方法来检测用户是否在jQuery中使用移动设备? 类似于CSS @media属性的东西? 如果浏览器位于手持设备上,我想运行其他脚本。 jQuery $.browser函数不是我正在寻找的。 您可以使用简单的JavaScript来检测它,而不是使用jQuery: if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) { // some code.. } 或者你可以将它们结合起来,使它更容易通过jQuery