简体   繁体   中英

Can't find what's wrong in this code

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!

您需要确保在包含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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM