Search code examples
javascriptphptagsechobreadcrumbs

PHP echo <tag> content from the same page for breadcrumbs


I am using the following script to create breadcrumbs, however it takes the same foldername as the name of the link. Is there a way to echo the first 4 words for example from the title tag of the html page (<title>Any written title here for the page</title>) instead of the foldername?

<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>

So the link name would be instead of the foldername, it becomes Any written title here....


Solution

  • Yes, you can use document.title to get the title of the page.

    So you can try something like this:

    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) + "\">" + document.title.split(" ").slice(0, 4).join(" ") + "...</A>";
    var url = window.location.protocol + "//" + path;
    document.writeln(url);