0

I have this code of a breadcrumb that I found, but it shows the whole link. How can I make a similar simple breadcrumb that shows only the name of the page based on the path? (only in JavaScript) (it would be good to change the background color too)

<SCRIPT LANGUAGE="JavaScript">


var path = "";
var href = document.location.href;
var s = href.split("/");
for (var i=2;i<(s.length-1);i++) {
path+="<A HREF=\""+href.substring(0,href.indexOf("/"+s[i])+s[i].length+1)+"/\">"+s[i]+"</A> / ";
}
i=s.length-1;
path+="<A HREF=\""+href.substring(0,href.indexOf(s[i])+s[i].length)+"\">"+s[i]+"</A>";
var url = window.location.protocol + "//" + path;
document.writeln(url);

</script>
Ry-
  • 218,210
  • 55
  • 464
  • 476
Sara980710
  • 11
  • 1

1 Answers1

0

do you mean that it will not include the site in the breadcrumbs??

<script language="Text/JavaScript">


var path = "";
var href = document.location.href;
var s = href.split("/");
for (var i=2;i<(s.length-1);i++) {
  path+="<A HREF=\""+href.substring(0,href.indexOf("/"+s[i])+s[i].length+1)+"/\">"+(i == 2 ? "home" : s[i])+"</A> / ";
}
i = s.length-1;
path += "<A HREF=\""+href.substring(0,href.indexOf(s[i])+s[i].length)+"\">"+s[i]+"</A>";
var url = path;
document.writeln(url);

</script>
Cuzi
  • 1,026
  • 10
  • 16
  • No right now it shows: http://www.javascriptkit.com / script / script2 / breadcrumb.shtml But I want it to be like (with hyperlinks): Home/script/script2/breadcrumb – Sara980710 Apr 17 '17 at 12:59
  • do you mean to replace the "javascriptkit.com" to "Home"? if so, i updated the example above. – Cuzi Apr 17 '17 at 13:10
  • Well not exactly home, it was just an example. I want to show the title of the recent pages that you have been on before, to know where you are and have been. It doesn't show anything when I copy-paste your code. – Sara980710 Apr 17 '17 at 13:21
  • In my answer I replaced the string in `i=2` to "home" just replace it to the string that you need – Cuzi Apr 17 '17 at 13:42
  • Ah okay, but do you think it could be automatic? When you create the page you usually give it an title/name. Could you take that name automatically instead of writing it manually? And how do I delete the "https://" – Sara980710 Apr 17 '17 at 13:56
  • I'm not familiar with ajax :P How do you implement that to the navigation? – Sara980710 Apr 17 '17 at 14:07
  • You need to create a function that will return to you the title. And put it it where I wrote "Home" – Cuzi Apr 17 '17 at 14:08
  • How do I do that? – Sara980710 Apr 17 '17 at 14:16