prototype.js breaking my Javascript

Discussion in 'Javascript' started by mike_solomon@lineone.net, Feb 7, 2007.

  1. Guest

    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
     
    , Feb 7, 2007
    #1
    1. Advertising

  2. Guest

    On 7 Feb, 18:25, "David Golightly" <> wrote:
    > On Feb 7, 9:37 am, 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"></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

    >
    > 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, 2007
    #2
    1. Advertising

  3. RobG Guest

    On Feb 8, 3:37 am, 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"></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/ >


    --
    Rob
     
    RobG, Feb 7, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Amy
    Replies:
    0
    Views:
    513
  2. June Lee
    Replies:
    2
    Views:
    809
    Jim Cobban
    Apr 13, 2008
  3. Replies:
    9
    Views:
    198
    Thomas 'PointedEars' Lahn
    May 26, 2006
  4. Replies:
    3
    Views:
    271
  5. javascript fish
    Replies:
    0
    Views:
    176
    javascript fish
    Oct 11, 2008
Loading...

Share This Page