简体   繁体   中英

display username in html <p> tag from the url

After the login form has been submitted the page will redirect to the users page and the username is shown as the last value in the url. What i want to do it display the username from the url in a html p tag.

(login.js)

 var loginApp = new Vue({
        el: '#login',
        data: {
            username: '',
            password: '',
        },

        methods: {
            onSubmit: function () {
                // check if the user already exists
                var users = '';

                var newUser = this.username;
                var passcheck = this.password;

                if (localStorage.getItem('users')) { // 'users' is an array of objects
                    users = JSON.parse(localStorage.getItem('users'));
                }

                if (users) {
                    if (users.some(function (user) {
                        return user.username === newUser & user.password === passcheck

                    })) {

                        alert('Welcome back-' + newUser);
                        window.location.href = 'user-profile.html' + '#' + newUser;




                    } else {
                        alert('Incorrect username or password');


                    }
                }
            }
        }

    });

(user-profile.html)

<script>
    var segment_str = window.location.newUser;
    var segment_array = segment_str.split( '/' );
    var last_segment = segment_array.pop();
    document.write(last_segment); // alerts segment4

</script>

在此处输入图像描述

Try to use jquery:

$("p").text('your username')

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