0

I'm using a receipt printer to print expiry labels. It works using touch screen set of buttons which open a print window to send the document to the printer. In the print window I need to use Javascript to write the product name and expiry length.

How do I go about simply writing the contents of the query: Example..

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

Print Window

<script>
document.write ("Teriyaki"); 
document.write ("48hr"); 
</script>
George Kagan
  • 5,913
  • 8
  • 46
  • 50
  • 2
    You're probably looking for this: http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript – Alfred Xing Nov 24 '16 at 19:19
  • Is there any html on the page? – Ted Nov 24 '16 at 19:20
  • Hi @AlfredXing I was looking for a non jquery solution - this is just a simple quick script to replace a broken label printer – hertingford Nov 24 '16 at 19:45
  • Note that in 2016, the preferred solutions from the thread linked by Alfred Xing should be probably to use [URLSearchParams](http://stackoverflow.com/a/12151322/245966) built-in (which is low in the list of answers as it's a recent thing), and provide a polyfill for old browsers. – jakub.g Nov 24 '16 at 20:08
  • @hertingford The top answer on the linked question is non-jQuery. – Alfred Xing Nov 25 '16 at 03:06
  • @jakub.g Super useful, but [caniuse](http://caniuse.com/#search=URLSearchParams) says less than 60% of browsers support it, so I would personally be a bit hesitant to recommend it as a general solution at the moment – Alfred Xing Nov 25 '16 at 03:08

1 Answers1

0

Hopefully I understood your requirements and this can help you. In the below snippet, I'm using regular expressions to parse out the "location string" coming from the window.

    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])
blaze_125
  • 2,262
  • 1
  • 9
  • 19