当前位置:网站首页>Location object

Location object

2022-06-24 18:35:00 Brother Mengfan

Catalog

1、 What is? location object

2、location attribute

3、location Method


1、 What is? location object

window Object location attribute , This property is used to get or set the... In the browser address bar URL value . This
An object will be returned after the property is obtained or set , So this attribute is called location object .
URLUniform Resource Locator) It's called uniform resource locator , Is the address of standard resources on the Internet . It consists of the following
Several parts .

Example :

    <form><input type="text" name="account" placeholder=" account number ">
        <input type="password" name="pwd" placeholder=" password ">
        <input type="submit" value=" Submit "> </form>
    <script>
        console.log(location);
        console.log(location.protocol); //  agreement  
        console.log(location.host);
        console.log(location.hostname + ', ' + location.port);
        console.log(location.href);
        console.log(location.search);
    </script>

2、location attribute

  Example : Save the request parameters in the object . Such as : hold user=xaoupeng&pwd=123456 Save to an object .

    <form>
        <input type="text" name="account" placeholder=" account number " value="xaoupeng">
        <input type="password" name="pwd" placeholder=" password " value="123">
        <input type="text" name="code" placeholder=" Verification Code " value="abc1">
        <input type="submit" value=" Submit ">
    </form>
    <script>
        //  demand : Save the request parameters in the object . Such as : hold  user=xaoupeng&pwd=123  Save to an object .
        function getSearchString() {
            // 1.  adopt  location  Of  search  Property to get the parameter list of the request  
            // '?account=xaoupeng&pwd=123' 
            var queryString = location.search;
            // 1.1  Determine whether the request parameters exist  
            queryString = queryString.length > 0 ? queryString : '';
            // 2.  Remove the... Before the string  ?  Number  
            // 'account=xaoupeng&pwd=123' 
            queryString = queryString.substr(1);
            // console.log(queryString); 
            // 3.  Cut multiple parameters  
            // ['account=xaoupeng', 'pwd=123'] 
            var obj = {};
            if (queryString.indexOf('&') !== -1) {
                var parameters = queryString.split('&')
                    // 4.  Traversal array , Then the parameter name and parameter value are segmented , And assign it to the object .
                var items = null;
                for (var i = 0; i < parameters.length; i++) {
                    //console.log(parameters[i]); 
                    items = parameters[i].split('=')
                        //console.log(items); 
                    obj[items[0]] = items[1]
                }
            }
            console.log(obj);
            return obj;
        }
        var result = getSearchString();
        console.log(result);
    </script>

3、location Method

In addition to the above properties ,location The following methods are also provided in .

  Method use example :

        // location.href = 'https://www.baidu.com/' 
        location.assign('https://www.baidu.com');
        // location.replace('a.html');
        // location.reload();

原网站

版权声明
本文为[Brother Mengfan]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202211333167002.html

随机推荐