简体   繁体   中英

How to get value from the URL

Thease days, I study about WebRTC from https://bitbucket.org/webrtc/codelab But, I have some problem at step5. I can't resolve step5 bonus points number 5. Question is this : This app uses a JavaScript prompt to get a room name. Work out a way to get the room name from the URL, for example localhost:2013/foo would give the room name foo.

I used javascript window.location.pathname, but it didn't work. If you help me, I very appreciate it!

You can do like this:

var str = window.location.href;
var path = str.split('/');
var value = path[path.length-1]; //foo

pathname actually works.

<html>
    <head>
        <script>
            alert(window.location.pathname);
        </script>
    </head>
    <body>
    </body>
</html>

you can use this code

var aarr = window.location.href.split('/');
//get last value
var id = aarr[aarr.length -1];

You can use lastIndexOf since you're only concerned about getting one element instead of using split which would return all of them.

var url = "http://localhost:2013/foo";
console.log(url.substr(url.lastIndexOf('/') + 1)); // returns 'foo'

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