prototype.js breaking my Javascript

M

mike_solomon

I am trying to use prototype.js to log javascript errors among other
things but by including

<script type="text/javascript" src="prototype.js"></script> in 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]
x.value = hiddenValue;
}
//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
 
M

mike_solomon

I am trying to use prototype.js to log javascript errors among other
things but by including
<script type="text/javascript" src="prototype.js"></script> in 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]
x.value = hiddenValue;
}
//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

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 :(
 
R

RobG

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"></script> in 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/ >
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top