By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,660 Members | 1,295 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,660 IT Pros & Developers. It's quick & easy.

prototype.js breaking my Javascript

P: n/a
I am trying to use prototype.js to log javascript errors among other
things but by including

<script type="text/javascript" src="prototype.js"></scriptin my
page some of my existing javascript no longer works

for example

function submitAction(form,newaction,hiddenName,hiddenValue ) {

form.action=newaction;
//alert(mike.ADMUSER.value);

for ( i in hiddenName) {
var x = form[hiddenName[i]]
x.value = hiddenValue[i];
}
//form.submit();
alert(x.value)

}
This is called in the page like so:
<form name="main" method="post">
<a href="#" onClick="submitAction(document.main,'menuforms.php ',
['EVENT'],['bttf07'])">BTTF</a>

<input type="hidden" name="EVENT">
</form>

Can anyone explain why this gives me a Javascript error

Thanks

Feb 7 '07 #1
Share this Question
Share on Google+
3 Replies

P: n/a
On Feb 7, 9:37 am, mike_solo...@lineone.net wrote:
I am trying to use prototype.js to log javascript errors among other
things but by including

<script type="text/javascript" src="prototype.js"></scriptin my
page some of my existing javascript no longer works

for example

function submitAction(form,newaction,hiddenName,hiddenValue ) {

form.action=newaction;
//alert(mike.ADMUSER.value);

for ( i in hiddenName) {
var x = form[hiddenName[i]]
x.value = hiddenValue[i];
}
//form.submit();
alert(x.value)

}

This is called in the page like so:
<form name="main" method="post">
<a href="#" onClick="submitAction(document.main,'menuforms.php ',
['EVENT'],['bttf07'])">BTTF</a>

<input type="hidden" name="EVENT">
</form>

Can anyone explain why this gives me a Javascript error

Thanks
Well, first off, your "this gives me a javascript error" is not
especially helpful. Could you elaborate on what the text of the error
is? If you're developing using IE, chances are you aren't going to
get meaningful error messages to begin with; pick up Firefox and
Firebug.

Second, your syntax is incorrect for what you actually want. You're
treating an array like an object:

for (i in hiddenName) {} // this will iterate through all of an
object's properties; if you pass it an array it will iterate over its
enumerable items as well as added properties and methods
for (i=0; i<hiddenName.length; i++) {} // this is what you want: only
iterate over the object's enumerable properties

The reason why Prototype exposes this incorrect syntax is because it
adds properties to the built-in Array object's prototype like so:

Array.prototype.flatten = function() { /* code */ }

Try entering this code in the address bar of your page running
Prototype.js and it should all make sense:

javascript:var s='', a=[1,2,3];for (var n in a) { s += n+':'+a[n]
+'\n'; } alert(s);

David

Feb 7 '07 #2

P: n/a
On 7 Feb, 18:25, "David Golightly" <davig...@gmail.comwrote:
On Feb 7, 9:37 am, mike_solo...@lineone.net wrote:


I am trying to use prototype.js to log javascript errors among other
things but by including
<script type="text/javascript" src="prototype.js"></scriptin my
page some of my existing javascript no longer works
for example
function submitAction(form,newaction,hiddenName,hiddenValue ) {
form.action=newaction;
//alert(mike.ADMUSER.value);
for ( i in hiddenName) {
var x = form[hiddenName[i]]
x.value = hiddenValue[i];
}
//form.submit();
alert(x.value)
}
This is called in the page like so:
<form name="main" method="post">
<a href="#" onClick="submitAction(document.main,'menuforms.php ',
['EVENT'],['bttf07'])">BTTF</a>
<input type="hidden" name="EVENT">
</form>
Can anyone explain why this gives me a Javascript error
Thanks

Well, first off, your "this gives me a javascript error" is not
especially helpful. Could you elaborate on what the text of the error
is? If you're developing using IE, chances are you aren't going to
get meaningful error messages to begin with; pick up Firefox and
Firebug.

Second, your syntax is incorrect for what you actually want. You're
treating an array like an object:

for (i in hiddenName) {} // this will iterate through all of an
object's properties; if you pass it an array it will iterate over its
enumerable items as well as added properties and methods
for (i=0; i<hiddenName.length; i++) {} // this is what you want: only
iterate over the object's enumerable properties

The reason why Prototype exposes this incorrect syntax is because it
adds properties to the built-in Array object's prototype like so:

Array.prototype.flatten = function() { /* code */ }

Try entering this code in the address bar of your page running
Prototype.js and it should all make sense:

javascript:var s='', a=[1,2,3];for (var n in a) { s += n+':'+a[n]
+'\n'; } alert(s);

David- Hide quoted text -

- Show quoted text -
David

Thanks very much - I'd been looking at it for hours

Now just got to test the other 2000 lines of code in my javascript
file :(

Feb 7 '07 #3

P: n/a
On Feb 8, 3:37 am, mike_solo...@lineone.net wrote:
I am trying to use prototype.js to log javascript errors among other
things but by including
Use Firefox + Firebug, you will find it far more useful for debugging
that Prototype.js, even if your target is IE only.

<URL: https://addons.mozilla.org/search.php?
q=Firebug&type=E&app=firefox >
>
<script type="text/javascript" src="prototype.js"></scriptin my
page some of my existing javascript no longer works
Prototype is not name-spaced, you likely have a conflict with some
other variable with the same name. I don't know enought about it to
say where off the top of my head and I don't feel like trawling
throught its few thousand lines of code to find out. :-)
>
for example

function submitAction(form,newaction,hiddenName,hiddenValue ) {

form.action=newaction;
//alert(mike.ADMUSER.value);

for ( i in hiddenName) {
It is a very bad idea to let counters slip into the global space,
always declare variables with var unless you have a good reason not
to.

for (var i in hiddenName) {

Also, before version 1.5, Prototype.js extended Object, so you might
be having trouble here. In version 1.5, Prototype itself started to
use for..in so it no longer extends the built-in Object object.

[...]
>
Can anyone explain why this gives me a Javascript error
If it all works without Prototype.js then I'd advise you to not use it
and find a better behaved library. If you say what you are primarily
using it for (other than debugging) you might get some suggestions.
The Yahoo! UI stuff is pretty good, but might be a bit big for what
you are doing:

<URI: http://developer.yahoo.com/javascript/ >
--
Rob

Feb 7 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.