1

I have html with js in one file. I want had two different files js, and html.

This is all in one file (first case), and this code work :

<html>
<head>
    <title>menu</title>
    <script src="https://cdn.jsdelivr.net/jquery/1.12.4/jquery.min.js"></script>
    <script src="https://cdn.jsdelivr.net/jquery.validation/1.15.1/jquery.validate.min.js"></script>

</head>
<body>
<div class="container">
    <h2>Registration</h2>
    <form id="commentForm" method="post" action="Servlet" name="add_data">

        <label for="username">First Name</label>
        <input type="text" name="username" id="username" />

        <button type="submit">Register</button>

    </form>
    <script type="text/javascript">
        $(function() {
            // Initialize form validation on the registration form.
            // It has the name attribute "registration"
            $("form[name='add_data']").validate({
                // Specify validation rules
                rules: {
                    username: {
                        required: true,
                        minlength: 5
                    }
                },

                messages: {
                    username: "Please enter your firstname"
                },

                submitHandler: function(form) {
                    form.submit();
                }
            });
        });
    </script>
</div>
</body>
</html>

But when I create second file valid.js and move all JavaScript code, this is don't work (second case):

<html>
<head>
    <title>Меню</title>
    <script src="https://cdn.jsdelivr.net/jquery/1.12.4/jquery.min.js"></script>
    <script src="https://cdn.jsdelivr.net/jquery.validation/1.15.1/jquery.validate.min.js"></script>
    <script src="valid.js"></script>

</head>
<body>

<div class="container">
    <h2>Registration</h2>
    <form id="commentForm" method="post" action="Servlet" name="add_data">

        <label for="username">First Name</label>
        <input type="text" name="username" id="username" />

        <button type="submit">Register</button>

    </form>
</div>
</body>
</html>

And file valid.js which exist in same folder with html file. This is copy of code in <script> block in first example.

$(function() {
    $("form[name='add_data']").validate({
        rules: {
            username: {
                required: true,
                minlength: 5
            }
        },
        messages: {
            username: "Please enter your firstname"
        },

        submitHandler: function(form) {
            form.submit();
        }
    });
});

HTML and JS files in same directory. May be I need call in html my js code? There my mistake? Why second case don't work? How to fix this issue?

Pavel
  • 2,005
  • 5
  • 36
  • 68
  • 1
    add valid js function code inside $(document).ready(function() {}); block. – Naga Sai A Jun 24 '17 at 02:43
  • 1
    any console errors ? – Naga Sai A Jun 24 '17 at 02:44
  • 1
    Seems to me like it should work, if everything you've said is accurate. If you right click and view source, and then click on your valid.js, it should take you to a text readout of the file. If not, then your reference is wrong. @NagaSaiA, `$(function() {})` is a shorthand version of document.ready for jquery. – Nieminen Jun 24 '17 at 02:45
  • 3
    I just created plunker and it works perfectly - http://plnkr.co/edit/cRUr4AeWGMoJhEv5MRJw?p=preview – Naga Sai A Jun 24 '17 at 02:47
  • 1
    as @Nieminen mentioned this seems to be reference error – Naga Sai A Jun 24 '17 at 02:48
  • @Naga Sai sintaxis error. I think ma by reson is specific java servlet... I use directory WEB-INF for cecuridy and servlet get only my jsp file without js. But this hypothesis. When I use crome console I get: Uncaught SyntaxError: Unexpected token < valid.js:2 . And when i click on Frames->Scripts->valid.js when he show me my HTML not js file. It's very strange... – Pavel Jun 24 '17 at 03:15
  • 1
    @Pavel, Could you please provide screenshot of error and provide complete error and also where did you place the js file .. in the same folder where html file resides? – Naga Sai A Jun 24 '17 at 03:21
  • @Pavel, from the error I see that there is some unwanted string "<" that is causing the error ..Please fix the errors in valid.js at line 2,as per the console error – Naga Sai A Jun 24 '17 at 03:26
  • @NagaSaiA http://savepic.ru/14576780.png http://savepic.ru/14567564.png http://savepic.ru/14571660.png + http://savepic.ru/14592143.pngthis screens – Pavel Jun 24 '17 at 03:37
  • You have to use your script like this – Saurav Jamwal Jun 24 '17 at 05:03

3 Answers3

2

JSP and JS files code are working fine and I have created plunker for reference - http://plnkr.co/edit/cRUr4AeWGMoJhEv5MRJw?p=preview

Issue from the screenshot and console error is that the placement of JS file in your directory sructure.

All files under WEB-INF is unavailable to the outside. Place your static file under WEB-INF folder and move JS files out of WEB-INF and it will work.

Change the reference of js path in the reference after moving valid.js out of WEB-INF

 <script src="**valid.js**"></script>
Naga Sai A
  • 10,771
  • 1
  • 21
  • 40
  • no unfoutenately it's don't work I put my js file in root webapp folder and IDE do refactor src="../../valid.js" it's don't work and I try copypast You're path "**valid.js**"(but I don't understeand what is mean **) it same result what You see in printscreen. – Pavel Jun 24 '17 at 04:46
  • 1
    try adding type="text/javascript" ..... – Naga Sai A Jun 24 '17 at 05:05
  • 1
    Can you share the console log now? – Naga Sai A Jun 24 '17 at 05:06
  • absolutely same how on screen which I send. May by issue in ../../ This is mean that need able for inner WEB-INF and then go up = ../.. but I dont access to WEB-INF. Need come in webapp folder from above but how... – Pavel Jun 24 '17 at 05:10
  • 1
    https://stackoverflow.com/questions/39766914/include-javascript-file-from-inside-web-inf – Naga Sai A Jun 24 '17 at 05:16
  • Yessss!!!!! Thank You!!! In You're last link https://stackoverflow.com/questions/39766914/include-javascript-file-from-inside-web-inf last solution <%@include file="/WEB-INF/views/valid.js" %> is work and work inner WEB-INF!!! You're greate Thank You!!! – Pavel Jun 24 '17 at 05:37
  • Glad it helped you :) – Naga Sai A Jun 24 '17 at 05:40
0

Add <script src="valid.js"></script> at the end of body tag.

  • What is the problem with the code in question? It should also work! Explain why is it not working? – Jenish Rabadiya Jun 24 '17 at 02:46
  • He's using the `$(function() {});` which doesn't really matter if he's importing the JS file before or after since the JS will run after the DOM has loaded. – Brian Moreno Jun 24 '17 at 02:46
  • Look his one file code where his js is below form, and with two files its in head tag. Since that all info he gave without any console errors I gave that answer. It sometimes don;t work on chrome if its in head for no reason Ive had same issues... –  Jun 24 '17 at 02:52
  • 1
    it works on chrome and doesn't matter where he is importing JS file - http://plnkr.co/edit/cRUr4AeWGMoJhEv5MRJw?p=preview – Naga Sai A Jun 24 '17 at 02:54
  • When you add 'ready' but since he didn't add it it won'y work if its in head. –  Jun 24 '17 at 02:56
  • @NoNameNeedToKnow as Nieminen mentioned $(function() {}) is a shorthand version of document.ready for jquery, I just updated plunker with same and no issues – Naga Sai A Jun 24 '17 at 03:00
  • @NoNameNeedToKnow Following that logic, importing jQuery inside the `` shouldn't work either, yet it works in the first example he showed us. It doesn't matter where he's importing his JS file, that's why the `$(function() {}) ;` is there. – Brian Moreno Jun 24 '17 at 03:02
  • Idk who down/up voted my question, but by your answer where you add some code, he didnt ask you what is missing he asked why code isnt working and I gace just answer how his code works on my pc, I didnt try to edit code or do anything else like you did u added extra '$(function() {}) ;' but his code has one your have two explain why its not working as one but it is as two. While I just added answer where it works for me by adding it to the bottom of the page with his code not my code. –  Jun 24 '17 at 17:50
0

It seems to also work in the second case. You could try putting the script tag at the bottom of the page and use this tag

script src="valid.js" type="text/javascript"

with the type property included to be consistent with the first case, and you may want to serve static files in a dev environment using some sort of web server instead of dragging the files into the browser. But, it should work.

pacificpelican
  • 363
  • 1
  • 7