How To Use A Variable In A String
I am trying to implement a variable in a string. I have searched for this and tried throwing different things at it but nothing seems to work.. I have a variable that looks like th
Solution 1:
You'll have to concatenate your string with the variable using the javascript +
symbol, like below:
HMI.Builder.init('Files/HMIBuilder/' + getQueryVariable("name"), function () {
//run code after page/iframe is loaded
});
Or use the handy replace
for more readability:
HMI.Builder.init('Files/HMIBuilder/{name}'.replace('{name}', getQueryVariable("name")), function () {
//run code after page/iframe is loaded
});
Of course, you could do it with two steps for even more readability:
var route = 'Files/HMIBuilder/{name}'.replace('{name}', getQueryVariable("name"));
HMI.Builder.init(route, function () {
//run code after page/iframe is loaded
});
Finally, on recent browsers (and with some transpilers for backward compatibility), you could use the ES6 "`" syntax:
var route = `Files/HMIBuilder/${getQueryVariable("name")}`;
HMI.Builder.init(route, function () {
//run code after page/iframe is loaded
});
Solution 2:
use a template literal
HMI.Builder.init(`Files/HMIBuilder/${getQueryVariable(variable)}`, function () {
//run code after page/iframe is loaded
});
Solution 3:
You can get a list of parameters by running the following function
functionextractParams(url) {
newURL(url).search.substr(1).split('&').map(x => {
var y = x.split('=');
return { [y[0]]: y[1] };
});
}
var params = extractParams('https://www.website.com?id=151&name=john');
Which would print the following
[{id: 151}, {name: "john"}]
Post a Comment for "How To Use A Variable In A String"