2016-11-24 82 views
0

我正在使用收据打印机打印过期标签。它使用触摸屏按钮组来打开打印窗口以将文档发送到打印机。在打印窗口中,我需要使用Javascript来编写产品名称和过期长度。从javascript中写入字符串中的字符串

我如何去简单地写了查询的内容: 例..

http://www.url.com/printLabel.html?product=Teriyaki&expiry=48hr

打印窗口

<script> 
document.write ("Teriyaki"); 
document.write ("48hr"); 
</script> 
+2

你可能在寻找这样的:http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript –

+0

页面上是否有任何HTML? – Ted

+0

Hi @AlfredXing我在寻找一个非jQuery的解决方案 - 这只是一个简单的快速脚本来替换破损的标签打印机 – hertingford

回答

0

希望我理解你的要求,这可以帮助你。在下面的代码片段中,我使用正则表达式来解析出来自窗口的“位置字符串”。

var thepath = window.location; 
 
    //here I'm just overwritting the path to make sure I have your required params in it... 
 
    thepath = "http://stackoverflow.com/questions/40793279/write-string-from-url-in-javascript?product=ThisOne&expiry=48h"; 
 
    var prodPattern = new RegExp(/product=([^&]*)/); 
 
    var expPattern = new RegExp(/expiry=([^&]*)/); 
 
    
 
    console.log(prodPattern.exec(thepath)[1]) 
 
    console.log(expPattern.exec(thepath)[1])