Javascript拆分URL
我想拆分URL的一些特定部分,以下是我到目前为止所做的Javascript拆分URL,javascript,arrays,split,Javascript,Arrays,Split,我想拆分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 = re
<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>
变量query
这样输出;
page,2013,05,部分页面标题
我只想要一些页面标题
部分,还需要删除连字符
因此,最终输出将是http://www.mydomain.com/search/?q=some 页面标题
这怎么可能?请帮忙!!
谢谢拆分返回一个数组,将其用作数组
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的最后一个/
//获取url之后的部分
//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> */
var url=window.location;
//函数获取主机名和路径名
//var l=getLocation(url);
//将路径名拆分为/
var array=url.pathname.split('/');
//修复concate的url协议和主机名
var pathnames=window.location.protocol+“/”+window.location.host;
//循环以获取拆分的url路径名,并在特定div中插入每个url
对于(i=1;i
按“/”
拆分路径名
,并存储在查询中
。获取数组中的最后一项(var page=query.pop();
)并删除“.html”:page=page.replace(/\.html$/,”)代码>,然后将“-”替换为“”:page=page.replace(/-/g,”)代码>,然后生成最后一个字符串:var redirectpath=”http://www.mydomain.com/search/?q=“+第页代码>我自由编辑了你的答案,只添加了连字符删除:)^你们真是太棒了;)我把这篇文章加入书签,以便稍后回来为你们投票(因为我没有声誉去投票)
//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> */