Using JavaScript to alter links to current host

Noah B

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

Jay Blanchard

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Using preg_replace to alter youtube links

From Dev

Using preg_replace to alter youtube links

From Dev

Using javascript to alter content of a <div>

From Dev

Alter data attribute using javascript

From Dev

Custom action links in .PDF...write Javascript to alter the appearance of links when clicked?

From Dev

Using Array Function to Alter Cell Values Based on Their Current Values?

From Dev

Using Array Function to Alter Cell Values Based on Their Current Values?

From Dev

Intercept and alter a site's javascript using greasemonkey

From Dev

Using JavaScript to alter attributes in a MVC web application

From Dev

alter session for current schema

From Dev

Alter Links to Other PDF Documents

From Dev

Using current url in a javascript function

From Dev

How to preload and cache URLs/links using JavaScript?

From Dev

Retrieving links from webpage using javascript

From Dev

Overriding default behavior of links using JQuery and JavaScript

From Dev

Clicking links on just opened pages using javascript

From Dev

javascript alter variable definition

From Dev

Highlighted current links

From Dev

Override host of webapi odata links

From Dev

get number of days in the CURRENT month using javascript

From Dev

How to close current tab using javascript?

From Dev

SharePoint 2013 get current user using JavaScript

From Dev

Add current date using inline javascript

From Dev

Using javascript or jquery to auto populate the current time

From Dev

How to pass current url in the session using javascript

From Dev

JavaScript -- get current date using UTC offset

From Dev

Highlight current day on a <div> table using javascript

From Dev

how to redirect to current page using javascript

From Dev

digital current running clock using javascript not working

Related Related

  1. 1

    Using preg_replace to alter youtube links

  2. 2

    Using preg_replace to alter youtube links

  3. 3

    Using javascript to alter content of a <div>

  4. 4

    Alter data attribute using javascript

  5. 5

    Custom action links in .PDF...write Javascript to alter the appearance of links when clicked?

  6. 6

    Using Array Function to Alter Cell Values Based on Their Current Values?

  7. 7

    Using Array Function to Alter Cell Values Based on Their Current Values?

  8. 8

    Intercept and alter a site's javascript using greasemonkey

  9. 9

    Using JavaScript to alter attributes in a MVC web application

  10. 10

    alter session for current schema

  11. 11

    Alter Links to Other PDF Documents

  12. 12

    Using current url in a javascript function

  13. 13

    How to preload and cache URLs/links using JavaScript?

  14. 14

    Retrieving links from webpage using javascript

  15. 15

    Overriding default behavior of links using JQuery and JavaScript

  16. 16

    Clicking links on just opened pages using javascript

  17. 17

    javascript alter variable definition

  18. 18

    Highlighted current links

  19. 19

    Override host of webapi odata links

  20. 20

    get number of days in the CURRENT month using javascript

  21. 21

    How to close current tab using javascript?

  22. 22

    SharePoint 2013 get current user using JavaScript

  23. 23

    Add current date using inline javascript

  24. 24

    Using javascript or jquery to auto populate the current time

  25. 25

    How to pass current url in the session using javascript

  26. 26

    JavaScript -- get current date using UTC offset

  27. 27

    Highlight current day on a <div> table using javascript

  28. 28

    how to redirect to current page using javascript

  29. 29

    digital current running clock using javascript not working

HotTag

Archive