-
Apr 30th, 2013, 02:56 AM
#1
Thread Starter
Fanatic Member
alert is not appearing
hello to every one
I have following code and it simple example of timer
Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Paper.aspx.cs" Inherits="JQueryAjax.Paper" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript">
function myFunction()
{
setTimeout(function(){alert("Hello")},3000);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<p>Click the button to wait 3 seconds, then alert "Hello".</p>
<button onclick="myFunction()">Try it</button>
</div>
</form>
</body>
</html>
and it should appear alert after 3 seconds but this does not happening .. any help
There is no achievement without goals
-
Apr 30th, 2013, 07:41 AM
#2
Re: alert is not appearing
I got it to work.... by removing the form tag... which is what I expected... the presence of a form tag cause the browser to do a form send, either by GET (default) or POST ... so what's happening is that you're GETting the form, causing it to reload/refresh... so the timer never gets a chance to run.
-tg
-
Apr 30th, 2013, 08:12 AM
#3
Re: alert is not appearing
Originally Posted by techgnome
I got it to work.... by removing the form tag... which is what I expected... the presence of a form tag cause the browser to do a form send, either by GET (default) or POST ... so what's happening is that you're GETting the form, causing it to reload/refresh... so the timer never gets a chance to run.
-tg
Correct. If you want to use a button in a form tag, use <input type="button", not the button element. As using the button element can submit a form: http://www.w3schools.com/tags/tag_button.asp
-
May 1st, 2013, 03:16 PM
#4
Re: alert is not appearing
You can also just do <button type="button">click me</button> to prevent form submission with a button tag.
https://developer.mozilla.org/en-US/...Element/button
-
May 1st, 2013, 04:09 PM
#5
Re: alert is not appearing
I am wondering - can you also do it by having a RETURN FALSE somewhere in that function to stop the event from propagating? Just curious...
-
May 1st, 2013, 08:04 PM
#6
Re: alert is not appearing
if the myFunction returns False... then you would have to call it like this:
<button onclick="return myFunction();">Try it</button>
you could also do this (and not modify the myFunction):
<button onclick="myFunction(); return false;">Try it</button>
I've done that before... but it would also depend on whether the form was just a container for the button (and if that's the case it should be removed to be semantically right)
-tg
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|