【发布时间】:2011-08-25 21:13:55
【问题描述】:
我目前正在尝试为我正在创建的网站创建导航系统。我花了几个小时试图解决这个问题,但我不明白为什么它不起作用 我正在尝试用变量文件名替换所有出现的“index.html”。
function changeSideNav(filenames)
{
var urlarray = window.location.href.split("?");
var url = urlarray[0];
alert(url); // This returns "http://localhost/xxx/index.html"
var urlspl = url.replace(/index.html/gi,filenames);
alert(url.replace(/index.html/i,filenames) +'/'+ filenames); //This returns "http://localhost/xxx/index.html/newpage.html" (if pram was newpage.html).
//i txpected it to return "http://localhost/xxx//newpage.html"
//set a new source
document.getElementById('SideNavIframe').src = urlspl +'/'+ filenames;
}
编辑: 我觉得这很奇怪: 如果我尝试替换“/index.html”而不是“index.html”,它会从输出中删除“/”,所以我得到“http://localhost/xxxindex.html/newpage.html”。
【问题讨论】:
-
为什么不简单地
url.replace("/index.html", filenames)? -
该代码实际上工作得很好。
-
如果我是你,我会发送
alert(filenames)以确保你知道正在发生的一切。
标签: javascript replace split