Dear community ,
I would like to get a variable from an URL.
As far as I know executeScript supports Java Sript.
As a beginner, I would like to know how to use it in combination with getQueryVariable().
My Url looks like this:
nameofthewebsite.com/PHP?ref_id=12345
I need the number 12345.
Thank you in advance!
1 Like
To slice of parts of a string, use the “substring” JS function. The internal var ${!URL}
contains the current URL.
executeScript_Sandbox | var s = ${!URL}; s = s.substring(s.lastIndexOf("=") + 1, s.length); return s; | number
Test macro:
{
"Name": "slice",
"CreationDate": "2020-5-9",
"Commands": [
{
"Command": "store",
"Target": "nameofthewebsite.com/PHP?ref_id=12345",
"Value": "myurl"
},
{
"Command": "executeScript_Sandbox",
"Target": "var s = ${myurl}; s = s.substring(s.lastIndexOf(\"=\") + 1, s.length); return s; ",
"Value": "n"
},
{
"Command": "echo",
"Target": "${n}",
"Value": "lime"
}
]
}
1 Like
Elster
3
Thank you very much for your help.
Could you explain the function within s.substring?
I´d like to know what +1 means. At the moment I only get the whole url and I am not able to slice it with your S.substring solution.
What is your (test) URL?
“+1” adds one to the found index, otherwise “=” would be extracted as well.
1 Like
For my solution s.substring works in combination with s.lastindexof:
s.substrings(s.lastIndexOf(“URLparam”+7,s.lastIndexOf(“URLparam”)+13) ,return s
Great, thank you !
This is a simple and alternative solution
{
"Name": "slice_alternative",
"CreationDate": "2020-6-9",
"Commands": [
{
"Command": "store",
"Target": "nameofthewebsite.com/PHP?ref_id=12345",
"Value": "MyUrl"
},
{
"Command": "executeScript_Sandbox",
"Target": "return ${MyUrl}.split(\"=\")[1];",
"Value": "MyPart"
},
{
"Command": "echo",
"Target": "${MyPart}",
"Value": "lime"
}
]
}
Result
[status]
Playing macro slice_alternative
[info]
Executing: | store | nameofthewebsite.com/PHP?ref_id=12345 | MyUrl |
[info]
Executing: | executeScript_Sandbox | return ${MyUrl}.split("=")[1]; | MyPart |
[info]
Executing: | echo | ${MyPart} | lime |
[echo]
12345
[info]
Macro completed (Runtime 1.06s)
1 Like