Keypress Event - Javascript/Jquery [closed] - javascript

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I want to start a timer at first keypress (on the ) and it should count down till 0. The catch is that once it is started it should not stop until it has reached 0 nor it should be affected by subsequent key presses.
Thank you! :)

<html>
<head>
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>
<body>
<h1 align="center"></h1>
<script>
var time=10;
var timer;
$('body').keypress(function() {
timer=setInterval(function(){
if(time<=0)
clearInterval(timer);
$('h1').html(time);
time--;
},1000)
$(this).unbind('keypress');
});
</script>
</body>
</html>

Related

how to make an alert pop up before my script [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I'm new to js and I was working on something where this thing makes a bunch of new tabs, but when the code is run, the alert pops up when each tab is opened and on each individual tab.
here's the code
<script>
alert('something')
setInterval(function() {
var w = window.open();
w.document.write(document.documentElement.outerHTML||document.document
Element.innerHTML);
}, );
</script>
pls be nice I'm new
What about this one
<!DOCTYPE html>
<html>
<body>
<p>Click the button to open a new browser window.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
alert("Hi there");
window.open("https://www.w3schools.com");
}
</script>
</body>
</html>

javascript: How would you make text spin using javascript? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have this code:
<img src="http://www.w3schools.com/html/html5.gif" id="imgid">
<h1 id='header'>Example</h1>
<!--This is an example html doc not the real thing!-->
<button onclick="spin(document.getElementById('header'));spin(document.getElementById('imgid'));">Spin!</button>
<script>
function spin(object) {
setInterval(function() {
object.style.transform += "rotate(10deg)";
}, 1);
}
</script>
How do I make the text spin correctly? If you could explain how it works then that would be great. If you need any references of my sources then:
SetInterval
style.transform
By "correctly" I mean spinning centered. (Not all over the page).
Thanks!
Try this. The main problem was that the width extended all the way across the page which messed up the rotation, so adding in display:inline-block made the width match up with the div's contents.
<style>
#imgid{display:inline-block;}
#myDIV{display:inline-block;}
</style>
<html>
<head>
<title>Example</title>
</head>
<body>
<div><img src="http://www.w3schools.com/html/html5.gif" id="imgid"></div>
<div><h1 id='myDIV'>Example</h1></div>
<div><button onclick="spin(document.getElementById('imgid'));spin(document.getElementById('myDIV'));">xdfdsf</button></div>
</body>
</html>
<script>
function spin(object)
{
setInterval(function()
{
object.style.transform += "rotate(10deg)";
}, 1);
}
</script>

redirect to appstore or google play [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I will send my clients the link for the app in the following format
http://goo.gl.com/downloadtheapp (or whatever)
I want this file be somewhere on my server that includes java script code that checks what is the device type and redirects to the convenient store. that is, google play if the device was android based and appstore if the device was ios based.
till now I tried this, but it does not work.
<html>
<head>
<script type="text/javascript">
$(document).ready(function ()
{
if(navigator.userAgent.toLowerCase().indexOf("android") > -1)
{
window.location.href = 'http://play.google.com/store/apps/details?id=com.truecaller&hl=en';
}
if(navigator.userAgent.toLowerCase().indexOf("iphone") > -1)
{
window.location.href = 'http://itunes.apple.com/lb/app/truecaller-caller-id-number/id448142450?mt=8';
}
}
</javascript>
</head>
<body>
</body>
</html>
The problem is with your script tag and you are missing );
And by the way, did you import jQuery?
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script>
$(document).ready(function (){
if(navigator.userAgent.toLowerCase().indexOf("android") > -1){
window.location.href = 'http://play.google.com/store/apps/details?id=com.truecaller&hl=en';
}
if(navigator.userAgent.toLowerCase().indexOf("iphone") > -1){
window.location.href = 'http://itunes.apple.com/lb/app/truecaller-caller-id-number/id448142450?mt=8';
}
});
</script>

Unable to access html elements by tag using Java script [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
My html is like this:
<!DOCTYPE html>
<html>
<head>
<title>
Event Handling
</title>
</head>
<body>
<h1> This is the chapter in which I am handling Events.The site you should visit is: </h1>
<b>here<b>
<script src="events.js"></src>
</body>
</html>
The javascript is like this:
var link = document.getElementsByTagName("a")[0];
link.onclick = MyEventHandler;
function MyEventHandler()
{
alert("ouch!");
return false;
}
It is not working, but using inling event handler I am doing it successfully. Please helop me.
You need to assign the event handler function, not the return value of calling that function.
link.onclick = MyEventHandler;
(Also note that you need to use the end tag </script> not </src>.)

Error about dynamic countdown redirect [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
I have an javascript to redirect to another page after countdown 10 seconds, but it doesn't work. Can anyone help to fix the problem?
<body>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type ="text/javascript" src ="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var number = 10;
var url = 'http://localhost/rms/index.php/';
function countdown() {
setTimeout(countdown,1000);
$('#title1').html("Redirecting in " + number + " seconds");
number --;
if(number<0) {
window.location = url;
number = 0;
}
}
countdown();
});
</script>
</head>
<body>
<div id="title1"></div>
</body>
As everybody wrote in the comments, it should work. Try to replace
<script type ="text/javascript" src ="jquery.js"></script>
with this:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js"></script>
In this case you don't have to worry about your jquery.js file, which could be missing or corrupted.
I have one note to your code. Are you aware of that you call window.location = url; two times?
If its just a regular countdown with redirect I will put the setTimeout call in if statement which will prevent calling it twice.

Categories