0

I'm learning jQuery, and I'm running into a small problem. jQuery code works when I put it directly in my tags in my HTML, but when I import it from a different file, nothing happens. These are my script tags in HTML:

<script type="text/javascript" src="scripts/jquery-1.10.2.js"></script>
<script type="text/javascript" src="scripts/test.js"></script> <!--This is my jQuery code-->

test.js contains the following code:

$(document).ready(function(){
console.log("Hello");
});

When the page loads, the console is empty. However, when I paste the following code as so, in my html document, everything works fine.

<script>

    $(document).ready(function(){
    console.log("Hello");
    });

</script>
6
  • 2
    It should work, problem must be somewhere else. Is path an filename correct? Commented Aug 19, 2013 at 6:42
  • Do you see any 404 errors in dev tools? Commented Aug 19, 2013 at 6:43
  • There must be a path declaration problem , otherwise it should be works Commented Aug 19, 2013 at 6:44
  • Do you see something like "cannot find file" in errors?. Add jQuery from an online CDN. It might help you to check if this is a path problem. Commented Aug 19, 2013 at 6:44
  • No as per this stackoverflow.com/questions/3859966/… Commented Aug 19, 2013 at 6:48

4 Answers 4

1

Do you really have a folder named scripts in the folder where your HTML files exist? Otherwise try changing this src="scripts/jquery-1.10.2.js" to probably this: src="/scripts/jquery-1.10.2.js"

You can check by browsing to http://yoururl/scripts/jquery-1.10.2.js, the Jquery code should show up. If it's not there, you have to make sure that you include the right folder/file.

Sign up to request clarification or add additional context in comments.

1 Comment

Adding the preceding forwardslash caused a cannot find resource error in Chrome. Also, I did what you said by typing in the URL to the file, and the text shows up fine.
0

Yes.You will be able to push your scripts in 2 ways:: 1-push your scripts in tag like>>

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

2-push your inline scripts with attributes like>>

<button type='button' onclick="$(this).hide()">Hide Me</button>

Comments

0

make sure that the scripts folder contain test.js make sure that the extension is correct ..sometimes I make the same same mistake, instead of test.js i save it as test.js.txt

2 Comments

Triple-checked the file names and path, everything seems to be in order.
code works perfect in my system..please change the code as $(document).ready(function(){ alert("Hello"); }); so that you can see it in browser
0

Try putting

<script type="text/javascript" src="scripts/jquery-1.10.2.js"></script>

in the body rather than the head.

Credits to this answer for giving the idea

Comments

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.