jQuery declare function and call - javascript

<script>
function showAlert(){
alert('Bazinga')
}
$(document).ready(function(){
showAlert();
});
</script>
In my example I first of all declaring function and else calling this function when documents is loaded. But alert is now show.

Make sure you have included jQuery library , The script should be after the jQuery library.
<script src="http://code.jquery.com/jquery-1.10.2.min.js" type="text/javascript"></script>
<script>
function showAlert(){
alert('Bazinga');
}
$(document).ready(function(){
showAlert();
});
</script>
Fiddle

I think you forgot the ";" on alert, try adding ";". If that does not do the trick you could try move up ur document ready.

Related

Uncaught TypeError: undefined is not a function $

I'm using a asp.net site.
I'm including this script in the header.
<script type="text/javascript">
$('#settings, #agency_text').on('click', function () {
$('#logout').fadeToggle('fast');
});
</script>
then I get this error:
Uncaught TypeError: undefined is not a function
The error means that the jQuery library wasn't loaded (correctly).
First you need to load in the jQuery library to be able to perform jQuery functions.
Add this line above your script in html
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
For more info check out this page here: http://www.w3schools.com/jquery/jquery_get_started.asp
add $(document).ready(function(){ });
<script type="text/javascript">
$(document).ready(function(){
$('#settings, #agency_text').on('click', function () {
$('#logout').fadeToggle('fast');
});
});
</script>
I also change my code like this
<script type="text/javascript">
$(document).ready(function(){
$('#settings, #agency').click(function () {
$('#logout').slideToggle('fast');
});
});
</script>
It works with "jquery-1.4.1.min.js" :)

Conflicts with jquery scripts

I'm a new JQuery programmer, here is my head code:
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
function myfunction(){
$("button").click(function(){
$("p").hide();
};
});
$(document).ready(myfunction);
</script>
this script works perfectly, but when i add these other three scripts in the page:
<script type="text/javascript" src="/file/js/prototype.js"></script>
<script type="text/javascript" src="/file/js/scriptaculous.js?load=effects,builder"></script>
<script type="text/javascript" src="/file/lightbox/js/lightbox.js"></script>
che console error gave me:
Uncaught TypeError: undefined is not a function
on '$(document).ready(myfunction);' line.
what is the error? Is a conflict problem?
jQuery defines a function called $ that you are trying to use.
prototype.js also defines a function called $ and overwrites the one from jQuery.
Use jQuery to refer to the jQuery function instead of $.
Prototype.js also uses the global $ variable. You need to make sure they don't conflict with each other by using the jQuery variable instead:
// wrap it up
(function($) {
function myfunction() {
$("button").click(function(){
$("p").hide();
});
};
$(document).ready(myfunction);
})(jQuery);
You can also just replace all jQuery $ uses with jQuery, but the method above is generally simpler to implement and maintain.

Append and call dynamic javascript function with jQuery

Can you create a dynamic javascript function and immediately call it?
I already found that you can't use the callback from append.
proposed, non working, code:
<script type="text/javascript">
var count=1;
$(document).ready(function(){
$(".main").append('
<script type="text/javascript">function init'+count+'(){alert("'+count+'");}<'+'/script>
');
window['init'+count]();
});
</script>
<div class="main"></div>
Edit:
Narrowed it down to a synchronization problem. Placing alert("") between append and window makes it work, but that is not really a useful fix because the count might go up to a 100 when I place this code in a loop.
Guess I'll have to keep looking.
That should work, but you aren't quoting correctly. There shouldn't be any unescaped double quotes within append(" and "). Try:
$(".main").append("
<script type=\"text/javascript\">function init'+count+'(){alert(\"'+count+'\");}<'+'/script>"
);
use an external script and $.getScript(), it allows you to specify a callback and makes your code a bit neater
$.getScript("http://scriptURL.com/script.js", function(){ itit(); });
Theres no point of inlining the code.. you could just run it in the same place you're appending the tags or use jQuery.globalEval();
Yes, you can! This is the way javascript was invented! It is extremely powerful:
<script type="text/javascript">
var count=1;
var init = [];
$(document).ready(function(){
(function (count) {
init[count] = function () {
alert(count);
}
})(count);
init[count]();
});
</script>
<div class="main"></div>
or, if you just want to construct and call the function without storing it ...
<script type="text/javascript">
var count=1;
$(document).ready(function(){
(function () {
alert(count);
})();
});
</script>
<div class="main"></div>

Just trying to show and alert when i click on a link

i'm trying this code:
<script type="text/javascript">
$('#ooo').click( function() {
alert("jadner");
});
<script>
prueba
I expected it shows the alert when i click on "prueba", but it doesn't show anything..
Regards
Javi
You need to bind the event only after the DOM finishes loading. Wrap your code inside $(document).load() event like this:
<script type="text/javascript">
$(document).ready(function() {
$('#ooo').click(function() {
alert("jadner");
});
});
</script>
1 - Make sure jQuery has been included.
2 - Make sure your click handler is in a $(document).ready(function() {...}); block.
See:
http://docs.jquery.com/Tutorials:Introducing_$(document).ready()
You have to either reverse the order of the tags, i.e. put the script after the link:
prueba
<script type="text/javascript">...</script>
or wrap the code in a $(document).ready() callback:
$(document).ready(function(){
$('#ooo').click( function() {
alert("jadner");
});
});
The problem: When your JS is executed, it tries to find the element with ID ooo which does not exist yet. Parsing is always from top to bottom. So the script gets executed before the link element is created.
Add return false; below the alert. and like Nanne says, make sure you've got jQuery loaded properly
should be </script> not <script>
use document.ready()
<script type="text/javascript">
$(document).ready(function(){
$('#ooo').click( function() {
alert("jadner");
});
});
</script>
prueba
Try the following code.
<script src="http://code.jquery.com/jquery-1.4.4.js"></script>
<script language="javascript">
$(function()
{
$('#ooo').click( function()
{
alert("jadner");
});
});
</script>
prueba

javascript loading problem

When i am trying to write the code like
document.getElementById('id1') after teh script tag it is showing document.getElementById(..) null or not an object..
Is it necessary to write document.getElementById('id1') in function only.
If i write this code in function then it is accepting. So what the mistake here..
and if i want to execute a function on loading of the page where to write onLoad() function.. i try to write at but it is not loading.. please help me
Thank you
In order to be sure that your dom element is loaded, you have to wait the document is loaded.
To do this you can do:
<head>
<script type="text/javascript">
function foo(){
var elem = document.getElementById("yourElem");
//...
}
</script>
</head>
<body onload="foo()">...</body>
or
<head>
<script type="text/javascript">
function foo(){
var elem = document.getElementById("yourElem");
//...
}
window.onload = foo;
</script>
</head>
<body>...</body>
If you want the script to run after the page is loaded, you can use window.onload.
<script>
window.onload = function () {
//code goes here
}
.
.
.
</script>
Put your script bellow the element you are getting will also work.
<div id="ele"></div>
<script language="javascript">
alert(document.getElementById('ele').tagName);
</script>
<div id="ele1"></div>
But unless you have special purpose, it's a good habit to write handlers in after document loaded, that is, put your code in window.onload event handler.

Categories