Jump to content

form button in my java code


bw83

Recommended Posts

Im working on a button to initiate inside my script for setInterval. Without the button the script works fine but I wanna be able to leave the page open and just use a button to start or stop it.

<html>  <head>    <script type="text/javascript">    var frame_url = "http://api.msappspace.com/proxy/relay.proxy?opensocial_authtype=SIGNED&opensocial_token=o7H9ZTCi1TvHlRTZXxMzWF6DGlOE9a2bFqpVxwrqB9XdNbs3QaY/eps3Nm+3VTXopYM87CjwJ/D3C8SvRWvWMxAylK2n2CyJHLvbucKsyqU=&opensocial_url=http%3A//mob-dynamic-lb1.mobsters02.com/mob/heal%3Fuser_id%3D128559318%26level%3D89%26session_id%3D9abc68f653ee41a4ba1f8ce7146f0f0fda1dee09%26session_key%3D6b22e11a566c543a8606ea160af714da868bcc56";    function refresh_frame(frame_id, theurl)    {      var fr = document.getElementById(frame_id);      var now = new Date();      now = now.valueOf();      fr.src = theurl + "&now=" + now;    }        setInterval('refresh_frame("Heal", frame_url);', 400);   </script>  </head>  <body>    <IFRAME id="Heal" width=800 height=200 marginwidth=0 marginheight=0 frameborder=0 scrolling=auto></IFRAME>    <script type="text/javascript">    document.getElementById("Heal").src = frame_url;    </script>  </body></html>

that is the original script that works fine on page load.

<html>  <head>    <script type="text/javascript">    var frame_url = "http://api.msappspace.com/proxy/relay.proxy?opensocial_authtype=SIGNED&opensocial_token=o7H9ZTCi1TvHlRTZXxMzWF6DGlOE9a2bFqpVxwrqB9XdNbs3QaY/eps3Nm+3VTXopYM87CjwJ/D3C8SvRWvWMxAylK2n2CyJHLvbucKsyqU=&opensocial_url=http%3A//mob-dynamic-lb1.mobsters02.com/mob/heal%3Fuser_id%3D128559318%26level%3D90%26session_id%3D9abc68f653ee41a4ba1f8ce7146f0f0fda1dee09%26session_key%3D1c33870fc69a4c64a9da701d7c54a5a9a1064371";<form><input type="button" onclick="    function refresh_frame(frame_id, theurl)    {      var fr = document.getElementById(frame_id);      var now = new Date();      now = now.valueOf();      fr.src = theurl + "&now=" + now;    }        setInterval('refresh_frame("Heal", frame_url);', 400);"" value="Heal"/></form>   </script>  </head>  <body>    <IFRAME id="Heal" width=800 height=200 marginwidth=0 marginheight=0 frameborder=0 scrolling=auto></IFRAME>    <script type="text/javascript">    document.getElementById("Heal").src = frame_url;    </script>  </body></html>

when using this script and trying to use a button to start it the page is completely blank with no button. Do i have to close the open <script> right before the onClick button and then reopen another one? or does javascripting have a similar feature to the onclick button that would be better to replace it with?

Link to comment
Share on other sites

Ive tried about 5 different ways to cover about every way I could think of. Ive eliminated the < form > part of the code but left the button. Ive gotten the button to show up outside of the Iframe but it does nothing for the set interval part of it. If I try to put the button on the inside of the frame then I see no button at all.

Link to comment
Share on other sites

The second script has a lot of problems - mid-string newlines and intermangled HTML and JS just for starters.In the first script, you shouldn't set the interval before the page has loaded - if the page takes longer than 4/10 of a second to load you cause a JS error ("document" is not defined).Can you please show us how you're trying to set up the button in question?

Link to comment
Share on other sites

That was kind of the problem im running into with not wanting the setInterval to be started on page load. I basically just want a Iframe centered on the page (ive fixed that part just didnt worry about it at first its not that important) with a button above the iFrame. When I click on the botton I want it to start the interval for the url inside of the iFrame.

<html>  <head>    <script type="text/javascript">    var frame_url = "http://api.msappspace.com/proxy/relay.proxy?opensocial_authtype=SIGNED&opensocial_token=o7H9ZTCi1TvHlRTZXxMzWF6DGlOE9a2bFqpVxwrqB9XdNbs3QaY/eps3Nm+3VTXopYM87CjwJ/D3C8SvRWvWMxAylK2n2CyJHLvbucKsyqU=&opensocial_url=http%3A//mob-dynamic-lb1.mobsters02.com/mob/heal%3Fuser_id%3D128559318%26level%3D90%26session_id%3D9abc68f653ee41a4ba1f8ce7146f0f0fda1dee09%26session_key%3D1c33870fc69a4c64a9da701d7c54a5a9a1064371";    function refresh_frame(frame_id, theurl)    {      var fr = document.getElementById(frame_id);      var now = new Date();      now = now.valueOf();      fr.src = theurl + "&now=" + now;    }   </script>  </head>  <body>    <center>  <br><br><br>  <input type="button" onclick="setInterval('refresh_frame("Heal", frame_url);', 400);" value="Heal" />   <br><br><br>  <IFRAME id="Heal" width=1000 height=400 marginwidth=0 marginheight=0 frameborder=0 scrolling=auto> </IFRAME></center>    <script type="text/javascript">    document.getElementById("Heal").src = frame_url;    </script>  </body></html>

This one is about as close as i get. The page loads inside of the iFrame like intended and the button is visible but it doesnt start an interval when clicking on the button. Actually clicking on the button does nothing =/ but this is what im trying to make it look like or anything to that extent.

Link to comment
Share on other sites

The HTML parser thinks this is your onclick attribute:

"setInterval('refresh_frame("

EDIT: BTW, I realized your second script didn't have a lot of problems, but those it did have were glaring. I cleaned it up below, but some things could still be improved.

<html>	<head>		<script type="text/javascript">			var frame_url = "http://api.msappspace.com/proxy/relay.proxy?opensocial_authtype=SIGNED&opensocial_token=o7H9ZTCi1TvHlRTZXxMzWF6DGlOE9a2bFqpVxwrqB9XdNbs3QaY/eps3Nm+3VTXopYM87CjwJ/D3C8SvRWvWMxAylK2n2CyJHLvbucKsyqU=&opensocial_url=http%3A//mob-dynamic-lb1.mobsters02.com/mob/heal%3Fuser_id%3D128559318%26level%3D90%26session_id%3D9abc68f653ee41a4ba1f8ce7146f0f0fda1dee09%26session_key%3D1c33870fc69a4c64a9da701d7c54a5a9a1064371";						function refresh_frame(frame_id, theurl)			{				var fr = document.getElementById(frame_id);				var now = new Date();				now = now.valueOf();								fr.src = theurl + "&now=" + now;			}		</script>	</head>	<body>		<center>			<br><br><br><input type="button" onclick="setInterval('refresh_frame(\'Heal\', frame_url);', 400);" value="Heal">			<br><br><br><IFRAME id="Heal" width=1000 height=400 marginwidth=0 marginheight=0 frameborder=0 scrolling=auto></IFRAME>		</center>		<script type="text/javascript">			document.getElementById("Heal").src = frame_url;		</script>	</body></html>

Link to comment
Share on other sites

Thank you the start works great like that. Doing it like that without naming the function for the setinterval and just starting it is there a way to cear it like this

<html>    <head>        <script type="text/javascript">        var frame_url = "http://api.msappspace.com/proxy/relay.proxy?opensocial_authtype=SIGNED&opensocial_token=o7H9ZTCi1TvHlRTZXxMzWF6DGlOE9a2bFqpVxwrqB9XdNbs3QaY/eps3Nm+3VTXopYM87CjwJ/D3C8SvRWvWMxAylK2n2CyJHLvbucKsyqU=&opensocial_url=http%3A//mob-dynamic-lb1.mobsters02.com/mob/heal%3Fuser_id%3D128559318%26level%3D90%26session_id%3D9abc68f653ee41a4ba1f8ce7146f0f0fda1dee09%26session_key%3D1c33870fc69a4c64a9da701d7c54a5a9a1064371";                function refresh_frame(frame_id, theurl)        {            var fr = document.getElementById(frame_id);            var now = new Date();            now = now.valueOf();                        fr.src = theurl + "&now=" + now;        }        </script>    </head>    <body>        <center>            <br><br><br><input type="button" onclick="setInterval('refresh_frame(\'Heal\', frame_url);', 400);" value="Heal"><input type="button" onclick="clearinterval(refresh_frame)" value="stop">            <br><br><br><IFRAME id="Heal" width=1000 height=400 marginwidth=0 marginheight=0 frameborder=0 scrolling=auto></IFRAME>        </center>        <script type="text/javascript">            document.getElementById("Heal").src = frame_url;        </script>    </body></html>

From some of the stuff i was reading in the tutorials and stuff on it especially the one with the start and stop count that can be restarted and such they have everything named as individual functions. Does that need to be done in order to be able to stop and restart it?

Link to comment
Share on other sites

clearInterval takes as an argument the object returned by setInterval.

var interval = setInterval(foo, 1000);clearInterval(interval);

If you're not going to have more than one interval per page, this relatively simple construct will work.

<input type="button" onclick="window.interval = setInterval('refresh_frame(\'Heal\', frame_url);', 400);" value="Heal"><input type="button" onclick="clearInterval(window.interval);" value="stop">

But otherwise you need some way to associate each interval with some other significant object for the sake of uniqueness. For example, in your SCRIPT tag,

window.intervals = {};

And in your HTML:

<input type="button" onclick="window.intervals.heal = setInterval('refresh_frame(\'Heal\', frame_url);', 400);" value="Heal"><input type="button" onclick="clearInterval(window.intervals.heal);" value="stop">

Link to comment
Share on other sites

There is only going to be One interval on a page so the simple one will work but Im not sure if im looking too much into it or trying to take it too much as is.

<html>    <head>        <script type="text/javascript">        var frame_url = "http://api.msappspace.com/proxy/relay.proxy?opensocial_authtype=SIGNED&opensocial_token=o7H9ZTCi1TvHlRTZXxMzWF6DGlOE9a2bFqpVxwrqB9XdNbs3QaY/eps3Nm+3VTXopYM87CjwJ/D3C8SvRWvWMxAylK2n2CyJHLvbucKsyqU=&opensocial_url=http%3A//mob-dynamic-lb1.mobsters02.com/mob/heal%3Fuser_id%3D128559318%26level%3D90%26session_id%3D9abc68f653ee41a4ba1f8ce7146f0f0fda1dee09%26session_key%3D1c33870fc69a4c64a9da701d7c54a5a9a1064371";        var interval = setInterval(foo, 1000);clearInterval(interval);        function refresh_frame(frame_id, theurl)        {            var fr = document.getElementById(frame_id);            var now = new Date();            now = now.valueOf();                        fr.src = theurl + "&now=" + now;        }        </script>    </head>    <body>        <center>            <br><br><br><input type="button" onclick="window.interval = setInterval('refresh_frame(\'Heal\', frame_url);', 400);" value="Heal"><input type="button" onclick="clearinterval(window.interval);" value="stop">            <br><br><br><IFRAME id="Heal" width=1000 height=400 marginwidth=0 marginheight=0 frameborder=0 scrolling=auto></IFRAME>        </center>        <script type="text/javascript">            document.getElementById("Heal").src = frame_url;        </script>    </body></html>

I tried it as is how you had coded it and the start part still works. But the stop doesnt. So then i wasnt sure if the interval was coded as a generic method so i tried also like

var interval = setInterval('refresh_frame(\'Heal\', frame_url);', 400);clearInterval(interval);

Neither of them makes it stop. So im not sure if im just way over thinking this one or if its just way out of my reach

Link to comment
Share on other sites

clearinterval should be clearInterval. Also, this doesn't belong in your code (unless you have a function/string called "foo"):

var interval = setInterval(foo, 1000);clearInterval(interval);

Plus it doesn't have any real effect unless you separate the two statements, so your modification above is also useless. (The interval is cleared immediately after being set, so it doesn't get the chance to execute.) The HTML I gave above (with the wrong method name) separates the statements so they actually mean something.

Link to comment
Share on other sites

Just wanted to say thank you for the patience. When I sent my last message to you it was already after 1am (im stationed in germany) and ive been sick the last couple days I'm not quite sure why I was trying some of the stuff I did. I tried once just now after waking up and I got it to work perfectly. Once again thank you.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...