I need to display the URLs in a list of search results in a breadcrumb style (i.e. such as Google does - Google Breadcrumbs), and have limited knowledge of JavaScript (and haven't touched it in nearly two years). Can you help me please? I can apply the code if it's provided with clear instruction, and am very comfortable with HTML and CSS, but have not attempted to create breadcrumbs without lists before.
Where do I start?
Input would be page's URL (class is .slimBreadcrumbLink) - e.g. https://www.example.com/level1/level2/level3/level4 - and output would be as below:
Level 2 > Level 3 > Level 4
I haven't tried anything of significance yet, I'm starting here. I've read through the other breadcrumb questions posed, but it's not helped so far. I found the below but don't know how to implement it.
var path = location.pathname;
var here = location.href.split('/').slice(3);
var parts = [{
"text": 'Home',
"link": '/'
}];
for (var i = 0; i < here.length; i++) {
var part = here[i];
var text = part.toUpperCase();
var link = '/' + here.slice(0, i + 1).join('/');
parts.push({
"text": text,
"link": link
});
}
Thank you.
Here is a function that will create the HTML structure for the breadcrumbs:
Because it is ES6, you will have to use a transpiler like babel to make it compatible with older browsers. Also, because you are parsing the URL, you cannot customize the title of the links.
You can then use the function like this, to parse the
url
and create theol
list in the element with theid
.