height and width method doesn't work when jquery library located in head section??
Hello guy!
I'm new in jquery and i have a small test with height and width method in jquery
I use the seperate file to execute my code to get height and width of element. but when i call it in body and jquery library in head section. So height and width method doesn't work at all. I don't know why but when i locate jquery library in body section (the same place with my file) . It worked fine!! Below is my code
Html:
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
- </head>
- <body>
- <div class="container">
- <img src="day15/img4.jpg" alt="Alternate Text" class="img" />
- </div>
- <script src="this_is_my_seperate_file.js" type="text/javascript"></script>
- <script type="text/javascript">
- (function () {
- //test
- var test = new Getwidthheightimg($('.img'));
- test.show_result();
- })();
- </script>
- </body>
- </html>
And this is "
this_is_my_seperate_file.js" file
- function Getwidthheightimg(img) {
- this.img_width = img[0].width; //get width of image
- this.img_height = img[0].height; //get height of image
- }
- Getwidthheightimg.prototype.show_result = function () {
- console.log(this.img_width);
- console.log(this.img_height);
- };
So that's it but it doesn't work untill i put jquery library above my file(.js) like this in the body section:
- <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
- <script src="this_is_my_seperate_file.js" type="text/javascript"></script>
Hope you help me got it clearly! Thanks.