最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

arrays - Javascript Split URL - Stack Overflow

programmeradmin1浏览0评论

I want to SPLIT some specific parts of the URL, here is what i have so far.

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

var redirectpath = "/?q="
window.location.href = redirectpath + query;
</script>

The URL structure will be like this:

.html

The variable query outputs like this; page,2013,05,some-page-title

i only want the some-page-title part and also remove the hyphens.

so the final output would be /?q=some page title

how is that possible? Please help!! Thanks

I want to SPLIT some specific parts of the URL, here is what i have so far.

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

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

The URL structure will be like this:

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

The variable query outputs like this; page,2013,05,some-page-title

i only want the some-page-title part and also remove the hyphens.

so the final output would be http://www.mydomain./search/?q=some page title

how is that possible? Please help!! Thanks

Share Improve this question asked Aug 15, 2013 at 14:20 33_____________33_____________ 451 gold badge1 silver badge10 bronze badges 1
  • Split the pathname by "/" and store in query. Take the last item in the array (var page = query.pop();) and remove the ".html": page = page.replace(/\.html$/, "");, then replace "-" with " ": page = page.replace(/-/g, " ");, then make the final string: var redirectpath = "http://www.mydomain./search/?q=" + page; – Ian Commented Aug 15, 2013 at 14:24
Add a ment  | 

2 Answers 2

Reset to default 7

Split returns an array, use it as an array!

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./search/?q="
window.location.href = redirectpath + query[0];

This assuming you always want the part of the url after the last /

//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/plaint-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/plaint-items">plaint-items</a>/
                                </div> */
发布评论

评论列表(0)

  1. 暂无评论