我正在尝试为我创建的网站创建导航系统。我花了几个小时试图弄清楚,但我不明白为什么它不工作 我试图用变量文件名替换所有“index.html”的事件。Javascript String.Replace does not work?
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.htm”明明不是“的index.html”,它消除了“/”从输出,所以我得到“ HTTP://localhost/xxxindex.html/newpage.html”。
为什么不简单'url.replace(“/ index.html”,文件名)'? – 2011-05-15 12:25:16
该代码实际上工作得很好。 – Pointy 2011-05-15 12:26:51
如果我是你,我会提出一个'alert(文件名)'来确保你知道正在发生的一切。 – Pointy 2011-05-15 13:11:22