[英]Javascript ,how to combine two variables into a third
I'm working on a project to look at alternative ways to deploy printers to users from an html page residing on an internal SharePoint site. 我正在研究一个项目,以研究从驻留在内部SharePoint网站上的html页面向用户部署打印机的替代方法。
I've found some code someone had posted that seems to work well, but I was looking to see if I could change it a little to suit our needs a little better, allowing for a cleaner way to create hundreds of new printer installation links. 我发现有人发布了一些似乎运行良好的代码,但是我想看看是否可以对其进行一些更改以更好地满足我们的需求,从而允许使用更简洁的方法来创建数百个新的打印机安装链接。
This works for us. 这对我们有用。
var WshNetwork = new ActiveXObject("WScript.Network");
var PrinterPath = "\\\\printserver-a\\printer-a";
WshNetwork.AddWindowsPrinterConnection(PrinterPath); //Does the adding of the printer
WshNetwork.SetDefaultPrinter(PrinterPath); //Sets printer as system default
Rather than have to modify the PrinterPath variable, what I'd like to define ahead of time is a PrintServer variable as well as a Printer variable, then combine those two variables into a 3rd, listed in the printer path. 无需修改PrinterPath变量,我想提前定义一个PrintServer变量以及一个Printer变量,然后将这两个变量合并到打印机路径中列出的第3个变量中。
I've tried some examples I've seen, and have spoken to a colleague who has a better understanding of JavaScript, but could not find anything that works for me. 我已经尝试了一些示例,并与一位对JavaScript有更好理解的同事进行了交谈,但找不到适合我的任何东西。 I was thinking something like this. 我在想这样的事情。
var PrintServer = "printserver-a";
var Printer = "printer-a";
var WshNetwork = new ActiveXObject("WScript.Network");
var PrinterPath = "\\\\PrintServer\\Printer";
WshNetwork.AddWindowsPrinterConnection(PrinterPath); //Does the adding of the printer
WshNetwork.SetDefaultPrinter(PrinterPath); //Sets printer as system default
But this is not working for me, and am looking to see what it is I'm missing. 但这对我不起作用,并且正在寻找我所缺少的东西。 I have no understanding of JavaScript whatsoever, but have found this to be one of the best solutions for us. 我对JavaScript毫无了解,但是发现这对我们来说是最好的解决方案之一。
A template literal would be effective here: 模板文字将在此处有效:
var PrinterPath = `\\\\${PrintServer}\\${Printer}`;
Alternatively, string concatenation: 或者,字符串串联:
var PrinterPath = "\\\\" + PrintServer + "\\" + Printer;
You can use basic string concatenation , or possibly template literals . 您可以使用基本的字符串连接 ,也可以使用模板文字 。 The difference is that template literals are new to JavaScript and aren't supported on Internet Explorer at all, just modern browsers. 区别在于模板文字是JavaScript的新功能,而Internet Explorer完全不支持,而仅是现代浏览器。 You can check the compatibility table . 您可以检查兼容性表 。 The change you'd have to make would just be the following line. 您必须进行的更改仅是以下几行。 Below I've listed the two solutions. 下面,我列出了两种解决方案。
With string concatenation: 使用字符串连接:
var PrinterPath = "\\\\" + PrintServer + "\\" + Printer;
With template literals: 使用模板文字:
var PrinterPath = `\\\\${PrintServer}\\${Printer}`;
It looks like you want to concatenate a few strings together. 看起来您想将几个字符串连接在一起。 Here's one way to do that in JavaScript: 这是在JavaScript中执行此操作的一种方法:
var PrintServer = 'printserver-a';
var Printer = 'printer-a';
var PrinterPath = '\\\\' + PrintServer + '\\' + Printer;
In modern JavaScript (aka ES6) you can use template literals to accomplish what you want, like so: 在现代JavaScript(也称为ES6)中,您可以使用模板文字来完成所需的操作,例如:
var PrintServer = 'printserver-a';
var Printer = 'printer-a';
var PrinterPath = `\\\\${Printer}\\${Printer}`;
Note that PrinterPath must use backticks instead of single quotes. 请注意,PrinterPath必须使用反引号而不是单引号。
Now here's are an unsolicited tip, since you're just starting to learn JavaScript: The general convention in JS is to use variable names that are not capitalized (the exception to this would be for "class" functions). 现在,这是一个不请自来的提示,因为您才刚刚开始学习JavaScript:JS中的常规约定是使用不使用大写字母的变量名(“类”函数例外)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.