Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How javascripts are identified in/from and to the HTML section?

Status
Not open for further replies.

HollyVally

Programmer
Jan 3, 2003
48
BD
Hi all,

Here is how I have organised the structure:
a) all the js scripts are in file -- 'scriptfile.js'
b) all the HTMLs are created on the fly from a PHP script.
However, I haven't given the PHP - rather the output as I have got fron view source file!

HTML generation are done in 3 steps: Pick-a-choice, buffer-zone, Put-the-choice(see the code pls.)
which are individually created for the user-end.

[bold]Problems facing[/bold]: 1) none of the inline function-calls from the HTML section reaches the scripts
2) but if 'alert()' is used instead of calling the local function the alert comes up when clicked!

Can any one pls spot the problem and explain me where did I do wrong?

In my file scriptfile.js I have the following:

<SCRIPT language = &quot;javascript&quot;>
<!--
function shouldset(passon){
chosenNumber(passon)
}

function chosenNumber(elem){
document.form.chosenNumber.value=elem
document.form.YouHaveChosen.value='You have chosen'+elem
}

function setChosenNumber(cell){
crossNumber.&quot;cell&quot;+cell.value=crossNumber.chosenNumber.value
crossNumber.YouHaveChosen.value=&quot;Click now!&quot;
crossNumber.chosenNumber.value=&quot;&quot;
}
//-->
</script>


... and in my scriptfile.html I have the following form created through PHP:

<FORM action=&quot;CN.php&quot; method=&quot;post&quot; enctype=&quot;multipart/form-data&quot; name=&quot;formCN&quot;>

<table cellspacing=&quot;0&quot; cellpadding=&quot;0&quot;><!-- table 1 -->
<TR>
<TD colspan=&quot;10&quot; class=&quot;number&quot;>To choose a number<BR>Click on it</td>
</tr>
<TR><!-- Pick Choice -->
<td class=&quot;number&quot;><A href=&quot;javascript:chosenNumber('1');>1</A></TD>
<td class=&quot;number&quot;><A href=&quot;javascript:chosenNumber('2');>2</A></TD>
...
<td class=&quot;number&quot;><A href=&quot;javascript:chosenNumber('9');>9</A></TD>
<td class=&quot;number&quot;><A href=&quot;javascript:chosenNumber('0');>0</A></TD>
</TR>
<TR><!-- buffer zone -->
<TD colspan=&quot;10&quot; class=&quot;number&quot;>
<INPUT type=&quot;text&quot; name=&quot;chosenNumber&quot; value=&quot;&quot; onChange=&quot;shouldset(this.value)&quot;>
<INPUT type=&quot;text&quot; name=&quot;YouHaveChosen&quot; class=&quot;gamecell&quot; value=&quot;Click Now!&quot; onChange=&quot;shouldset(this.value)&quot; size=&quot;30&quot; readonly>
</td>
</tr>

</table>
<table class=&quot;number&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot;><!-- Table 2 -->
<tr><!-- Put Choice -->
<td><input name=&quot;cell1&quot; type=&quot;text&quot; class=&quot;number&quot; value=&quot;1&quot; onClick=&quot;setChosenNumber(this.formCN.cell1.name);this.blur()&quot;><img border=&quot;0&quot; src=&quot;../images/fill.gif&quot; width=&quot;10&quot; height=&quot;10&quot;></td>
<td><input name=&quot;cell2&quot; type=&quot;text&quot; class=&quot;number&quot; value=&quot;2&quot; onClick=&quot;setChosenNumber(this.formCN.cell2.name);this.blur()&quot;><img border=&quot;0&quot; src=&quot;../images/fill.gif&quot; width=&quot;10&quot; height=&quot;10&quot;></td>
...
<td><input name=&quot;cell5&quot; type=&quot;text&quot; class=&quot;number&quot; value=&quot;5&quot; onClick=&quot;setChosenNumber(this.formCN.cell5.name);this.blur()&quot;><img border=&quot;0&quot; src=&quot;../images/fill.gif&quot; width=&quot;10&quot; height=&quot;10&quot;></td>
</tr>
</form>
 
HollyVally,

Some goofs here.

First of all, in your external (scriptfile.js), it should look only like this:

Code:
function shouldset(passon){
    chosenNumber(passon)
}

function chosenNumber(elem){
    document.form.chosenNumber.value=elem
    document.form.YouHaveChosen.value='You have chosen'+elem
}

function setChosenNumber(cell){
    crossNumber.&quot;cell&quot;+cell.value=crossNumber.chosenNumber.value
    crossNumber.YouHaveChosen.value=&quot;Click now!&quot;
    crossNumber.chosenNumber.value=&quot;&quot;
}

You just don't need all that other junk -- a JS file is purely JS, not HTML.

Second, you have to declare your JS file in the body of your HTML file. So, in the head of your HTML document, add this line:

Code:
<script src=&quot;scriptfile.js&quot; type=&quot;text/javascript&quot;></script>

That should get you back on the bicycle.

Cheers,


[monkey] Edward [monkey]

&quot;Cut a hole in the door. Hang a flap. Criminy, why didn't I think of this earlier?!&quot; -- inventor of the cat door
 
Thanks Edward,
[bold]You just don't need all that other junk -- a JS file is purely JS, not HTML[/bold]
Yes you are absolutely right! but I have used .js not for the compiler to understand, but merely for my understanding. Because I have them included into a file.


yes I did exactly that but...

I actually have these 2 files included in another file in the following way:

.....
<?php require_once(&quot;phpClasses/scriptfile.php&quot;);
$game = new scriptfile(9); ?>
.....
<?php include(&quot;includes/scriptfile.js&quot;);?>
<?php echo $game->getgame();?>
......

Here above, scriptfile.php is the one that actually creates the previously mentioned html code on the fly.

... And it is not calling the function properly.

What I badly need is to know that where things are going wrong?

If the 'html' is not right than of course my 'html creation on-the-fly' is possibly not appropriate. In which case I'll need certainly to change the PHP script accordingly. But I am then lacking in my javascript apprehansion, which I again need to upgrade. --- which is why I came to the javascript forum!

On the otherhand, if my javascript function declarations are not okay than I must rectify my javascript functions as you guys might tell me better than the others.

Finaly, apart from these junk is my basic js coding right?

Thanks in advance.
 
&quot;You just don't need all that other junk -- a JS file is purely JS, not HTML&quot;

HollyVally, what Edward means is to leave the &quot;<script>&quot; and &quot;</script>&quot; tags out of your .js file altogether.

a .js include should look like this:

// begin of file
alert(&quot;Hello World&quot;);
// end of file

not this:
<script>
alert(&quot;Hello World&quot;);
</script>



=========================================================
try { succeed(); } catch(E) { tryAgain(); }
-jeff
 
Hi all,

Thanks to both of you(Edward & Jeff) for the info. Myself being a newcomer you can easily imagine my lack of knowledge in these fields! :-D

Cheers.
 
Hm. Funny, I think of a .JS file as an external collection of functions. Therefore, I would think of a .JS file as starting either with a comment or the word &quot;function&quot;.

Jeff, can you just toss JS commands into a JS file without sticking them in a function?

Curious,


[monkey] Edward [monkey]

&quot;Cut a hole in the door. Hang a flap. Criminy, why didn't I think of this earlier?!&quot; -- inventor of the cat door
 
Edward,

yes, though my example above was purely for illustration.

i commonly assign new class methods or attach functions to events in .js files - for example, these are technically statements even though they contain functions:

window.onload = function() {
alert(&quot;onload fired&quot;);
}

Array.prototype.ruin = function() {
this.length = 1;
this[0] = &quot;Your array has been ruined.&quot;;
}




=========================================================
try { succeed(); } catch(E) { tryAgain(); }
-jeff
 
This...

function chosenNumber(elem){
document.form.chosenNumber.value=elem
document.form.YouHaveChosen.value='You have chosen'+elem
}

should be this...

function chosenNumber(elem){
document.formCN.chosenNumber.value=elem
document.formCN.YouHaveChosen.value='You have chosen'+elem
}

and what's crossNumber? A different form?

Adam
while(ignorance==true){perpetuate(violence,fear,hatred);life--};
 
Thats right Adam,

What I am trying to do is a simple value assignment for a game and it result set: where both are updated as played.

Big thanks for that CN thing!

Cheers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top