简体   繁体   English

Javascript 帮助 - 我的面包屑脚本正常工作

[英]Javascript Help - my breadcrumbs script to work correctly

Can someone take a look at this and tell me what I'm doing wrong?有人可以看看这个并告诉我我做错了什么吗? Basically the script is supposed to grab the URL of the page, splits the URL into an array via '/' and then document writes the HTML href link for each subdirectory.基本上,该脚本应该获取页面的 URL,将 URL 通过“/”拆分为一个数组,然后文档为每个子目录链接写入 HTML。 It kinda works until there is a webpage that is 2 folders deep from the root, and then it messes up.它有点工作,直到有一个距根目录深 2 个文件夹的网页,然后它就搞砸了。

If this is the URL for example: website.com/root/folder/subfolder/page.html it gives me:如果这是 URL 例如: website.com/root/folder/subfolder/page.html它给了我:

> Home / Folder / Subfolder / Page

visually on the page, which is correct.视觉上在页面上,这是正确的。 (website.com/root/ is the actual home page.) (website.com/root/ 是实际的主页。)

The link for 'Home' is correct and takes you to website.com/root/ . “主页”的链接是正确的,可将您带到website.com/root/

The second link for 'Folder' is correct and takes you to website.com/root/folder/ . “文件夹”的第二个链接是正确的,它会将您带到website.com/root/folder/

However, the third link for 'Subfolder' is wrong and for some reason links to website.com/root/ again.但是,“子文件夹”的第三个链接是错误的,并且由于某种原因再次链接到website.com/root/

It's got to be something on line 14 where it says getLoc(i-3,subs[i],subs.length)+defp+ but I can't figure out what I'm missing.它必须是第 14 行的内容,上面写着getLoc(i-3,subs[i],subs.length)+defp+但我不知道我错过了什么。

Here is the script:这是脚本:

function breadCrumbs(base,delStr,defp,cStyle,tStyle,dStyle,nl) { 
    tit=document.title.replace(/Ak DRB > /g,"");
    loc=window.location.toString();
    subs=loc.split("/");
    document.write('<strong>></strong> <a href="/drb/" class="'+cStyle+'">Home</a>  '+'<span class="'+dStyle+'">'+delStr+'</span> ');
    if (loc.includes(".html") && !loc.includes("index.html")) {
        a=1;
    }else{
        a=2;
    }
    for (i=4;i<(subs.length-a);i++) {
        SUB=makeCaps(unescape(subs[i]));
        document.write('<a href="'+getLoc(i-3,subs[i],subs.length)+defp+'" class="'+cStyle+'">'+SUB+'</a>  '+'<span class="'+dStyle+'">'+delStr+'</span> ');
    }
    if (nl==1) document.write("<br>");document.write('<span class="'+tStyle+'">'+tit+'</span>');
}
function makeCaps(a) {
  g=a.split(' ');for (l=0;l<g.length;l++) g[l]=g[l].toUpperCase().slice(0,1)+g[l].slice(1);
  return g.join(" ");
}
function getLoc(c,e,f) {
  var d="";
  if (c>0) {
    for (k=0;k<c;k++) d=d+"../";
  }else{
    d="../"+e+"/";
  }
  return d;
}

OMG, I got it to work.天哪,我让它工作了。 I just had my relative path structure wrong and was trying to make it too complicated.我只是把我的相对路径结构弄错了,并试图让它变得太复杂。 All you need for the final getLoc function is:最终getLoc function 所需的只是:

function getLoc(c,e,f) {
  var d="";
  var g=f-5;

  if (c==g) {
      d="./";
  }else{
      d="../";
  }
  return d;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM