I am using one Javascript function to generate a random Number.
Used a User Parameter(Preprocessor) under the request
Added a Variable: farmeid
Function: ${__javaScript('bam-'+parseInt((Math.random()*1000000),10))}
When I am using the varialble ${frameid}, it is returning no Value.
The problem is that there is a comma in your expression here: ),10 which acts as a delimiter for function parameters. If you can remove this ,10 bit so your expression could look like:
${__javaScript('bam-'+parseInt(Math.random()*1000000),)}
This would be successfully evaluated by __javaScript function.
If this 10 bit is a must you can use BSF PreProcessor with the following code:
vars.put("frameid",'bam-'+parseInt((Math.random()*1000000),10));
And the easiest way is using __Random function like
bam-${__Random(111111,999999,)}
directly where it's required.
Hope this helps.
It's better to use Jmeter's Random Variable generator.
http://jmeter.apache.org/usermanual/component_reference.html#Random_Variable
Cheers,
Related
So I have came across a curious question that I can't find its answer anywhere and there isn't much documentation on what eval does when you pass to it string literals.
If I do eval("alert(1)") I will get an alert box with 1, however, when I do eval`alert(1)` I just get an array with "alert(1)" I am not sure where that is coming from, isn't it supposed to be treated the same as the previous example?
Also, eval`${1}` returns an array with two empty elements, why?
What you're running into is something to do with tagged templates.
Essentially, you are doing string interpolation and using a function to decide how to create the string. The first argument is expected to be an Array that contains all of the string parts (everything between ${var} declarations). The return of any function used this way is expected to be the string. The reason you are seeing 2 entries is because this function returns a raw format in addition to the one it tried to create using the tag function.
This is a very basic JS question, but this "issue" drives me crazy.
From simple algebra I would expect that both first and the second statement are vaild. But the second is always throwing "Invalid assignment" error.
Does anyone have a good explanation for it?
fieldname1 = document.getElementById("emailID1");
document.getElementById("emailID2") = fieldname2;
Thanks so much,
Most common programming languages, including JavaScript, require that the left-hand side of an assignment (the "target") be something called an l-value. That means it's an expression that denotes a place to put a value. A simple variable name, or a reference to an object followed by .propertyName suffix, works as an l-value.
In your case, the function call return value is not an l-value, because JavaScript does not make that possible (some languages do). A function call is always an r-value, meaning something that appears on the right-hand side of an assignment.
Now, in your particular case, because getElementById() returns a reference to a DOM element, you can do something like this:
document.getElementById("something").name = "frederick";
The function still returns an r-value, but that .name works as a property reference and thus as an l-value.
The assignment operator resolves the right side of the equal sign and stores it in the variable on the left side, which is what is happening in the first line.
The second line is basically trying to take the value of a variable fieldname2 and store it in a function call document.getElementById("emailID2")
JavaScript doesn't know how to resolve that at runtime, so it's throwing an invalid assignment operation.
There's more information on assignment from MDN here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Assignment_Operators
You can't assign a value to an object itself in this case.
document.getElementById("emailID2") = fieldname2;
As i guess you want to do something like this:
document.getElementById("emailID2").name = fieldname2;
I have been wondering if is possible to use a HTML <input id="in"> (which I could enter lets say 25 / 6 * 3) and output the mathematical evaluation with Javascript, I have written up something which very understandably did not work, would there be any way to make it work?
function go() {
var x = $("#in").val();
alert(parseInt(x))
}
Your answers are much appreciated
Yes you can using the eval() function. Assuming you're input is going to take a value then you can simply call eval(myString) which should return the result.
For example if you execute the following:
alert(eval('3 + 4')); // alerts 7
Be wary though in that anything you do really shouldn't be saved as this could lead to javascript attacks if someone is able to submit some javascript that will run on someone elses browser.
Yes, it is possible to do what you're asking; you just have to use eval(). However, using eval() is not very safe. since this means that any JavasScript code inside #in will be evaluated, which is probably not what you want.
A better way to do this would be to write a parser to parse the mathematical expression and build a syntax tree and then evaluate that.
You can use the expression parser of math.js
alert(math.eval('3 + 4'));
I have the following bit of code
console.log("I am");
var x = "console.log('Alive!')";
Now I only want to use x to execute the code-string that is assigned to it - I may not even know the value of x for example but simply want to execute it whatever it maybe - is this possible?
eval() This will convert string to javascript code.
eval("console.log('Alive! Woo!')");
eval and new Function let you parse and execute JavaScript code from strings.
In general, avoid executing code from strings. And never execute code from strings where the strings are untrusted input (for instance, if you take input from user A, never use these to evaluate it in a session with user B).
I see answers here pointing you at eval. eval grants access to your local variables and such to the code, making it really powerful and really dangerous if you use it with untrusted input.
Where possible, avoid eval. You can easily avoid it in your case:
For instance:
console.log("I am");
var x = "console.log('Alive!')";
new Function(x)();
That code creates a function whose body is the text from x, then immediately executes the function.
What you are looking for is eval(). By passing a string to this function you will evaluate the string as JavaScript code and it will return whatever return-value the code in the string returns.
Be aware when using this function though. You do not want to evaluate any code you do not know is safe to execute. For example, running user-generated code could mess up whatever you are making. While using this in JavaScript on a website this will probably only cause issues on the client-side and hence probably won't be much of a security threat, you would want to be VERY careful when evaluating code on for example a server side.
As have been hinted to in other posts here you probably want to make a function instead of an evaluated string if you are in control of the source code that is to be run.
What you are looking for is called a function:
function x() {
console.log('Alive!');
}
If x is already a string containing the code you could use eval(x) to execute it. eval is evil though.
var x = "console.log('Alive!')";
eval(x)
Is there any max length for a JavaScript function() parameter?
I created a function which had a long parameter, but it didn't work. When I shortened the parameter of the same function, it worked. Does that mean that there is a maximum length for a function argument? If so, please recommend alternatives to my current method.
JavaScript
function example(idnum) {
alert(idnum);
}
HTML
<div onclick='example(*php variable,no special character included*)'></div>
When the PHP variable is long, such as "17b6f557969619544eb1e6cd58c3f341", it does not work. But if I change the variable to something like "203", the function works successfully.
"Douglas Crockford" wrote in message
news:9a027$3fa7c56d$44a4aebc$9152#msgid.meganewsse rvers.com...
[color=blue]
... . The maximum length with will be implementation-specific. ...[/color]
In microsoft.public.scripting.jscript, Michael Harris
(Microsoft.MVP.Scripting), who might be expected to know, quoted:-
In JScript,
variant string variables have the same limit as in VBScript, up to
2^31 characters.
String literals (as in "this is a literal") have (IIRC) a limit
~2^10 (1024) characters.
for the JScript implementation.
Blockquote
Sounds like you actually want to pass the PHP variable's value literally to the function, not as a variable name.
example(17b6f557969619544eb1e6cd58c3f341)
tries to call the function with that variable (likely causing an exception, or even being a syntax error), while
example(203)
calls the function with the number literal for the integer 203. If you want to pass the value as a string, use json_encode!
Also notice that you will need to escape everything for use in a HTML attribute (i.e. escape quotes, < and >), as you have
<div onclick=" [some code here] ">