0

Possible Duplicate:
What does a script-Tag with src AND content mean?

I used the following in my html page

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript" src="resources/scripts/jquery-1.8.2.min.js">

$(document).ready(function(){
  alert("ABC");
});
</script>
</head>

<body>
<p>THIS IS MY INDEX PAGE.</p>
</body>
</html>

The jquery doesn't work here meaning I don't see any effect of it after putting an alert in it.

But when I put in seperate <script> tag like the one below it works,

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript" src="resources/scripts/jquery-1.8.2.min.js">
</script>
<script>
$(document).ready(function(){
  alert("ABC");
});
</script>
</head>

<body>
<p>If you click on me, I will disappear.</p>
</body>
</html>

so if anyone any explain?

Community
  • 1
  • 1
Abubakkar
  • 15,488
  • 8
  • 55
  • 83
  • 2
    http://stackoverflow.com/questions/6528325/what-does-a-script-tag-with-src-and-content-mean – Sylvain Oct 22 '12 at 17:23
  • thanks for all the answers and comments. Even google would not have been able to respond in such a short time with so many accurate answers. – Abubakkar Oct 22 '12 at 17:30

4 Answers4

6

From w3schools:

http://www.w3schools.com/tags/tag_script.asp

"Note: If the "src" attribute is present, the element must be empty."

When you use script tag you can provide the source code using src attribute or inside the tag, but not both of them.

sanzante
  • 844
  • 1
  • 11
  • 28
  • 4
    https://developer.mozilla.org/en-US/docs/HTML/Element/script w3schools is wrong in this case. "must be empty" is false, it can contain content, however it will not be executed. – Kevin B Oct 22 '12 at 17:30
  • 1
    careful about citing w3schools. They're not known for their accuracy. In this case the element doesn't "have to be empty" the content within the script is simply ignored. Mozilla docs are a much more complete and accurate resource. – Ben McCormick Oct 22 '12 at 17:33
  • I'll remember this, I use this because it was the first result on my search for a source (they seems to be good positioned). – sanzante Oct 22 '12 at 17:38
3

This is because, you are providing the src tag with the javascript.

The src file contents will get expanded as content of <script> (start and end) tag.

You can inspect this with firebug if you want.

Abubakkar
  • 15,488
  • 8
  • 55
  • 83
maximus ツ
  • 7,949
  • 3
  • 25
  • 54
2

What your first tag is doing is telling the browser that the content of the script is located in an external file. It will ignore any scripting in the actual tag in favour of the contents of the file.

You need to use the separate tag.

Xyon
  • 901
  • 9
  • 18
0

I don't know . But if the source you assigned in "src" inside the tag is a online one.

If you save go to that source and copy the script and save it in your local. Then

assign the "src" of your local one then it will work. ie.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript" src="../polin/jquery_library.js">
function fn()
{alert("pp");}
</script>
<script type="text/javascript">
$(document).ready(function(){
alert("ABC");
});
function fn()
{alert("oo");}
</script>
</script>
 </head>

<body onload="fn()">
<p>THIS IS MY INDEX PAGE.</p>
</body>
</html>

src="../polin/jquery_library.js" this file is in my local where the jquery source script is saved. And it work

polin
  • 2,745
  • 2
  • 15
  • 20