1

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.

4
  • You're more likely to be at http://subsite.mysite.com/sites/department%20X/class/SitePages/Home.aspx. Also: how do you plan to determine the links (a or img...) that you want to manipulate? Commented Jan 31, 2011 at 23:41
  • Will it always be (literally) class or can this part differ? Commented Jan 31, 2011 at 23:41
  • Sorry I should have added that, class will also be different. What I want to do is that wherever the script is used it will remove what's between the /department x/ and /sitepages/home.aspx Commented Jan 31, 2011 at 23:44
  • If I can get the correct URL I know how to change the url of the link in question, it's only one link on each page and I can give it a class department or something Commented Jan 31, 2011 at 23:45

1 Answer 1

2

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/");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.