0

I am newer to Javascript and HTML. I want to check whether an image is loaded or not. I found this code on StackOverflow,but I don't know how to combine them into a finished piece: Any one can tell me?

The js code:

<script language="JavaScript">
    $("img").one("load", function() {
      // do stuff
    }).each(function() {
      if(this.complete) $(this).load();
    });
    </script>;

The html code:

<div>
  <table width="100%" height="100%" align="center" valign="middle"> 
    <tr>
       <td bgcolor="258cca"><td align="center" valign="middle">
         <p>
            <img src="http://www.freeiconspng.com/uploads/cute-ball-stop-icon-png-1.png">
         </p>
         <p class="STYLE1 STYLE2"></p>
      </td>
    </tr>
    <tr></tr>
  </table>
  <BODY ondragstart="return false;" ondrop="return false;">
 </div>

The CSS:

body {
    background-color: #558CCA;
}
.STYLE1 {
    color: #FFFFFF;font-family: Arial, Helvetica, sans-serif;font-size: 30px;
}
.STYLE2 {
    font-size: 21px;
}  
1
  • i suggest running through a few online tutorials, or maybe signing up with a learning site like teamtreehouse.com Commented Sep 14, 2016 at 13:26

5 Answers 5

1
<html>
    <head>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
        <style type="text/css">
            body {background-color: #558CCA;}
            .STYLE1 {color: #FFFFFF;font-family: Arial, Helvetica, sans-serif;font-size: 30px;}
            .STYLE2 {font-size: 21px}    
        </style>

        <title></title>
        <script type="text/javascript">
            window.onload=function(){
                $("img").on("load", function() {
                }).each(function() {
                if(this.complete) $(this).load();
                });
            }
        </script>
    </head>

    <body>
        <div>
            <table width="100%" height="100%" align="center" valign="middle"> 
                <tbody>
                    <tr>
                        <td bgcolor="258cca"></td><td align="center" valign="middle">
                            <p>
                            <img src="http://www.freeiconspng.com/uploads/cute-ball-stop-icon-png-1.png">
                            </p>
                            <p class="STYLE1 STYLE2"></p>
                        </td>
                    </tr>
                    <tr>
                    </tr>
                </tbody>
            </table>
        </div>
    </body>
</html>

Fiddle

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks everyone,thanks for kindly help,I think this answer is the best one for a newer like me. :)
1

The code you found is using jquery library so in order for it to work, you must first GET the jquery plugin in your page, just put this code anywhere in your document. For faster loading make it at the bottom of your page. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> Secondly, put your jquery code UNDER this link, otherwise it won't work.

Comments

0

This is a basic starter html template

<html>
    <head>
        <script language="Javascript">
            Function here
        </script>
    </head>

    <body>
        Html code here
    </body>
</html>

Comments

0

Put the <script>...</script> block to the end of your page, before the </body> tag. If you have your scripts at the top of the page, it can cause mistakes sometimes.

Comments

0

A few errors in your code :

  • There should be no semicolon after the script tag
  • jquery has "on" method, its misspelled "one" here

When in one file the basic html page syntax goes as:

<!DOCTYPE html>
<html>
<title>Web Page Design</title>
<head>
<script  src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<style type="text/css">
 /* css styles go here*/
 body {
    background-color: #558CCA;
 }
.STYLE1 {
    color: #FFFFFF;font-family: Arial, Helvetica, sans-serif;font-size: 30px;
 }
.STYLE2 {
    font-size: 21px;
}
</style>
<script type="text/javascript">
    // javascript/jquery code goes here
     $("img").on("load", function() {
      // do stuff
    }).each(function() {
      if(this.complete) $(this).load();
    });
</script>
</head>
<BODY ondragstart="return false;" ondrop="return false;">
<div>
  <table width="100%" height="100%" align="center" valign="middle"> 
    <tr>
       <td bgcolor="258cca"><td align="center" valign="middle">
         <p>
            <img src="http://findicons.com/files/icons/1072/face_avatars/300/a05.png">
         </p>
         <p class="STYLE1 STYLE2"></p>
      </td>
    </tr>
    <tr></tr>
  </table>
 </div>
 </body>
</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.