Describes the common method for javascript to obtain url Information, including javascripturl.
First with "http://www.cnblogs.com/wuxibolgs329/p/6188619.html#flag? Take test = 12345 as an example, and then obtain the components of it.
1. Obtain the complete url of the page
var a=location.href;console.log(a); // “http://www.cnblogs.com/wuxibolgs329/p/5261577.html#flag?test=12345”
2. Get the Domain Name of the page
var host = window.location.host; //www.cnblogs.comvar host2 = document.domain; //www.cnblogs.comvar a = location.hostname; //www.cnblogs.com
3. Obtain the url protocol
var a=location.protocol;console.log(a); //http:
4. Obtain the port
var a=location.port;console.log(a);
5. Get the page path
var a=location.pathname;console.log(a);
6. set or obtain the URL protocol section
var a = location.protocol;
7. Obtain the part after #
var a=window.location.hash; var b=a.substr(1); console.log(b); // flag?test=12345
8. What is the question mark in the href attribute? Next part
// At this time the case address becomes "http://www.cnblogs.com/wuxibolgs329/p/5261577.html? Test = 12345 ". Get test = 12345var a = location. search; var B = a. substr (1); console. log (B); // if the case is still "http://www.cnblogs.com/wuxibolgs329/p/5261577.html#flag? Test = 12345 ", you need to write the following code to obtain test = 12345var a = location. href; var B = a. substr (a. lastIndexOf ('? ') + 1); console. log (B );
9. Get the part after =
var a=location.href;var b=a.substring(a.lastIndexOf('=')+1);console.log(b); // 12345
The above is all the content of this article. I hope this article will help you in your study or work. I also hope to provide more support to the customer's home!