ANSWER
@Jay-Blanchard came up with a fantastic solution to this problem. I took it a little further and incorporated the location check and add class.
$(document).ready(function () {
var url = window.location.href;
var page = url.substring(url.lastIndexOf('/') + 1);
var pathArray = window.location.href.split('/');
var protocol = pathArray[0];
var host = pathArray[2];
var totalURL = protocol + '://' + host;
$(".Nav-Links li [href]").each(function () {
var linkURL = $(this).attr('href');
var pageURL = linkURL.substring(linkURL.lastIndexOf('/') + 1);
var newURL = protocol + '//' + host + '/' + pageURL;
$(this).attr('href', newURL);
});
$("ul li [href]").each(function() {
if (this.href == window.location.href) {
$(this).addClass("active");
}
});
});
Be aware that this absolutely requires jQuery.
Thanks Jay!
PROBLEM
I have a pretty solid way of building a simple menu that highlights a link if it is the current page, but now I'm working with SharePoint, and I can only present the unpublished design in the edit mode. Basically, the URL changes to http:// www.primary-edit.example.com/ instead of http:// www.example.com/.
What I'm trying to do is gather the current URL strip it down to the base URL, then add that to the start of menu links. That way it will show http:// www.primary-edit.example.com/link.html in edit mode and http:// www.example.com/link.html after publishing, and highlight the current page link correctly.
In the past I have used this:
<ul class="Nav-Links">
<li><a href="http://www.example.com/index.html">Link 1</a></li>
<li><a href="http://www.example.com/second.html">Link 2</a></li>
<li><a href="http://www.example.com/third.html">Link 3</a></li>
</ul>
$(document).ready(function() {
$(".Nav-Links li [href]").each(function() {
if (this.href == window.location.href) {
$(this).addClass("active");
}
});
});
This has worked great in the past, but SharePoint doesn't play fair.
Here is what I have come up with so far.
//Break up the URL into usable pieces
var url = window.location.href;
var page = url.substring(url.lastIndexOf('/') + 1);
var pathArray = window.location.href.split( '/' );
var protocol = pathArray[0];
var host = pathArray[2];
var totalURL = protocol + '://' + host;
//Umm... somehow add "host" to each link in .Nav-Links
//Check the .Nav-Links href for matching links, then add .active to matching link
$(document).ready(function() {
$(".Nav-Links li [href]").each(function() {
if (this.href == window.location.href) {
$(this).addClass("active");
}
});
});
This has done a bang-up job of breaking-up the URL. The problem is, I have no idea how to add the "host" variable to the start of the current link href. Any help you can give will be fantastic.
Thanks
If you must replace them then you can do this -
$(".Nav-Links li [href]").each(function () {
var linkURL = $(this).attr('href');
var pageURL = linkURL.substring(linkURL.lastIndexOf('/') + 1);
var newURL = protocol + '://' + host + '/' + pageURL; // uses your previously established variables
$(this).attr('href', newURL);
});
http://jsfiddle.net/jayblanchard/umabA/ Make sure to inspect the markup with a DOM browser.
Keep in mind that this manipulation assumes that your website and files are positioned within the directory a certain way. If you're expecting users to download and install something you'll also want to take into account @BillyJ's comments.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments