Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have jquery file called jq1
iput this is code inside the file
JavaScript
$(document).ready(function () {
            $('#album-list').click(function () { alert("test"); });
            $("#target").click(function () {alert("Handler for .click() called.");});
        });
then in my aspx page
i put reference
HTML
<script src="Scripts/jquery-1.8.2.js"></script>
<script src="Scripts/jq1.js"></script>

<body>
    <img id="album-list" src="Images/orderedList3.png" />
    <div id="target">
        Click here
    </div>
    
</body>

when i click on the image or div i didn't get the alert message
can anybody tell me what is the problem
Posted
Updated 3-Jun-15 1:50am
v2
Comments
F-ES Sitecore 3-Jun-15 8:06am    
Your script tags should go inside the head element. Not sure if that'll make a difference or if you are paraphrasing your code. Use the browser's dev tools (f12) to ensure the scripts are actually loaded and that there aren't any javascript errors on the console. Also use javascript debugging to see if your ready event is even called.
What errors you see on browser console?
md_refay 3-Jun-15 10:15am    
when i click the image or div i didn't get the alert message
so i need where the error
Peter Leow 3-Jun-15 9:56am    
What the name of the external jquery file, is it 'jq1.js' or 'jq1'?
md_refay 3-Jun-15 10:16am    
jq1

1 solution

Essentially, there is no "internal" or "external" JavaScript code.

Your HTML code including "jquery-1.8.2.js makes your whole HTML invalid. It really belongs to your <head> element, not so sure about "jq1.js". JavaScript code in all files is processed in the order it is included. If some file is processed later than some other file, all the declarations of the previously included in the previous file are taken into account.

It all happens as if all JavaScipt files were merged in one big file, with only on difference: the position of HTML code in this order. If a file is included in the <head> element, the code cannot assume existing of any DOM elements, they don't exist at the moment of processing of this file. But the jQuery and jQuery-UI files are abstracted from concrete DOM, of course, so put them there. And the HTML document is fully complete only when the document.ready event is invoked; this is what you actually do. See also: http://learn.jquery.com/about-jquery/how-jquery-works.

Also note that there is no such concept as "call a file" ("jq1" in your case). You only call the JavaScript object of the 'function' type. All objects, including function objects, can be dynamically created from code, and it can happen later. The function object which can be statically created can be called in the fragment of code before function declaration. The object created dynamically (say, those having a member of the function type) should be created before the actual call.

Taking this into account should dismiss your concerns about "external" code.

Now, the solution. Try this:
HTML
<html>

<head>
   <title>Use correct HTML structure</title>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
   <script src="Scripts/jquery-1.8.2.js"></script>
   <script src="Scripts/jq1.js"></script> <!-- if it is a good script! -->
</head>

<body>

    <script>
	   $(document).ready(function () {
              $("#album-list").click(function () { alert("test"); });
              $("#target").click(function () {
                 alert("Handler for .click() called.");
              });
       });
    </script>

    <img id="album-list" src="Images/orderedList3.png" />
    <div id="target">
        Click here
    </div>   

</body>

</html>

Sorry, I did not test the operation, just restructured HTML. It looks correct to me. Please try.

—SA
 
Share this answer
 
v4
Comments
Richard Deeming 3-Jun-15 12:57pm    
Putting script tags in the <body> is fairly common, and doesn't make the HTML invalid in any way. The recommendation used to be[^] to put your scripts at the bottom, just before the closing </body> tag, so that they didn't block the page from rendering.

If you're going to put the script tags in the <head>, you should add either the async or defer attributes. This will ensure that the page isn't blocked waiting for the scripts to download.

There's a good explanation on SO[^].
Sergey Alexandrovich Kryukov 3-Jun-15 13:16pm    
Yes, of course it would be valid.

You did not get the point. Look at the inquire's code sample again. You will see two script tags BEFORE body and NOT IN head. This is the violation of HTML structure. Take a look and you will see it.

Also, take a second look at my answer and you will see that I confirmed the usage of script inside body.

Can you see my point now?

—SA
Richard Deeming 3-Jun-15 13:19pm    
Oh, I was assuming the code in the question was a stripped-down sample. If that's the entire HTML output, then there are quite a few problems with it! :)
Sergey Alexandrovich Kryukov 3-Jun-15 13:30pm    
I would understand it if the problem was different. Anyway, what I wrote is the solution, not counting that the role of jq1 is unknown... :-)
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900