The forum of the forums
Would you like to react to this message? Create an account in a few clicks or log in to continue.

JavaScript Error in Widgets

3 posters

Go down

Solved JavaScript Error in Widgets

Post by Cyberwatt August 11th 2009, 8:09 am

Hi all,

I'm trying to incorporate a small form and javascript into a forums widget to simulate a dice roll. It works fine on a separate HTML page, but I get a "variable undefined" error when I try to place the code in a widget. See the code below. Any ideas as to why a widget might object to the following?

<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
var die = 6;
var dice = 3;
function dice_roll(die, dice) {
var roll = 0;
for (loop=0; loop < dice; loop++) {
roll = roll + Math.round(Math.random() * die) % die + 1;
}
document.form.text.value = roll;
}
// End -->
</script>

<center>
<form name=form>
<table border=2 cellpadding=5>
<tr>
<td colspan=2 align=middle>What dice type?</td>
<td colspan=2 align=middle>How many dice to roll?</td>
</tr>
<tr>
<td valign=top align=middle>
<p><input type=radio name=sides onclick="die = 3">3 Sided
<p><input type=radio name=sides onclick="die = 4">4 Sided
<p><input type=radio name=sides onclick="die = 5">5 Sided
<p><input type=radio checked name=sides onclick="die = 6">6 Sided
<p><input type=radio name=sides onclick="die = 8">8 Sided
</td>
<td valign=top align=middle>
<p><input type=radio name=sides onclick="die = 10">10 Sided
<p><input type=radio name=sides onclick="die = 12">12 Sided
<p><input type=radio name=sides onclick="die = 20">20 Sided
<p><input type=radio name=sides onclick="die = 30">30 Sided
<p><input type=radio name=sides onclick="die = 100">100 Sided
</td>
<td valign=top align=middle>
<p><input type=radio name=number onclick="dice = 1">1
<p><input type=radio name=number onclick="dice = 2">2
<p><input type=radio checked name=number onclick="dice = 3">3
<p><input type=radio name=number onclick="dice = 4">4
<p><input type=radio name=number onclick="dice = 5">5
</td>
<td valign=top align=middle>
<p><input type=radio name=number onclick="dice = 6">6
<p><input type=radio name=number onclick="dice = 7">7
<p><input type=radio name=number onclick="dice = 8">8
<p><input type=radio name=number onclick="dice = 9">9
<p><input type=radio name=number onclick="dice = 10">10
</td>
</tr>
<tr>
<td align=middle colspan=4>
<input type=button value="Roll Dice" name=button onclick="dice_roll(die, dice)">
<input type=text size=10 name=text>
</td>
</tr>
</table>
</form>
</center>
avatar
Cyberwatt
New Member

Posts : 2
Reputation : 0
Language : English

Back to top Go down

Solved Re: JavaScript Error in Widgets

Post by ankillien August 11th 2009, 12:46 pm

Hello,

I think the widget objects this because the <script> part should go inside the head tags what a widgets can't contain.

You can create an HTML page for this and then use <iframe> tag to show it in a widget.

To do this, create an HTML page and put the whole code in it. It work fine in HTML page, right?
Now create a widget and put this code in it...

Code:
<iframe width="100%" height="100%" src="HTML PAGE LINK HERE"></iframe>

Now it should work in widget.
ankillien
ankillien
Energetic

Posts : 5198
Reputation : 129
Language : English, XHTML, CSS, JS, PHP, SQL

Back to top Go down

Solved Re: JavaScript Error in Widgets

Post by Cyberwatt August 11th 2009, 9:39 pm

Many thanks! That did it. Smile
avatar
Cyberwatt
New Member

Posts : 2
Reputation : 0
Language : English

Back to top Go down

Solved Re: JavaScript Error in Widgets

Post by Sanket August 11th 2009, 10:58 pm

Since this thread appears to be solved, I will lock this thread and mark it as solved.
Sanket
Sanket
Sanket
ForumGuru

Male Posts : 48766
Reputation : 2830
Language : English
Location : Mumbai

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum