It works if you're someone like Amazon that can pay based on sales generated, but that's actually somewhat unusual. A lot of advertisers are hoping to also push their brand, and there fraud is the big problem. A lot of the JS served is just validating that the ads displayed properly, or was seen by the correct target audience.