SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2004
    Location
    NJ
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Can't update document.getElementById("objects").innerHTML in a for loop!!!

    ok,
    I'm making a physics program, and the following code works:

    <html>
    <head>
    <title>Physics</title>
    </head>
    <body onClick="main()">
    <form name=form1>
    <input type=text name=x value=300>
    <input type=text name=y value=200>
    <input type=text name=xf value=50>
    <input type=text name=yf value=100>
    </form>
    <script language=javascript type=text/javascript>
    function main(){
    for (var i=0; i<100; ++i){
    x=document.form1.x.value;
    y=document.form1.y.value;
    xf=document.form1.xf.value;
    yf=document.form1.yf.value;
    var x2=var_return(1, x, y, xf, yf, 3);
    var y2=var_return(2, x, y, xf, yf, 3)
    var xf2=var_return(3, x, y, xf, yf, 3);
    var yf2=var_return(4, x, y, xf, yf, 3);
    var c='<IMG border=2 SRC=rect01.gif STYLE="position:absolute; TOP:'+x2+'px; LEFT:'+y2+'px">';
    document.getElementById("objects").innerHTML=c;
    document.form1.x.value=x2;
    document.form1.y.value=y2;
    document.form1.xf.value=xf2;
    document.form1.yf.value=yf2;
    }
    }



    function var_return(variable, xpos, ypos, xforce, yforce, mass){
    var z=variable;
    var x=xpos;
    var y=ypos;
    var xf=xforce;
    var yf=yforce;
    var m=mass;
    var Cd=.5;
    var rho=.0024;
    var A=116;
    var g=9.80665;
    var W=m*g
    var Vt = Math.sqrt((2*W)/(Cd * rho * A));
    var xa=xf/m;
    var glimit_xa=0;
    if (xa<Vt){
    var glimit_xa=xa;
    }
    if (xa>Vt){
    var glimit_xa=Vt;
    }
    var g_xf=glimit_xa*m;
    var ya=yf/m;
    var g_ya=(ya*1)-(g*1);
    var glimit_ya=0;
    if (g_ya<-Vt){
    var glimit_ya=-Vt;
    }
    if (g_ya>-Vt){
    var glimit_ya=g_ya;
    }
    var g_yf=glimit_ya*m;
    var x2=(x*1)+(glimit_xa*1);
    var y2=(y*1)+(glimit_ya*1);

    if (z==1){
    return x2;
    }
    if (z==2){
    return y2;
    }
    if (z==3){
    return g_xf;
    }
    if (z==4){
    return g_yf;
    }
    }
    </script>

    <div id="objects"><script language=javascript type=text/javascript>
    document.write(c);
    </script></div>

    </body>
    </html>

    but I don't want to use the form and inputs as a form of global variables....so, I made code without them and used only variables and functions:

    <html>
    <head>
    <title>Physics</title>
    </head>
    <body onClick="main()">
    <script language=javascript type=text/javascript>
    function main(){
    var x=200;
    var y=300;
    var xf=50;
    var yf=100;
    for (var i=0; i<100; ++i){
    var x2=var_return(1, x, y, xf, yf, 3);
    var y2=var_return(2, x, y, xf, yf, 3)
    var xf2=var_return(3, x, y, xf, yf, 3);
    var yf2=var_return(4, x, y, xf, yf, 3);
    var c='<IMG border=2 SRC=rect01.gif STYLE="position:absolute; TOP:'+x2+'px; LEFT:'+y2+'px">';
    document.getElementById("objects").innerHTML=c;
    x=x2;
    y=y2;
    xf=xf2;
    yf=yf2;
    }
    }



    function var_return(variable, xpos, ypos, xforce, yforce, mass){
    var z=variable;
    var x=xpos;
    var y=ypos;
    var xf=xforce;
    var yf=yforce;
    var m=mass;
    var Cd=.5;
    var rho=.0024;
    var A=116;
    var g=9.80665;
    var W=m*g
    var Vt = Math.sqrt((2*W)/(Cd * rho * A));
    var xa=xf/m;
    var glimit_xa=0;
    if (xa<Vt){
    var glimit_xa=xa;
    }
    if (xa>Vt){
    var glimit_xa=Vt;
    }
    var g_xf=glimit_xa*m;
    var ya=yf/m;
    var g_ya=(ya*1)-(g*1);
    var glimit_ya=0;
    if (g_ya<-Vt){
    var glimit_ya=-Vt;
    }
    if (g_ya>-Vt){
    var glimit_ya=g_ya;
    }
    var g_yf=glimit_ya*m;
    var x2=(x*1)+(glimit_xa*1);
    var y2=(y*1)+(glimit_ya*1);

    if (z==1){
    return x2;
    }
    if (z==2){
    return y2;
    }
    if (z==3){
    return g_xf;
    }
    if (z==4){
    return g_yf;
    }
    }
    </script>

    <div id="objects"><script language=javascript type=text/javascript>
    document.write(c);
    </script></div>

    </body>
    </html>

    as you can see, they are both essentially the same exact thing, and yet the code with just the variables doesn't work. Well, it does KINDA work in that it displays it when it gets to the end of the loop, but not during, like its supposed to. Technically, it's NOT DOING WHAT I TOLD IT TO DO!!! Its not doing the following:
    var c='<IMG border=2 SRC=rect01.gif STYLE="position:absolute; TOP:'+x2+'px; LEFT:'+y2+'px">';
    document.getElementById("objects").innerHTML=c;

    Someone please help me figure out how I can get this program to work without the use of the forms.

    Thanx

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm making a physics program, and the following code works:
    I doubt it. Your code is essentially this:

    an html form

    function X(){}
    function Y(){}


    <div id="objects"><script language=javascript type=text/javascript>
    document.write(c);
    </script></div>

    </body>
    </html>

    How are you proposing that your browser knows what 'c' is? In addition, jamming a <script> between some <div> tags serves no purpose--remove it. In fact, get all of your javascript out of the html and into the <head> tags.

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2004
    Location
    NJ
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    umm....

    No offense, but you are completely focusing on the wrong things, it donsn't matter what is inside the div and what the value of c is, I know c doesn't equal anything and i just left it there for the hell of it, because I don't want anything to display untill I click. Secondly, although you probably are right about putting the div inside the javascript, it really doesn't make a difference, I had set that orignally and never found any particualy NEED to change it. Any way, if you can help me with my actual question, it would be helpful. Besides, what do you mean you doubt it? Just put the code in notepad, make the file and test it, it works, why the hell would i say it if it didn't? The only reason it wouldn't work would be browser problems, i personally tried it both on mozilla and ie6.
    thanx

  4. #4
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it works, why the hell would i say it if it didn't?
    ie6:
    An error has occurred in the script on this page.
    Line: 85
    Error: 'c' is undefined

    Firefox1.0:
    fails silently--nothing is written to the page

  5. #5
    SitePoint Enthusiast
    Join Date
    Nov 2004
    Location
    NJ
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    ummm....

    ummm...yeah, no offense, but seeing as how I pointed out all the irrelevant information in your last post, I'm not sure why you posted again if you don't know the answer. What can I say, I guess maybe the operating system matters, I don't know.....But i just tried and and all I know it is that it worked. Let me emphasize it again, stop commenting on how the c value is undefined, it has nothing whatsoever to do with me trying to get that code to work without using a form....I KNOW THE C VALUE IS UNDEFINED, GET OVER IT! In fact, here, i'll define it for you:
    <html>
    <head>
    <title>Physics</title>
    </head>
    <body onClick="main()">
    <form name=form1>
    <input type=text name=x value=300>
    <input type=text name=y value=200>
    <input type=text name=xf value=50>
    <input type=text name=yf value=100>
    </form>
    <script language=javascript type=text/javascript>
    function main(){
    for (var i=0; i<100; ++i){
    x=document.form1.x.value;
    y=document.form1.y.value;
    xf=document.form1.xf.value;
    yf=document.form1.yf.value;
    var x2=var_return(1, x, y, xf, yf, 3);
    var y2=var_return(2, x, y, xf, yf, 3)
    var xf2=var_return(3, x, y, xf, yf, 3);
    var yf2=var_return(4, x, y, xf, yf, 3);
    var c='<IMG border=2 SRC=rect01.gif STYLE="position:absolute; TOP:'+x2+'px; LEFT:'+y2+'px">';
    document.getElementById("objects").innerHTML=c;
    document.form1.x.value=x2;
    document.form1.y.value=y2;
    document.form1.xf.value=xf2;
    document.form1.yf.value=yf2;
    }
    }



    function var_return(variable, xpos, ypos, xforce, yforce, mass){
    var z=variable;
    var x=xpos;
    var y=ypos;
    var xf=xforce;
    var yf=yforce;
    var m=mass;
    var Cd=.5;
    var rho=.0024;
    var A=116;
    var g=9.80665;
    var W=m*g
    var Vt = Math.sqrt((2*W)/(Cd * rho * A));
    var xa=xf/m;
    var glimit_xa=0;
    if (xa<Vt){
    var glimit_xa=xa;
    }
    if (xa>Vt){
    var glimit_xa=Vt;
    }
    var g_xf=glimit_xa*m;
    var ya=yf/m;
    var g_ya=(ya*1)-(g*1);
    var glimit_ya=0;
    if (g_ya<-Vt){
    var glimit_ya=-Vt;
    }
    if (g_ya>-Vt){
    var glimit_ya=g_ya;
    }
    var g_yf=glimit_ya*m;
    var x2=(x*1)+(glimit_xa*1);
    var y2=(y*1)+(glimit_ya*1);

    if (z==1){
    return x2;
    }
    if (z==2){
    return y2;
    }
    if (z==3){
    return g_xf;
    }
    if (z==4){
    return g_yf;
    }
    }
    </script>

    <div id="objects"><script language=javascript type=text/javascript>
    document.write("YOU HAPPY NOW!!!");
    </script></div>

    </body>
    </html>

    There you go...
    And by the way, I kinda forgot to tell you, you have to click anywhere on the document to actually see the block moving...If you see something moving, its working.

  6. #6
    SitePoint Addict markchivs's Avatar
    Join Date
    Oct 2004
    Location
    Malvern Hills, UK
    Posts
    233
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I can see numbers changing does that count?

    Don't get any errors

    Don't see anything move

    Don't know why your being such a t**t when you've come on here to ask for help and people are offering you their experience at no cost and at the expense of their own time. Maybe you should go to another forum where impatient people who have difficulties with communication are given more assistance



  7. #7
    SitePoint Enthusiast
    Join Date
    Nov 2004
    Location
    NJ
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i'm not sure what t**t means , but i never tried to be offensive, and always tried to end positively, actually i kinda of misjudged what kinda people were on this forum, it was more of a joke than anything offensive, i'm used to having a little attitude and just as much as much used to forums where other people also have a little attitude. so...if i offended anyone, i'll take it back, i didn't mean to. from now on i'm not even gonna comment on any one's post if and until someone figures out the answer

    by the way, if anyone wants to say that i wrote too much to be commented on, and i should just give up tring to find the answer, go ahead i would be more than happy to be aware of the impossibility of my original question.

    Thanx

  8. #8
    SitePoint Enthusiast
    Join Date
    Nov 2004
    Location
    NJ
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    just wondering

    i'm just wondering, how long should I wait for an answer before I should officially call it quits?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •