View Full Version : putting code snippets into html

09-29-2011, 01:27 AM
Hi, I'm just now starting to try to insert some javascript into my html and php code. I'm only used to working with those two markups. I need to try some very simple javascript snippets in some forms I'm making, but am having an issue getting the code to work.

When I find these snippets, they are usually in a format like this:

// functions and whatnot here
function makeSomethingHappen(field, count, max) {

if (something.works > true)
field.value = i.will.crap(0, max);
// calculate the remaining characters
field.value = max - more.nodoze.caffein;

<form name=sample action="">
<input type="text" name="hours">

I've tried inserting the stuff like the webpages have it listed. I also try inserting everything contained in the <script> tags into the head of the document (or creating a head and putting it in there, since all my stuff is php and html code called to the file with php includes and whatnot).

Anyways, I hoping somebody can provide a pointer as to how to use these snippets or how to correctly place things like this. I'm assuming there's a relatively uniform way that people are posting their tiny little js codes and I'm missing some basic knowledge as to how to use it.

Any help is greatly appreciated.

Old Pedant
09-29-2011, 01:55 AM
Actually, it doesn't matter where you put it, for many purposes.

So maybe you need to provide a *real* example of what doesn't work for you.

Here's a clue, though:

If it's a PHP page, bring it up in your browser.

Then click on the VIEW menu item.

Then click on the SOURCE or PAGE SOURCE menu item.

That will show you the HTML (with JS) as the browser sees it.

If it doesn't look right, fix the PHP until it does.

If you think it does look right, but it still doesn't work for you, then copy/paste it here.

09-29-2011, 01:58 AM
for convenience, I tend to do it like this:

<!DOCTYPE html>
<meta charset="UTF-8">

<!-- html here-->

<script type="text/javascript">

//javascript here


so that the html elements are defined before the js starts trying to do stuff to them.

But there are various ways to do it. I guess others have their advantages.

If you're having trouble with some real code, why not post it?