0

I'm realy new at HTML and JS, so my question may sound dumb: I can't make my HTML code recognise and run my js code! no matter what I do, it doesn't seem that my HTML file find my js file, of if it does - it doesn't do anything.

I'm working on a page as a project, but because the js doesn't work I try to open a simple HTML file with a simple js - but it still doesn't do anything.

my questions are:

  1. what js function can I use just to see that the HTML file finds the js file and everything is working fine?

  2. this is my html simple code:

    <!DOCTYPE html>
    <head>
    <link rel="stylesheet" type="text/css" href="C:\Users\User\Dropbox\WEB\js\try.css">
    <script type="text/javascript" src = "C:\Users\User\Dropbox\WEB\js\scripts.js"></script>
    </head>
    <div>hi there</div>
    <div>bye there</div>
    
    </html>
    

the html and the js file are both in the same folder. The html recognise and reacts to the css file.

this is the js code:

$(document).ready(function(){
    var name = prompt("name?");
    console.log(name);
    $('div').click(function(){
            $('div').fadeOut('slow');
    });

});

yet when I opne the html file with chrome - nothing happens, not when I open and not when I click the div element.

so, what am I doing wrong? do experianced html\js developers can give me some tips on how to prevent this thing from happening in the future?

thx, Yishai

mizenetofa1989
  • 117
  • 2
  • 16
  • Didn't you forgot to include JQuery ? – Seblor May 29 '16 at 19:26
  • 1
    **1.** Your JS code is using jQuery. So you need to load it into your page. **2.** Your `div`s should be contained in a `` tag. **3.** Absolute URLs need a protocol (e.g. `http://`, `https://`). In your case, since you're doing it locally, your URLs should look like this: `file:///C:\Users...` **4.** To test if your code is correctly loaded, just put `alert('JS code loaded');` at the top of it. – blex May 29 '16 at 19:26
  • The problem is with the URL. You should provide relative paths instead of physical paths. – Kundan Singh Chouhan May 29 '16 at 19:26
  • @doug65536 the asker didn't follow the jQuery documentation. Such questions aren't useful for others, period. – John Dvorak May 29 '16 at 19:36
  • Has anyone noted that the question is tagged reactjs, not jquery? Seems as if both use a similar, if not same syntax (not familiar with reactjs myself, though). – Aconcagua May 29 '16 at 19:41
  • ReactJs is totally different stuff then jQuery, jQuery introduces a lot of specific syntax and ReactJs is almost pure JS ES6. I've been using jQuery for ±5 years and ReactJs ±2yrs. – Lukas Liesis May 29 '16 at 19:53
  • You're also probably having problems sourcing local js files. See http://stackoverflow.com/questions/20748630/load-local-javascript-file-in-chrome-for-testing – Jeff Puckett May 29 '16 at 20:10

2 Answers2

0

You have space before = and after =

...src = "C...

in your script tag

<script type="text/javascript" src = "C:\Users\User\Dropbox\WEB\js\scripts.js"></script>

P.s. you shouldn't use such paths. Just use ./js/scripts.js and it means: dear html, find js file in same directory as html file, just go to js subdirectory and grab file named scripts.js

This filename is bad, because it says nothing about what is inside, it's already clear by extension, that it's some javascript stuff.

if you are so new to all this magical world, please be welcome and enjoy your stay at this wonderland.

Pure awesomeness:

https://github.com/bolshchikov/js-must-watch

https://github.com/sorrycc/awesome-javascript

p.s. try some modern IDE for js code writing, you won't make any basic syntax mistakes. Try WebStorm (great out of box, others require plugins), Nuclide or sublime text with some plugins for js.

Lukas Liesis
  • 24,652
  • 10
  • 111
  • 109
0

It seems that you're using jQuery as JS code, you should try to put this before your js script :

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>

And, then, try to use relative path instead of absolute :).

EDIT : like this :

<link rel="stylesheet" type="text/css" href="try.css">
<script type="text/javascript" src = "scripts.js"></script>