Author Topic: WeatherCat Tags Inside Scripts?  (Read 2473 times)

KeithC

  • Strong Breeze
  • ***
  • Posts: 118
    • Denton Rainfall
  • Station Details: Vantage Pro2 (daytime FARS) w/ Macbook OS 10.9.2
WeatherCat Tags Inside Scripts?
« on: May 25, 2013, 03:13:36 PM »
Hello fellow WeatherCatters!

My question isn't the result of any kind of problem I'm having - I'm not having any problems with this; I'm just asking out of curiosity.

If I put a WC tag inside a JavaScript script, the script won't work. But any script CAN go get the data replaced by a tag and use it at will - no problem.

It would seem to me that, since the tags are replaced with data before uploading, and since JavaScript is client-side, that a tag should be able to go inside a script, where it would then be replaced by its corresponding data before uploading, and the script would work as it should in the browser. But that hasn't been the case.

Does anyone know why this is?

Thanks!

Keith

saratogaWX

  • Strong Breeze
  • ***
  • Posts: 136
  • Weather website programming enthusiast
    • Saratoga-Weather.org
  • Station Details: Davis VP1 +, iMAC 21, OSX 10.6.8
Re: WeatherCat Tags Inside Scripts?
« Reply #1 on: May 25, 2013, 05:29:15 PM »
Hi Keith,

The WeatherCat tags are only available for use inside files that WeatherCat processes and uploads to your website.  To use it in a JavaScript file on your site you would either have to have WeatherCat process and upload the JavaScript file routinely (not recommended), or have WeatherCat process and upload a small text file containing WC tags and have the JavaScript read that file, parse and format the output.

The latter is what I do with the Saratoga template set for AJAX-enabled display of conditions.
See: http://saratoga-weather.org/wxtemplates/index.php

BTW.. you don't have to use the template set at all.. you can 'steal with pride' the AJAX routine (and WC conditions template) from the WCT-plugin.zip and roll-your-own pages easily.

Best regards,
Ken
Ken True/Saratoga, CA, USA main site: saratoga-weather.org
Davis Vantage Pro Plus - FARS, Boltek-PCI/NexStorm, GRLevel3, WD, WL, VWS, Cumulus, Meteohub, WeatherSnoop, WeatherCat
Free weather PHP scripts/website templates - update notifications on Twitter saratogaWXPHP

KeithC

  • Strong Breeze
  • ***
  • Posts: 118
    • Denton Rainfall
  • Station Details: Vantage Pro2 (daytime FARS) w/ Macbook OS 10.9.2
Re: WeatherCat Tags Inside Scripts?
« Reply #2 on: May 25, 2013, 06:20:13 PM »
Hi Ken, and thank you for that answer! I will surely go AJAX harvesting when the need arises :-D

I left out the fact that these scripts were in the header of a web page that is "routinely processed" by WC, and not in an external JS file - if that matters. Right now, instead, I have all the WC tags that are used by my scripts in the body of that same web page. But since that whole page is pre-processed by WC before upload, it seems as though putting WC tags directly in JS scripts should work just fine, but it doesn't. For example:

<head>
<script>
var yr = STAT$RAIN:TOTAL:YESTERDAY$;
//...other stuff that works perfectly with a direct
// value inserted for the variable instead of a WC tag
</script>
</head>

One would think that WC tag would be replaced by a real value that JS could parse as a float and use in the script. But then the script doesn't work.

You said "(not recommended)" referring to doing this in "routinely processed" files. I'm curious as to why that is.

Here's my web page as I have it now with JS pulling tags from elsewhere on the same page:

http://dentonrainfall.com/

saratogaWX

  • Strong Breeze
  • ***
  • Posts: 136
  • Weather website programming enthusiast
    • Saratoga-Weather.org
  • Station Details: Davis VP1 +, iMAC 21, OSX 10.6.8
Re: WeatherCat Tags Inside Scripts?
« Reply #3 on: May 25, 2013, 06:26:44 PM »
Hi Keith,

.js files tend to be cached by the browser (as they seldom change) so putting them in .js file would likely result in not having current data.

.htm/.html files may also be cached by the browser (unless directed by <meta> tags to not cache the result)

Adding the WC tags inside a JavaScript embedded in a .htm/.html page as you show should work .. do you have an example URL (or can post the view-source) of the resulting page to see if the substitution is happening.

One other thing to keep in mind about JavaScript is that it is touchy .. one JavaScript error on the page and all JavaScript stops processing on that page.  Annoying, but it is what happens.

Best regards,
Ken
Ken True/Saratoga, CA, USA main site: saratoga-weather.org
Davis Vantage Pro Plus - FARS, Boltek-PCI/NexStorm, GRLevel3, WD, WL, VWS, Cumulus, Meteohub, WeatherSnoop, WeatherCat
Free weather PHP scripts/website templates - update notifications on Twitter saratogaWXPHP

KeithC

  • Strong Breeze
  • ***
  • Posts: 118
    • Denton Rainfall
  • Station Details: Vantage Pro2 (daytime FARS) w/ Macbook OS 10.9.2
Re: WeatherCat Tags Inside Scripts?
« Reply #4 on: May 25, 2013, 08:38:54 PM »
Hi Ken!

Yes, JS is touchy, as you said, and now that I've read your comments I think I did have the right idea but maybe had a tiny mistake somewhere that caused a JS rebellion. I'm sorry but I do not have an example to show you because I discarded all attempts when they didn't work. I will say though that I did see in some test runs that WC tags were being replaced correctly within my scripts, which was why I was confused; the scripts should have worked, but they didn't.

I'll try again with another test run or two and report back if I find my error(s), and I'll be sure to include source code.

Thanks again for your helpful input!

Keith

toadstone

  • Gentle Breeze
  • **
  • Posts: 29
    • Big Low Weather, Rainow.
  • Station Details: Station: None Intel i5 Hackintosh 16gb RAM Yosemite
Re: WeatherCat Tags Inside Scripts?
« Reply #5 on: March 19, 2014, 11:32:19 PM »
I'm not sure whether I'm on the same track or not here. I needed to resolve a minor issue with regard to reporting a value twice but using different units. In this case the barometer value in both mbs and inches Hg.  The reason for this was to oblige a neighbour who wanted to check his barometer.

Thinking the task through I decided that the simplest way was to perform a simple mathematical function using javascript. I'm no full blown programmer so forgive me if my resolution is non conformist! Alas it works.

The basic sum:  to convert millibars to inches of Hg:-    (current reading mbs x 29.92) divided by 1013.25= inches Hg

Quote
<script>
var x="STAT$PRESSURE:CURRENT$";      //this is the default pressure reading in millbars
var y=29.92;                                         //inches Hg per millibar
var z=x*y;
var a=z/1013.25;                                  //Standard pressure value
var num=parseFloat(a);                         //establish decimal floating point for var a

document.write(num.toFixed(2) + " ins Hg<br>");        //print the sum value to 2 decimal places along with the pressure units
</script>

The file that uses this script is placed in my custom web page folder and WeatherCat processes it then uploads it.

Because of the way I have built and customised my web site here are the links to see the:-

1. Bare result of the script above
http://www.biglow.co.uk/weather/php/qnhinch_hg.html

2. The page that uses the script in its place alongside other data
http://www.biglow.co.uk/weather/php/current_wx.html

3. Finally the home page that draws in the basic elements to make up the complete picture. I use Tumult Hype to create the iframe elements that allow all manner of data to be shown.
http://www.biglow.co.uk/weather/php/

So in my case the javascript solution in using the WeatherCat tags works.

Cheers, Peter.