1

I'm trying to integrate a simple html, js, and css file:

like this example on this link: jsfiddle.net/MHDhT

I can link no problem to the css file like this (in the head tag):

<link rel="stylesheet" href="style.css" />

but then I dont know how to link to the js file, and Im not sure If just having the js content I can see on that link is enough ??

can someone help me ?

7
  • the js fiddle (seems) to work perfectly fine to me. What exactly is your problem? you are very unspecific Commented May 22, 2013 at 15:08
  • 2
    <script src="src.js"></script> Commented May 22, 2013 at 15:08
  • 1
    Also it uses jQuery which you need to include before your script. Commented May 22, 2013 at 15:10
  • 1
    @Zenith - put that in an answer so he can accept it Commented May 22, 2013 at 15:11
  • I have a directory with 3 files...html, css, and js, but I dunno if what I need to put inside the js file is only what is on the fiddle link, and how to link from the html to the website... Commented May 22, 2013 at 15:18

3 Answers 3

2

Your js shoul be place on the end of your page for loading performance. (Like that the browser can start to render the web-page before the js has finished to load). But only if your Js is not mandatory for your Dom to load (it should be the case)

you can link them in two differents ways:

<script type="text/javascript">
   //Here is your code
</script>

OR

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

EDIT

Be sure to include jquery:

<head>
     <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>

If your code require the dom to be ready to be executed your should use

$(document).ready(function() {
  //This is executed when dom is ready
});

The final result shoul look like this: (I put all In 1 file to be easier for you)

<html>
    <head>
        <style type"text/css">
              .menu { 
              background: #3B6997;
              font-weight: bold;
              padding: 10px;
              width: 300px;
              display:none;
              }
              .menu a {
              color: white;
              }
              .hov {
              width: 300px;   
              }

    </style>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            $('.hov').hover(function() {
                $('.menu').slideDown();
            }, function() {
                $('.menu').slideUp();
            });
        });
    </script>
</head>
<body>
    <div class="hov">
        <a href="#">Activate Menu vv</a>

        <ul class="menu">
            <li>
                <a href="#">Item</a>
            </li>
        </ul>
    </div>
</body>
</html>

If you want to put yout js in separate file, let's say 'app.js', put this in this file:

$(document).ready(function() {
    $('.hov').hover(function() {
        $('.menu').slideDown();
    }, function() {
        $('.menu').slideUp();
    });
});

You link it by putting this on your head section :

<script type="text/javascript" src="app.js"></script>
Sign up to request clarification or add additional context in comments.

5 Comments

Strange, I cannot make it work...In the head tag I put this: <script type="text/javascript" src="ajax.googleapis.com/ajax/libs/jquery/1.9.1/…> <script type="text/javascript" src="file.js"></script>
in the file.js: $('.hov').hover(function(){ $('.menu').slideDown(); },function(){ $('.menu').slideUp(); });
@javardo you must have forgot the $(document).ready(function() {});
thanks, it works. But what it I want the js in a separate file ? I'm not sure, if I'm not linking correctly to it, or it's content is not correct...
thank you very much for your help....In fact I dont know what I did wrong cause I think I had the same thing, but I copied your solution and it works...maybe I had some typo or something. Anyways thanks :D
1

In JSFiddle you no need to specify these html, head, external css or js tags. But in your .html it is must. The script tag is used for linking a javascript file.

<html>
        <head>
        <link rel="stylesheet" href="/folderpath/style.css" />
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
        <script type="text/javascript" src="/folderpath/filename.js"></script>
        </head>
        <body></body>
</html>

1 Comment

The type attribute is not necessary.
0

To insert a JavaScript into an HTML page, use the <script> tag in the .html file

<script>
   //Your js code here 
</script> 

To include a separate file (.js), use :

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

1 Comment

not working, it the html file I put inside the head tag: <script src="file.js"></script> and then in the same directory theres the file.js with the exact code, of the link...but once I open the html in the browser it doesnt work like like in the example...

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.