peter
peter

Reputation: 445

Use JavaScript/jQuery to manipulate image link url?

SharePoint won't let me set the relative URL I want to use for a link so I'll have to use jQuery/JavaScript instead to manipulate the URL.

Let's say I am at

http://subsite.mysite.com/sites/department X/class/SitePages/Home.aspx

And on this site I have an image with a link which I want to take me to (skip class in the URL)

http://subsite.mysite.com/sites/department X/SitePages/Home.aspx

On another site

 http://subsite.mysite.com/sites/department Y/class/SitePages/Home.aspx

I want to be taken to

http://subsite.mysite.com/sites/department Y/SitePages/Home.aspx

To do this I was thinking about getting the current path with javascript, split on /, count the length of the array and remove what's between the third and second / from the right (class) and then build the URL again but is this the best way to go or is there a better way to do it?

Cheers

Edit: maybe I tried to achieve it in a way to difficult way now, it's just that if I set the url using the sharepoint gui with a relative path like ../sitepages/home.aspx or whatever sharepoint overrides this on save and set the full url. However, instead of having to split and stuff I can just change the url to the relative on with javascript.

Upvotes: 1

Views: 615

Answers (1)

gilly3
gilly3

Reputation: 91527

Use String.replace() with a regex that will match your urls. For example, this might work for you:

link.href = link.href.replace(/\/[^\/]+\/SitePages\//, "/SitePages/");

Upvotes: 2

Related Questions