javascript function not working after page load - php

why my code is not working? I have called a javascript function after page load PHP script.like below:
<script>
setTimeout("fb_login()",100);
</script>
fb_login() function is on same page
function fb_login()
{
alert("ok");
}
Tried with setTimeout("fb_login",100); too. but not working.
I have checked console, but it's giving no error.

Just change it to:
<script>
setTimeout(fb_login, 100);
</script>

Change this code:
<script>
setTimeout("fb_login()",100);
</script>
to this:
<script>
setTimeout(fb_login,100);
</script>
Good explanation from similar post - How can I pass a parameter to a setTimeout() callback?

It might be you given less time in setTimeout but and it's calling your function befor page loaded fully. So Try to increase time.
<script>
setTimeout("fb_login()",1000);
</script>

Make sure that fb_login is being initialized before calling otherwise it will give error. Either use document.ready or add that function before it is called. Does it give you some error like "fb_login is undefined" ?

try this
<script>
window.onload = function(){
setTimeout(fb_login,100);
};
function fb_login(){
alert("ok");
}
</script>
EDIT: first check if the below is working or not, if it does not work then pbm is somewhere else
window.onload = function(){
setTimeout(function(){
fb_login();
},100);
};

Related

How to pass php variables to jquery AJAX?

I have started learning jquery AJAX. I have run into a problem, and was wondering if you guys could help me. I am trying to pass a php variable back to jquery, but it displays as [object Object]. I will be posting my code below.
index.html:
<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function() {
$("p").text($.get("return.php"));
});
});
</script>
</head>
<body>
<p>This is a test!</p>
<button>Click Here</button>
</body>
</html>
return.php:
<?php
$message = "Another test!";
echo $message;
?>
So what is it that I need to do to pass php variable $message into the paragraph using jquery ajax?
I know I could simply do if I changed index.html to index.php, but then if $message later changes, I have to reload the page. I am trying to learn how to make dynamic content without having to reload the page.
Thanks ahead of time for any help you provide! :-)
You'll have to wait until the data is returned before you can use it:
$(document).ready(function(){
$("button").click(function() {
$.get("return.php", function(data) {
$("p").text(data);
});
});
});
Add a callback to get.
$.get("return.php", function(data) {
$("p").text(data);
});
You can use callback function in .get function.
$(document).ready(function(){
$("button").click(function() {
$.get("return.php",function(data){
$("p").text(data);
});
});
});
Here you can pass the datatype as well in which form you want the response from server.
Suppose you want to return anyother datatype(i.e. json)from server, just use datatype with it like this :
$(document).ready(function(){
$("button").click(function() {
$.get("return.php",function(data){
$("p").text(data);
},"json");
});
});
For more detail,refer : http://api.jquery.com/jQuery.get/

Add Redirect URL link to JS Function

How can I add redirect URL link to JS function (example form action to : session.php) in below code.
I've tried with another way code, but it still can't function.
$(document).ready(function() {
$("#submit_butt").click(function() {
var conf = {
frequency: 5000,
spread: 5,
duration: 600
};
/* do your AJAX call and processing here...
....
....
*/
// this is the call we make when the AJAX callback function indicates a login failure
$("#login").vibrate(conf);
// let's also display a notification
if($("#errormsg").text() == "")
$("#loginform").append('<p id="errormsg">Invalid username or password!</p>');
// clear the fields to discourage brute forcing :)
$("#password").val("");
document.forms['login_form'].elements['username'].focus();
});
});
You can try this
// similar behavior as an HTTP redirect
window.location.replace("http://stackoverflow.com");
// similar behavior as clicking on a link
window.location.href = "http://stackoverflow.com";
https://developer.mozilla.org/en-US/docs/DOM/window.location
Ref: How to redirect to another webpage in JavaScript/jQuery?
you can use this..
window.location.href = "http://www.google.com";
you can try
<script>
function name(){
window.location ='abc.php';
}
</script>
you can by breaking into php code inside your javascript
$(document).ready(function()
{
<?php
someFunction();
?>
});
but only if your javascript is in a php file so it can be processed by php. So if your linking to a .js file that needs to be changed to .php

Show something before setInterval jquery

my question is how can i show instant some information before load setInterval in jquery, and when is load setInterval will update it.
<script type=\"text/javascript\">
function getData(){
$(\"#whereshow\").load(\"somefile.php\");
}
setInterval(\"getData()\", 5000);
</script>
Can i make something like that?
<script type=\"text/javascript\">
function getData1(){
$(\"#whereshow\").load(\"somefile.php\");
}
function getData(){
$(\"#whereshow\").load(\"somefile.php\");
}
setInterval(\"getData()\", 5000);
</script>
...and getData1() will display instant onload page info, then will stop, and setIntervall wil do the other update job?
Basically what you want to do is have the setInterval fire immediately as well as on the interval.
That's easy.
(function() {
var getData = function() {
$("#whereshow").load("somefile.php");
}
setInterval(getData,5000);
getData();
})();
See, just call the function ;) I also added some improvements to your script.
EDIT: Another way of doing it that I just thought of:
setInterval((function() {
$("#whereshow").load("somefile.php");
return arguments.callee;
})(),5000);

Load javascript function and when its loaded load it again in a specified number of seconds

He guys,
Maybe you can help me, i need a code when the page is loaded a function (below)
function update() {
$('#animation').load('/Stream/readLevel');
}
needs to execute, when this action is complete (so the content of the page /Stream/readLevel has been fully loaded the function must be loaded again in a number of seconds (2 seconds).
How can i accomplish this?
Thanks in advance!
As you only want to reload after the contents has been fully loaded you have to set the timer in success callback of $.load function like following, otherwise it will send request to server every two seconds irrespect of if the previous ajax request has been completed or not.
function update() {
$('#animation').load('/Stream/readLevel',function(){
var timer = setTimeout(update,2000);
});
}
$(function(){
update();
});
you can do it like this.
$(function(){
update();
});
function update() {
$('#animation').load('/Stream/readLevel', function(){
alert("after two seconds of load");
setTimeout('update()', 2000);
});
}
​
<script>
function update() {
$("#animation").load('/Stream/readLevel',"",function(){
setTimeout(update,2000);
});
}
</script>
<body onload="update()">
// HTML CODE
</body>
Try
function update(){
$('#animation').load('/Stream/readLevel',function(){
setTimeout(update,2000); });
}
var c = 2;
function update() {
$('#animation').load('/Stream/readLevel');
}
function startTimer(){
if(c< 0){
c=2;
}
if(c==2){
update();
}
timer = setTimeout('startTimer()',1000);
c=c-1;
}
$(document).ready(function(){
startTimer();
});

Why is my PHP code not working?

I have made a code --
<?php
header("Content-type: text/javascript");
require_once('../php/config.php');
$domail_theme = $_GET['theme'];
?>
<?php
if($domail_theme=='default')
{
?>
$('div.theme_content').mouseover(function () {
$(this).stop().animate({
backgroundColor : '#234'
}, 250, 'linear', function() { });
});
$('div.').mouseout(function () {
$(this).animate({
backgroundColor : '#ddd'
}, 400, 'linear', function() { });
});
<?php
}
?>
And here is the script including tag --
<script src="domail_res/scripts/java/settings_form_css.php?theme=default"></script>
The thing I want is that when I mouse over on the div element with class theme_content it's background changes according to the given one in the animate function. It is working for the input element but that script is original javascript and in this I have included php that's why I'm thinking whether the php code I used is wrong or my javascript. Also when I include this code in javascript file it works correctly. And by the I'm including the script tag in between the body tag, is it wrong? Please help me out with this one.
Thanks in advance!
Try to put it inside script. <script></script>
And also it's possible that DOM might not have been loaded. so try putting wrappint in $(document).ready(function() { /*Your code here*/})
And also put alert('hello'); to check if that code is being executed or not. It might be useful in debugging.
The most likly explanation is that 'theme" is not set to 'default' in your URL request.
This should be easy to check. Also you can "View" -> "Page Source" in your browser and look at the html your php program is actually generating. If my guess is right you won't see your mouseover function in the page.
Hi you should write like this:-
<?php
if($domail_theme=='default')
{
?>
<script>
$('div.theme_content').mouseover(function () {
$(this).stop().animate({
backgroundColor : '#234'
}, 250, 'linear', function() { });
});
$('div.').mouseout(function () {
$(this).animate({
backgroundColor : '#ddd'
}, 400, 'linear', function() { });
});
</script>
<?php
}
?>
Just need add tag between on your php if statement.
Hope can help you.
Does it work better if you add type='text/javascript' to your script tag?
<script type='text/javascript' src="domail_res/scripts/java/settings_form_css.php?theme=default"></script>
Why not just put the php logic in your main file and include the javascript as plain javascript?
<?php
if($domail_theme=='default')
{
?>
<script type='text/javascript' src="domail_res/scripts/java/settings_form_css.js"></script>
<?php
}
?>
Seems like overcomplicating things

Categories