views:

235

answers:

2

What is the definition of entire page in the following line?

When the entire page is included via PHP include() or require() function, make sure to put that script code inside the <body> tag and not inside the <head>, because calling jQuery script file from inside the tag doesn't work for some reason.

I have had problems in setting up a jQuery codes. However, I managed to get one to work by putting it inside HEAD and the following code

#1 code

$(document).ready(function(){     
    --- code here---- 
});

I have not managed to get any functions to work. I have had them inside and outside the code #1. The following is an example.

#2 Not working code

    function notEmpty() {                        
        //put this in a function and call it when the user tries to submit
        var tags = document.getElementById('tags').value;
        if(tags == '' || tags == null) {         
            alert('Please enter one or more tags');
            return false;
        }
        return true;
    }

I use PHP to include the tags HTML, HEAD, /HEAD and BODY by the following code in my index.php.

#3 Code by which sources HTML, HEAD and body tags

 include ( './official_content/html_head_body.php' );
+3  A: 

I find that statement suspect. I've never encountered any problem including the <script> tag for jQuery in the <head> of the document, like any other <script> tag. The browser cannot be aware of anything that PHP has done. I suspect it is simply a misstatement.

To address your specific code, what you've written in #1 is entirely correct. Passing a function to $(document).ready() lets you supply code to be run when the document is "ready" (meaning: when the page itself has loaded, without necessarily having loaded external resources like images, stylesheets, et cetera, for which you wouldn't want to have to wait).

Regarding #2, that function is not using jQuery at all; it's using only built-in JavaScript functions. Having said that, it should work correctly by finding a form element with id="tags" and showing an alert box if that element has an empty value. How were you calling this function? In the world of jQuery, you'd do something like this:

$("#id-of-your-form").submit(notEmpty);

You could also rewrite the notEmpty() function to exploit jQuery like so:

function notEmpty() {{
    var tags = $("#tags").val();
    if (tags == '' || tags == null) {
        alert('Please enter one or more tags');
        return false;
    }
    return true;
}

Again, the rest of that function currently connects to jQuery at all. It's fine just the way it is there.

Finally, #3 is unrelated to jQuery, since that's processing happening within PHP, of which JavaScript remains entirely unaware.

VoteyDisciple
i concur. not ever seen this problem and find the 'sentence' questionable
Scott Evernden
+2  A: 

Put the jQuery source in the html_head_body.php file and add your function in your "content" php file.

<html>
<head>
   <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js" type="text/javascript"></script>
</head>
<body>
   <script type="text/javascript">
     $(document).ready(function(){

       function notEmpty() {                        
         //put this in a function and call it when the user tries to submit
         var tags = document.getElementById('tags').value;
         if(tags == '' || tags == null) {         
            alert('Please enter one or more tags');
            return false;
         }
         return true;
       }

     });
   </script>         
</body>
</html>
Fabian
I have now two `$(document).ready(function(){`: one in HEAD and one in BODY. -- **Can this be a problem?**
Masi
Nope, thats absolutely fine.
Fabian