Javascript issues |
Javascript issues |
Brian Chandler |
Jul 7 2021, 03:07 AM
Post
#1
|
Jocular coder Group: Members Posts: 2,460 Joined: 31-August 06 Member No.: 43 |
The big question, of course, is where is the real specification/manual for JS? Or does it have one? Is it like Topsy/ Are there just various manufacturers' interpretations of what thing were prbably meant to mean? But more practically...
*1* I want to add an 'onload' handler to a page, to fire the js which reads the current basket from a cookie and displays it. I can do this of course by the usual method of writing "onload={javascript text}" in the html body tag, but this means manual editing of a couple of hundred pages, so I tried to add it using javascript. Here's an example (everything is a work in progress...): Page: [https://imaginatorium.com/conan.html] java script: [https://imaginatorium.com/js/base.js] Unfortunately, since this javascript is running when it gets loaded, in the html head tag, on the console (in Firefox at least) this gives an error message: "TypeError: document.body is null" - so presumably until the browser gets to the <body> tag, this can't be used. And I can't see any other immediate way to attach it. Any suggestions? *2* If you play around with the "add to basket" buttons, you should soon get things like "Basket: 2 items", but you will also soon get "Basket: 222 items", because js can happily add 1 and 1 to get either 2 or 11 (because '+' is an overloaded operator for both addition and string concatenation). AIUI, there is no (cast) operator ("We don't need no stinking type stuff..."), but there is a function Number() which generates a number always. What is the best strategy? Just Number() everything?? |
Christian J |
Jul 11 2021, 03:40 AM
Post
#2
|
. Group: WDG Moderators Posts: 9,681 Joined: 10-August 06 Member No.: 7 |
There's also window.onload, of course.
|
Lo-Fi Version | Time is now: 5th June 2024 - 10:20 PM |