2

我想拆分 URL 的某些特定部分,这就是我目前所拥有的。

<script type='text/javascript'>
var query = window.location.pathname.split( '/' );
query = window.location.pathname.split( '.html' );

var redirectpath = "http://www.mydomain.com/search/?q="
window.location.href = redirectpath + query;
</script>

URL 结构将如下所示:

http://www.mydomain.com/page/2013/05/some-page-title.html

变量query输出是这样的; page,2013,05,some-page-title

我只想要这some-page-title部分并删除连字符。

所以最终的输出是http://www.mydomain.com/search/?q=some page title

这怎么可能?请帮忙!!谢谢

4

2 回答 2

7

拆分返回一个数组,将其用作数组!

var parts = window.location.pathname.split( '/' );
var query = parts[parts.length-1].split( '.html' );

query[0]= query[0].replace(/-/g," ");   

var redirectpath = "http://www.mydomain.com/search/?q="
window.location.href = redirectpath + query[0];

这假设您总是想要最后一个之后的 url 部分/

于 2013-08-15T14:23:59.517 回答
1
//get the url
                                var url = window.location;
                                //function to get the hostname and pathname
                                //var l = getLocation(url);
                                //split the pathname by /
                                var array = url.pathname.split('/');

                                //fix the url protocol and hostname for concate
                                var pathnames = window.location.protocol + "//" + window.location.host;

                                //loop to get the splited url pathname and insert the each url in specific div
                                for (i = 1; i < array.length; i++) {

                                    //concatenate the path for each loop
                                    pathnames = pathnames + '/' + array[i];

                                    //appending an ancher tag with href for path in the element with id table_panel_header
                                    $("div#table_panel_header").append(
                                            '<a href="' + pathnames + '">'
                                                    + array[i] + '</a>/');
                                }

                                //example text  seen as: Complaint_Module/master/complaint-items/

                               /* example  href will append like
                               <div id="table_panel_header">
                                <a href="http://localhost:8010/Complaint_Module">Complaint_Module</a>/
                                <a href="http://localhost:8010/Complaint_Module/master">master</a>/
                                <a href="http://localhost:8010/Complaint_Module/master/complaint-items">complaint-items</a>/
                                </div> */
于 2015-09-24T08:48:30.467 回答