Can't find what's wrong in this code - php

I have the following script which brings up a dialog on my screen telling me there has been an error:
<script type="text/javascript">
var myFunc = function()
{
var html='<div class="cs-body">explanation of the error</div>';
$(html).csDialog();
return false;
};
</script>
I want this dialog to pop-up when my php script returns a "1" value for $error, like this:
if ($error==1) {
echo "<script type='javascript'>$(document).ready(myFunc)</script>";
}
Even if I leave the if-clause and just echo the script it doensn't do anything. Can somebody tell me what I'm missing here?
Thanks in advance!

try changing script type from 'javascript' to 'text/javascript'
that did the trick! thanks lostsource!

You need to make sure, this script fragment is output after the inclusion of jQuery.

Check if you javascript function is defined when running this
echo "<script type='javascript'>$(document).ready(myFunc)</script>
Include jQuery and your js function in the tag, just to see if it works.
PS: you should only add js script in the tag if they are necessary right away, else, added them at the bottom of the tag. Remember, the loading of js files blocks the loading of any other page resources

try this
<?php if ($error==1) { ?>
<script type="text/javascript">
$(document).ready(myFunc)
</script>";
<?php } ?>
Dins

Related

how to run script onbeforeunload

hi somebody pls explain to me why this code of mine doesn't work
$(document).ready(function()
{wireUpEvents();
});
function wireUpEvents()
{window.onbeforeunload = function()
{
window.location="link"; // on before unload i will run this link first.
}
}
when i refresh my page this code will run but it'll not go to link(window.location) before load however when i add alert like this
window.location="link"; alert("anything");
it'll go to my link before load. I like to eliminate that weird alert().
pls feel free to suggest any treatment with my code. thanks
try below code
window.onbeforeunload=testfunc;
function testfunc()
{
window.location="link";
}
Try this :
<script type="text/javascript">
$(window).bind('beforeunload', function() {
window.location="link";
}
);
</script>

jQuery, AJAX wont show container in DIV

I have this jQuery script in my web page:
<script>
$(function() {
function callAjax(){
$('#lastlogins').load("ur.php");
}
setInterval(callAjax, 1000 );
});
</script>
But, in container lastlogins, there isn't shown file ur.php, it is just empty. I have created div like that:
<div id="lastlogins">
</div>
I don't see any wrong on your code. Only problem would be with your ur.php file. Just try to access your file directly and see, if that works.
In the meanwhile, refer my sample code.
<script>
$(function() {
function callAjax(){
$('#lastlogins').load("/SivaCharan/EGxWL/show");
}
setInterval(callAjax, 1000 );
});
</script>
<div id="lastlogins">
</div>
Refer my LIVE DEMO
After a quick review of the .load function, there can only be one possibility... your PHP file is not returning anything useful. Run it directly in a browser and make sure it returns valid HTML.
Your jQuery function is correct as is.

Calling Javascript based on PHP conditional when libraries load in the footer

I have the following in the body of a php page:
<?php if($foo) : ?>
<script>
js_func();
</script>
<?php else: ?>
//Do Something else
<?php endif; ?>
Based on the PHP conditional I either do or do not want to run js_func().
However if I am loading all of my scripts (including the script the defines js_func()) at the bottom of my page this will results in an error.
One possible solution would be to load the external script BEFORE calling js_func() but I understand that for performance reasons I shouldn't do that.
I could use $(document).ready(function() {}); but this just moves the error as jQuery is also loaded in the footer.
The only other options I can think of is to use window.onload or never call a js function inline. How does everyone else solve this issue?
Many thanks.
EDIT:
#Nile - Im not sure what you mean. Why would I comment out code that I want to execute?
#haynar1658 - I don't want to execute JS in the else scenario.
#Matthew Blancarte - Understood. That leads to my question, what's the best way to make sure that the js I need loads before that function is instantiated? Include the script before it? Use window.onload? etc.
I think you are making a rod for your own back. Depend on the question you described, you want to put all the function definition script block after the place where them being called. It's Impossible!
If you indeed need to do this, does this can help? :
<script>
var fns = []; /* use fns to keep all the js code
which call the functions defined after. */
</script>
<script>
//wrapp your code in a function and then push it into fns.
fns.push(function(){
js_func();
})
</script>
//script tags for loading your function definition js script.
<script src="path/to/jquery-any-version.js"></script>
<script src="path/to/other-libraries.js"></script>
<script>
//after your definition js scripts are loaded , call all functions in fns
for(var i=0, len=fns.length; i<len; i++){
var fn = fns[i];
fn.apply(this, []/* arguments that provided as an array */);
}
</script>
Just move the script to the top.
The difference (if there is one) is very small.
The believe that putting the <script>s in the <head> slows down the page is not "accepted" by all developers.
Did you try to echo it in PHP?
<?php if($foo) {
echo "<script> js_func(); </script>";
}else{
echo "something else";
}

PHP: Invoking Javascript Command

I'm using a plugin called Reveal, and trying to fire it based on whether a variable is present in the URL (i.e. www.mysite.com?status=new). I have tested the GET status code (works fine, echoed it) and also set up reveal to work when a button is pressed. Both work flawlessly. However, the goal is to fire the event if the status == new. This is my code:
<?
if($status == 'new'){
echo '<script type="text/javascript">$(\'#myModal\').reveal();
</script>';
}
?>
Doesn't work :/ Any help? I've tried many combinations with this and nothing seems to be working. My code is placed near the bottom of the page (in the body section), not sure if that matters..
EDIT: Also tried this (didn't work):
<?
if($status == 'new'){
echo '<script type="text/javascript">
$(document).ready(function() {
e.preventDefault();
$(\'#myModal\').reveal();
});
</script>';
}
?>
Zach
Try this:
echo '
<script type="text/javascript">
$(document).ready(function(){
$(\'#myModal\').reveal();
});
</script>';
as m90 already suggested, probably a good idea to load up the DOM before you execute the script. And you can place it anywhere, usually I place it in the head though, but it's all good.
Be aware that this kind of inline JS will be executed immediately by the browser, so it might be that the element you are trying to reveal isn't ready to be revealed yet (therefore nothing happens). Usually you will fix this by calling your scripts on $(document).ready(). See this article on the jQuery website
just wanted to put here a little bit different approach:
// Example URL:
// test.php?status=Message
var $_GET = <?php echo json_encode( $_GET ); ?>;
// alert( $_GET['status'] );
// console.log($_GET);

Automatic href Link click

Hai
i want to generated an automated click event. I am working in php server, i Know Javascript.
Below is my Code
<script language="javascript">
function autoClick() {
var elm=document.getElementById('thisLink');
elm.click();
document.getElementById('thisLink').click();
}
</script>
</head>
i put this on inside the body tag :
onload="setTimeout('autoClick();',3000);"
and inside the a tag :
href="./apage.php" id="thisLink" name="thisLink" target="newWindow"
But it doesn't work in MOzilla Is any solution , 0r any other solution ???
Thanks in advance
You could try JQuery's trigger function.
$('#thisLink').trigger('click');
This should possibly work although I haven't tested it.
JQuery: http://jquery.com
doc: http://docs.jquery.com/Events/trigger#eventdata
Element.click works only on input elements on Mozilla. Try something like
function autoClick() {
var elm=document.getElementById('thisLink');
document.location.href = elm.href;
}
instead, or if you prefer opening the link into a new window,
function autoClick() {
var elm=document.getElementById('thisLink');
window.open(elm.href, 'autoclickwindow');
}

Categories