简体   繁体   中英

why this javascript code doesn't work

There are two codes. one works the other doesn't work.

I debugged it and I found that

"var eb = new EventBus(" http://192.168.0.27:8081/bridge ");"

this line in login.js doesn't work well.

Thank you so much everytime.

working

<html>
<head>
<title></title>
<script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>
<script src="//cdn.jsdelivr.net/sockjs/0.3.4/sockjs.min.js"></script>
<script src="vertx-eventbus.js"></script>
</head>

<style>
.news {
    font-size: 20pt;
}
</style>

<body>

    <div class="news">Latest news:</div>
    <br>

    <div id="status" class="news"></div>

    <script>
    var eb = new EventBus("http://192.168.0.27:8081/bridge");
    eb.onopen = function() {
        // set a handler to receive a message
        eb.registerHandler('call', function(error, message) {
            console.log('received a message: ' + JSON.stringify(message));
        });

        // send a message
        eb.send('call', {name : 'tim', age : 587});
    }
</script>

</body>
</html>

no working - login.html

<!DOCTYPE html>
<!--
    login.html 
  -->
<html>
<head>
<meta charset="UTF-8">
<title>Flat HTML5/CSS3 Login Form</title>
</head>

<body>
    <div class="login-page">
        <div class="form">
            <form class="login-form" id="loginForm">
                <input type="text" id="username" placeholder="username" />
                <input type="password" id="password" placeholder="password" />
                <input class="submit" type="submit" title="Login" value="Login" onclick="check(this.form)" />

                <p class="message">
                    Not registered? <a href="#">Create an account</a>
                </p>
            </form>
        </div>
    </div>
    <script src="js/login.js"></script>
</body>
</html>

login.js

document.write("<script src='https://code.jquery.com/jquery-1.11.2.min.js'></script>");
document.write("<script src='//cdn.jsdelivr.net/sockjs/0.3.4/sockjs.min.js'></script>");
document.write("<script src='/vertx-eventbus.js'></script>");   

function check(form)/*function to check userid & password*/
{
    /*the following code checkes whether the entered userid and password are matching*/
    if(form.username.value != "" && form.password.value != "") {
        var eb = new EventBus("http://192.168.0.27:8081/bridge");

        eb.onopen = function() {
            // set a handler to receive a message
            eb.registerHandler('call', function(error, message) {
                console.log('received a message: ' + JSON.stringify(message));
            });

            // send a message
            eb.send('call', {name : 'tim', age : 587});
        }

    } else {
        alert("Input Password or Username")/*displays error message*/
    }
}

You have written

document.write("<script src='/vertx-eventbus.js'></script>");   

differently than in the working code. There is an extra "/" before "vertx..". Could that be the problem?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM