using javascript in a rails view when a link is clicked
I'm a full on newbie at programming. I know what I'd like to do, and believe I have the code I need, but I don't know how to get everything hooked up.
I have this link:
"color:green;" %>
When it is clicked/pressed, I would like this javascript countdown to be invoked: (it brings up a countdown box)
<script type="text/javascript">
var timeout;
function openInfoDialog() {
Dialog.info("Test of info panel, it will close <br>in 3s ...",
{width:250, height:100, showProgress: true});
timeout=3;
setTimeout(infoTimeout, 1000)
}
function infoTimeout() {
timeout--;
if (timeout >0) {
Dialog.setInfoMessage("Test of info panel, it will close <br>in " + timeout + "s ...")
setTimeout(infoTimeout, 1000)
}
else
Dialog.closeInfo()
}
openInfoDialog();
</script>
Then, when that box closes, I'd like the page to refresh. The thing is, I don't know how to call that javascript upon the click, or make a page refresh only one time.
Alternatively, I think I could make things work with this:
<script type="text/JavaScript">
<!--
function timedRefresh(timeoutPeriod) {
setTimeout("location.reload(true);",timeoutPeriod);
}
// -->
</script>
<p>
<a href="javascript:timedRefresh(2000)">Refresh this page in 2 seconds</a> |
<a href="javascript:timedRefresh(5000)">Refresh this page in 5 seconds</a>
</p>
</div>
But he开发者_Go百科re again I don't quite get how to use this javascript with a rails link.
Thanks for the help...
You need to add an event listener to the link. Assuming you're using jquery (if not you should):
<%= link_to 'Start monitoring this room', start_robot_path(@robot), :class => 'some_class %>
$(document).ready(function() {
$('.some_class').click(function(event){
openInfoDialog();
return false;
})
})
#stylesheet.css
a.some_class{
color:green
}
精彩评论