JavaScript escape sequence cannot read a variable value while concatenation? - javascript

I am working in a Laravel application. I want to concatenate a variable with a string using escape sequence (` `) in JS. A normal concatenation of strings and variable work fine using escape sequence. But it does not work and throws error when I concatenate PHP route string with a parameter. You will have an idea when you see the below code.
<script>
function changeStatus(value, applicationId){
var url = `"{{route('application.toggle.approval',['id'=> ${applicationId} ])}}"`;
console.log(url);
}
</script>
I am not being able to pass any kind of variable there. It is giving me error like below:
(2/2) ErrorException
Use of undefined constant applicationId - assumed 'applicationId' (View: C:\wamp64\www\oibn\app\Modules\Application\Views\applicationShowProtected.blade.php)
How do I pass the variable there? I can console.log the parameters passed in the function. But that value is not getting passed in concatenation. It gives error only. Please help.

Related

Zapier Javascript Find/Replace Special Characters

I am passing order data from Shopify to VimeoOTT using Zapier. We have been doing this for a year and it works great, except when the customer's name has special characters in it. For example "Jack & Jill (tumble)" The & or ( parenthesis ) causes an error that stops the process.
I am trying to use Zapier's Javascript Action to find and replace the name data's special characters, but I keep getting a coding error. This is my first time working with Javascript :(.
This image shows the Zapier Code Action
Here is an image showing the error I get
// this is wrapped in an `async` function
// you can use await throughout the function
nameFix = inputData.nameFix.replace(/[^a-zA-Z0-9 ]/g,'_').replace(/_{2,}/g,'_');
output = [nameFix];
In javascript, variables must be declared before they are used using let or const:
const nameFix = inputData.nameFix.replace(...
So, that'll fix the error you're seeing.
Separately, JS steps in Zapier must return an object (which has keys and values). So, you'll want to return the result like so:
return {result: nameFix}

How to pass a javascript variable in an ajax url?

I have a variable as per below:
var myip;
And I want to insert it in the url below:
$.ajax('http://api.ipstack.com/**[myip]**?access_key=mykey')
If I manually put in my ip in place of [myip] it gives me the desired results but I want to automate it.
I tried with the methods given in the url below but it didn't help.
How to pass Javascript variables inside a URL? AJAX
Thanks in advance for going through and helping!
Use string template.
$.ajax(`http://api.ipstack.com/${myip}?access_key=mykey`)
^ ^^^^^^^ ^
Or using string concatenation.
$.ajax('http://api.ipstack.com/' + myip + '?access_key=mykey')

Convert Razor String Variable to Javascript String Variable in MVC

I am trying to pass my string variable (even boolean) on my cshtml view to my javascript section. I can successfully pass the value from razor section to javascript if its integer. But what I want to accomplish is to pass the string value. Here is my sample code from my .cshtml:
string strAnnouncement = "My announcement";
int intCounterValue = 1200;
To receive the value on Javascript, here is my code:
//Cannot get the value, always error on Developer Tool console
var SessAnnouncement = #strAnnouncement;
//Can get the value successfully
var SessInitTimer = #intCounterValue;
As you can see, I can get the value via javascript on SessInitTimer which is 1200. But on SessAnnouncement, I get a Uncaught ReferenceError: My announcement is not defined.
How can I get the strAnnouncement value on my razor section and pass it on script section?
They are treated as variable and since you have not defined them you get the said error.
You need to wrap in quotes to be treated as string.
var SessAnnouncement = "#strAnnouncement";
A better approach would be to use JSON.Encode() method
var SessAnnouncement = #Html.Raw(Json.Encode(strAnnouncement));

SyntaxError: identifier starts immediately after numeric literal in Firebug

I'm getting that error when I call this javascript function:
function kickUser(id_userChat){
$.post("chatFuncs.php", { action: "kick", id_user: id_userChat });
}
this "kickUser" function is generated for every user connected to my chat box, like this
$listUsers .= '<img src="imgUsers/'.$DBClass->nomImg($rowUsers['id_user'],$posImg).'" height="'.$heightImg.'" width="'.$widhImg.'"/>
<span class="styleMsg">'.$rowUser['nameUser'].'</span>
Kick</br>';
and the action "kick" is just an update to my database where I remove the user from my chatUsers table
If I change $rowUsers['id_user'] for $rowUsers['userName'] the error changes to:
ReferenceError: 'userName' is not defined (i changed the real name of the user for 'userName' just for this example).
Identifiers in JavaScript can't begin with a number; they must begin with a letter, $ or _.
I'm guessing it's coming from this:
onclick="kick_user('.$rowUsers['id_user'].')">Kick</a>
If you mean to pass a string, then you need to quote the value being passed.
onclick="kick_user(\"'.$rowUsers['id_user'].'\")">Kick</a>
I don't know PHP, so maybe you need different escaping, but this should give you the idea.
The resulting JavaScript code will be
kickUser(userName)
…and obviously there is no js variable userName. You want to pass a string instead:
kickUser('userName');
So add the quotes/apostrophes to the output, and don't forget to escape the $rowUsers['userName'] properly. It's quite the same for $rowUsers['id_user'], which seems to have output even an invalid identifier.

In JMeter and BeanShell, how can I make a variable lowercase?

In JMeter's User Parameters, how can I make a variable lowercase?
Left column
my_lowercase_variable
Right column
${__BeanShell('${my_variable}'.toLowerCase())} //fails
or
${__javaScript('${my_variable}'.toLowerCase())} //fails
Such that ${my_lowercase_variable} is lowercase of ${my_variable}. Tried with quote and without and escaping and such. No luck. Any tricks or tips welcome.
Note to self.
It turns out to be a two liner in BeanShell Sampler rather than a __BeanShell command. Not exactly in the examples unfortunately.
I added the BeanShell Sampler under the Thread Group, then made a variable. No parameters in the form were required only the two liner script below. As long as I don't change the variable I can copy the data to another variable, change that instead, and then make a Value reference to that wherever needed.
First define a variable in some User Parameters or such
ie:
Name: my_initial_reference
Value: ITS IN CAPS
Add a Bean Sampler under the User Preferences or definition list (just next, it's not a child process)
Put in:
String blah = "${my_initial_reference}"; //
vars.put("blah", blah.toLowerCase()); //${blah} = "its in caps" now available
Now under that with Name/Value pairs I can map ${blah} as the value to whatever process name requires it.
Note that the Debug response will still show the initial value in caps but you'll also see blah=its in caps which is what I wanted to use.
Simply can add a function
${__lowercase(${VAL},VALUE)}
${__uppercase(${VAL},VALUE)}
Note: VAL can be correlated or paramiterized value (e.r VAL= TO LOWER or VAL= TO UPPER). We can use this function in beanshell (pre-processor/post-processor/sampler). Jmeter version using (2.6).
Can use it where ever we want in the script as ${VALUE}.
${__javaScript('${foobar}'.toLowerCase())} does work. If the output is ${foobar} instead of desired value, it means that the variable has not been declared
Note that variables are defined only after the "User Defined Variable" component has been parsed. Variables cannot be reused within a single "User Defined Variable" component e.g.:
The second row in that image will not be able to refer to the variable my_variable in the first row. To be able to refer to the first variable, two "User Defined Variable" components is needed. The first variable will be in the first component and the second variable in the second one, e.g.:
With that, ${my_lower_case_variable} will successfully be converted into some value.
${__BeanShell("${my_variable}".toLowerCase())} works too. (Note that Bean Shell requires double quotes. The code in your question uses single quotes.)
Another way is to use vars.get:
${__javaScript(vars.get('my_variable').toLowerCase())}
${__BeanShell(vars.get("my_variable").toLowerCase())}
Hmmmm, your bean shell code didn't work for me. The bean shell sampler returned:
Response code: 500
Response message: org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval Sourced file: inline evaluation of: ``String blah = AAP; vars.put("blah", blah.toLowerCase()); //${blah} now availab . . . '' : Typed variable declaration : Void initializer
I added two double quotes to solve it:
String blah = "${my_initial_reference}";
vars.put("blah", blah.toLowerCase()); //${blah} now available
The beanshell and JavaScript functions in this use will fail, because they don't import the packages you need in order to use .toLowerCase.
If you really need to use a function to convert case (rather then declaring them as lowercase in the first place), you may need to write a full beanshell post-processor script in order to import the needed packages.
http://jmeter.apache.org/usermanual/functions.html#__BeanShell
http://jmeter.apache.org/usermanual/functions.html#__javaScript
http://www.javadocexamples.com/java_examples/org/apache/jmeter/
var blah = "${my_initial_reference}";
blah.toLowerCase();

Categories

Resources