HTML coding help, please
0I want to have a sentence that says “current lead times as of ______ is x amount of weeks”.
I want the underscored part to have today’s date. Is there a way to do that automatically?
I can do the date thing, have the script installed (the date is at the top of the page), but I can’t figure out how to actually get it to be part of a sentence. It’s for our company’s contact form. So tired of people asking for the lead time when it’s on that page twice. “Oh, I didn’t realize that was up to date” is the response we’ll get when they ask for the current lead time.
Thanks for any pointers you can give me.
- 8 comments, 9 replies
- Comment
“lead times” is plural, so the verb form is also plural - they “are” x amount of weeks.
There are no “functions” like “what is today’s date?” in HTML. Even if there were, where would that date information come from? You can’t ask the browser (displaying your HTML page) to go ask whatever computer it is running on for the time/date. Your webpage can’t know if that is possible.
So, you can run a program on your server that is hosting the webpage to get the date and then turn it into the static text in the HTML page. But that would mean your webserver isn’t just copying a file to a browser. It’s running a program to calculate what this web page needs to be before sending it to the browser, and your program pretends it is a file it just read off the disk to the browser. There are many “web server platforms” or web CMS (content management systems) that make it easy to combine a fixed html template with a snippet of code to do what you want. Wordpress is a popular one and it uses PHP for its programming language. There are plenty of others, like Tomcat, that is a basic webserver that makes it easy to use Java programs to create the web pages And others, like NodeJS, and on and on. Windows (server or Pro editions) has IIS that can use any windows scripting language (like VBScript). The list is simply endless for these web site builder tools.
Another way to do it is to do the programming (get the date and format it for your web page) in the browser itself. However, this requires the browser to:
and
So you don’t have an absolute guarantee if you rely on the user’s browser to be able to run your program code and give your webserver permission to run whatever code it wants to send to your browser.
This is why we have “drive-by” web exploits that do nasty unwanted things to your computer and now we can’t have nice things on the internet.
This way also uses a somewhat recent addition to HTML (i.e. the 90s), called “DHTML”, which means you can have some Javascript code in your webpage that changes the webpage to calculate the date and insert it at the right spot.
Lastly, when they figured out the browser would allow the HTML of the page it was displaying to be modified and updated in real time, (using DHTML and Javascript together), they also added this to a way to simplify and organize how web pages look - called style sheets, or CSS. They are snippets that can be in your web page, but they don’t display anything, they just tell your browser how to display the web page - things like margins, what fonts to use when and where, layers, and which ones to show when (i.e. viewing on a computer or a mobile phone, landscape or portrait mode, printing, and a lot more.
It turns out, because CSS has this “if this is true, then do that” capability, that means it is pretty much a programming language as well. And you can (ab)use CSS to render a dynamic date/time value.
So, TL;DR, there’s a couple of different ways to do what you want, and there are tradeoffs (server or client side, scripting available) and you probably have to learn a little coding in some “web” programming language - Javascript, PHP, Java, etc.
Pretty much if this is the only thing you want to do, unfortunately, all of the ways to do it are over-engineered and overkill for just the one simple thing you want to do. Some are complex and geared for developers, others are simplified all-in-ones and geared for the DIY non-programmer. So part of the work is deciding what tools you will use.
There’s even servers that can monitor a youtube video and do self-updating titles with view counts or whatever.
In other words, you have a program running on some other computer that changes your (static) HTML web page on your web server every day when the date changes. You probably want to schedule that program to run just after midnight every day.
You’ll just have to train your program to “know” the right spot where the date is, and replace the text, and save the changed web page to the server.
Here’s a walkthrough for the browser-side way to do it:
Thanks, it’s taken care of. It was a simple javascript.
@lisaviolet Make sure you put in a <noscript> tag to at least put something there to tell the user that the date is supposed to be there but it is being blocked because they’ve disabled JavaScript in their browser or an extension is blocking your awesome code from using their browser to run it.
WORKER BEES! HERCULES! TURKEY GREASE! AWESOME!
@lisaviolet That probably doesn’t work on a Kindle. At least it takes into account the user’s preference for how to display dates, something the server side cannot know (without extra work to find out).
That may not apply, but you would be surprised to learn how “public” the public internet is.
You can’t always assume that the thing you use to interact with “The Intertubes” is the same thing the other 8 Billion things on the planet connected to the internet ate using.
@mike808 It does work on a kindle. I tested it.
@mike808 The part that’s in script shouldn’t matter if it shows or not. It’s just the date in parenthesis.
I’ve got it done, it’s okay. I’m finished with it. I’ve been finished with it.
You kids have a the fancy toys these days.
I’m this old.
There’s also a server-side way. It’s more compatible with browsers but requires a little research to check which server you’re using and may require a different file name like .dhtml instead of .html or a line in a config file to enable. Most commonly it would be set in angled brackets like any other html command and prefaced with a !
I’m certain the forum will munch it if I send the command ; there’s silly things you can do with server-side includes from user-entered text like buffer overruns to read or write out-of-bounds memory or even issue commands to the host computer. So the angled brackets ought to get replaced with their ampersand-escaped html entity equivalents. And the ampersand gets escaped a lot, too. So it gets very “Fight Club”-esque to type the commands in a forum like this because the server has one rule. “We don’t talk about server-side includes!”
But imagine this command set in your favorite < angled brackets > and try plugging it into stack overflow for clarification:
"As of --!echo var=DATE_LOCAL – your current lead time on this order is --!echo var=LISAVIOLET_LEAD_TIME – weeks. "
Ugh. That was fun on a phone keyboard. But, hey! No JavaScript required! Serves as plaintext. Parsed as the page is loaded. Entirely compatible on the client side![:)](https://dj5zo597wtsux.cloudfront.net/joypixels/assets/6.6/png/unicode/64/1f642.png)
What makes this fun and effective to me is the same server-side includes can be used to format your pages with a header and footer. So you include a “set var=LISAVIOLET_LEAD_TIME value=‘5’” in your header. It never leaves the server because it’s in the angled brackets. But all your lead time messages have access to the value you set ; when the lead time changes, update the header, and all the pages pick up the change the moment they’re served.
It’s fun! https://httpd.apache.org/docs/current/howto/ssi.html Has a decent tutorial. And there’s an IIS equivalent if your server happens to be Microsoft-based instead of Apache-based.
@2palms What about Tomcat based? Or NodeJS based? Or NGINX based? Or a Wordpress site? Or any number of http servers and content management frameworks that have their own unique method of invoking either SSIs (Server-Side Includes) or externally generated web pages. For Apache, you would also need to enable SSIs and that requires the ability to edit the config or have a fancy GUI control panel that can do it for you.
Anyway, the OP went with some client-side JavaScript, which is fine. That said, the ask was a bit on the “My car is making this funny noise. Any idea on how to fix it?” side. No way to offer a specific solution without making some assumptions, which also results in conflicting permutations of solutions.
Hm. On my phone the double hyphen (which is really important to the server) got replaced by an Em-dash. I can’t tell if that’s a font or browser setting over here or if it’s “I’m helping make fonts prettier” from Meh bot. But the brackets sorta survived. Better to check the link than to copy-paste whatever survived the forum engine.
<!‐‐echo var=DATE_LOCAL ‐‐>
@2palms Inline code mehdown is what you want. Or a code block.
As in
<!-- echo var=DATE_LOCAL -->
(using backquotes).@2palms @mike808 You can look at some examples or code if you want to know all about Mehdown.
@mike808 thanks!