[JavaScript]How to parse the URL and get the different fragments easily?
2014-01-19 08:19
686 查看
In this tutorial, i will show you how to get the href, protocol,host, host name,port and path information from URL with the help of JS.
var urlParsingNode = document.createElement("a"); var originUrl = urlResolve(window.location.href, true); function urlResolve(url, base) { var href = url; if (msie) { // Normalize before parse. Refer Implementation Notes on why this is // done in two steps on IE. urlParsingNode.setAttribute("href", href); href = urlParsingNode.href; } urlParsingNode.setAttribute('href', href); return { href: urlParsingNode.href, protocol: urlParsingNode.protocol ? urlParsingNode.protocol.replace(/:$/, '') : '', host: urlParsingNode.host, search: urlParsingNode.search ? urlParsingNode.search.replace(/^\?/, '') : '', hash: urlParsingNode.hash ? urlParsingNode.hash.replace(/^#/, '') : '', hostname: urlParsingNode.hostname, port: urlParsingNode.port, pathname: (urlParsingNode.pathname.charAt(0) === '/') ? urlParsingNode.pathname : '/' + urlParsingNode.pathname }; } function urlIsSameOrigin(requestUrl) { var parsed = (isString(requestUrl)) ? urlResolve(requestUrl) : requestUrl; return (parsed.protocol === originUrl.protocol && parsed.host === originUrl.host); }
相关文章推荐
- use noscript html tag when user disable the javascript in browser, guide user how to enable the js in different browser and retu
- JSP URI/URL - How to get the request URI, URL, and Context from a JSP
- how to get url and hostname and url text in one page
- SQL - How to get the current day, month and year
- How to get and set the drawing order of layers in globe(获取并设置Globe图层的叠加次序:)
- virtual memory layout and how to get it by the correspoinding functuon
- How to get the available RAM and the cpu usage in percents?
- PHP: How to Get the Current Page URL
- How to get and set the system socket buffer in AIX?
- LM_Sensors on Cent OS 5.4 How To Get And Install The Coretemp Module
- Android: How to download the latest zip Android Source Code easily and using it in Intellij
- How to get the query string by javascript?
- How to get the url of a page in OpenERP?
- How to get URL and QueryString value in an ASP.NET page
- How to easily open files and URLs from the command line
- CodeSnip: How to Get Id of the Record Using ASP.NET and SQL Server 2000
- How to Create a site at the specified URL and new database (Command-Line Operation)
- How to get the value of a form element : check box and radio button
- How to get the object type in javascript:
- How to download Xcode 4 / 5 / 6 / 7 and get the DMG file?