execute dynamically created code in javascript [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
In javascript I create a certain command:
arrSetContentCommands.push( "$('#titel"+i+"').trumbowyg('html', \""+data[i].value+"\");";`)
This enables me to use a html editor in browser. The id's it needs to use are also created, cause it depends on what's in the database.
how do i execute this? all the commands are placed in an array.

The solution is: Don't create code strings and put them in an array. Just execute the code:
$('#titel'+i).trumbowyg('html', data[i].value);
If those elements don't exist yet, just run this code later when they do, or if for some reason you won't have access to data later, build an array of functions:
arrSetContentCommands.push(function(index, value) {
$('#titel'+index).trumbowyg('html', value);
}(i, data[i].value));
...and later:
arrSetContentCommands.forEach(function(command) {
command();
});
Notice in that we don't use i or data[i] in the function we're pushing; see this question's answers for why not.
If you had a good reason for executing code in a string, you'd do it via new Function(...)(), like this:
arrSetContentCommands.forEach(function(command) {
new Function(command)();
});
But there are very, very, very few cases where that's appropriate, and obviously never do it with code you don't trust.

Related

How to run specific code javascript DOM in page at site [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 12 months ago.
Improve this question
I want help please :(
I have several dom codes, in a js file, running on a site
site.com/script.js
I want to customize a specific code, to work only on a sub-page in the site.
for example:
the codes work on site.com
and I want a specific code that works
only on the site.com/page16
and code javascript just run by one file
I dont have any access to html page
You can access the end part of your url by using window.location.pathname.
For the website site.com/page16 - window.location.pathname returns the string "/page16".
Next you could have a function that checks to see what the current pathname is and terminates early if the pathname doesn't match /page16.
For example:
const subPageFn = () => {
// terminates early if subpage doesn't match page16
if (window.location.pathname !== '/page16') {
return
}
// runs page16 specific code here...
}
Then you'd call the function in any page by using
subPageFn()

Is there a way to get an Object from an Array Javascript [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Coding in javascript, and I am trying to put objects into arrays, and the Array looks like
arr = [
{"id":1,"location":"Place1"},
{"id":2,"location":"Place2"}
]
I am trying to do arr[1].location, but arr[1] is undefined and console.log(arr[0]) and arr[1] in inspect element just returns as a "{". Im not sure if this is a chrome thing, but node does seem to accept the format.
Edit: I am collecting from a api that I made and this is the format it returns the code, I am just trying to access variable location. I am asking how would I get said variable
Problem answered in comments, didn't parse data.
Did you try parsing your data when received :
JSON.parse(apiResponse)
The problem was the way I handled the data. Instead of using a XMLHttpRequest for the array, I used jquery to request the data, and that seemed to work better for my API.

How to get all variable names but only from a specific file in JavaScript? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I need to get ALL variables from the file xyz.js in order to create an array with those names in main.js
You can't do that with non-module files, it's just not how JavaScript works.
If you were using JavaScript modules, you could get an object containing all of its exports via:
import * as x from "./the-module.js";
...but that's specific to modules and their exports.
As T.J. Crowder already noted: you can't just request all the variables from a specific file.
What you could do, is load the file contents and parse it.
It depends a bit on how the variables are declared in xyz.js.
If you know the variables are all defined with const or let it should be fairly easy to do. When multiple variables are defined with one const of let it gets a bit trickier.
The more you know about xyz.js, the easier it is to parse the content and extract the variable names.

Change coding in java script function [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have never seen it, but is there a way to modify the existing java script function through the code? Something similar to "ALTER PROCEDURE..." statement in SQL Server?
Thank you!
You can replace any function in Javascript simply by assigning a new function to it, they're merely object properties or simple variables:
Math.max = function () {
return Math.random(); // har har har
};
You cannot trivially "reach into" a function and change something about its internals though, no. Or at least, that would be pretty insane.

Coffeescript + Express.js : efficient way to pass req.session to model classes [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I don't know if the fact that I'm using CS is relevant.
Anyway: I've defined some classes that I use to handle database objects, but I cannot find a clean way to pass the current user id (found in req.session.user.id) to them.
Is there a better way to achieve this than passing it every time as a parameter in the constructor?
In my case, I've build a very simple middleware to save some of the session data, some of my config data, etc. in the res.locals object so as to be able to obtain them in various modules. It depends what do you have in your classes: if you have access to the response object or not.
The middleware solution may be a good idea for you too

Categories

Resources