【发布时间】:2012-06-06 00:00:13
【问题描述】:
我目前正在使用以下 javascript 代码:
<script type="text/javascript">
urlPath=window.location.pathname;
urlPathArray = urlPath.split('/');
urlPath1=urlPathArray[2];
document.write('<a href="http://www.example.com/contact.php?id='+urlPath1+'">test</a>');
</script>
假设当前 URL 是 http://www.example.com/products/0033.htm
javascript 生成一个指向http://www.example.com/contact.php?id=0033.htm 的超链接
如何修改此脚本,使 urlPath1 和最终的超链接没有“.htm”部分?
【问题讨论】:
-
文件扩展名是任意大小还是只有
.htm?因为你可以只使用一个子字符串 -
urlPathArray[2];使用urlPathArray[urlPathArray.length - 1]获取最后一个元素...
标签: javascript url location extract