JavaScript link and external script


#1

So when you have external script like official jquery linked to you html can you create a separate js file then link it to your html and write your code in there?Because i have tried it and did not worked, i think i have made mistake somewhere can someone explain to me how it can work.


#2

Hi S09abcd,

Would you mind posting and formatting the code you tried?
(See how to format code here)


#3

I tried but it wont let me because, i wrote it on my computer but it looks like this
Actual Html

<!DOCKTYPE html>

      <html>

            <head>
              <script type="text/javascript" src="jquery.js"></script>
              <script>
                  $(document).ready(function() {
                       alert("working");
                     });
           </head>
           <body>
           </body>
      </html>

The code above works perfectly but my question is can we make something.js file to write the jquery separately from the html beacuse i tried to link the html with something.js but it did not worked.
<link type="text/javascript" rel="javascript" href="something.js">
and i think there is a glitch in formatting code if the text is too close to the left border codes disapper i dont know maybe its just to me.


#4

@s09abcd You're misspelling DOCTYPE on line 1, and you need to use <script src=""> to bring in an external JavaScript file. <link href=""> is mainly for CSS.


#5

And the "glitch" with formatting is intended behavior. But thanks for pointing it out :)


#6

well i tried without docktype which is just basic html, but let me try src="" thanks for replying.


#7

i might just have missed it, but you forgot to close your script:

<script>
                  $(document).ready(function() {
                       alert("working");
                     });
missing </script> tag

To close your script, if your script is internal or external, doesn't matter, you need to close it


#8

Actually i just typed that here because it wont let me to format the code for my indentation.But the jQuery is on .js file which don't need script tag.


#9

Can we see your actual code? just used the backticks what zystvan explained earlier, then it doesn't matter how your code is indented


#10

javascript file

html file


#11

on line 5, make </Script> lowercase (<script>)
on line 6, you shouldn't use <link> to link to javascript files, you should used script for this:

<script type="text/javascript" src="index.js"></script>

the first screenshot, is index.js i assume? You do have a jquery-1.11.3.js file in the same directory as well?


#13

Thanks it did worked i thought <link> is used to link any external file to html.


#14

No, link is used to link to stylesheet. You can read about link on MDN


#15

Thanks again learned something new today.


#16

3 posts were split to a new topic: My JavaScript isn't working properly with my HTML