function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
FSamorganFSamorgan 

How to compose URLs in Communities Visualforce page

I have a Visualforce page that has javascript that creates urls as var url = '/' + obj.Id; this works fine when accessed in the normal Salesforce context but when this Visualforce page is accessed from a community then the url no longer works. This is because it expects the url to be var url = '/mycommunity/' + obj.Id;
I cannot use the typical solution {!URLFOR()} because the objects are being pulled via Javascript Remoting. Is there a way via Apex or preferrably Javascript to know the community name (if present)? So I can do var url = '/'; if (isCommunity) url += communityName + '/' ; url += obj.Id;
I need a solution that works both in and out of a community using the same Visualforce page.
Best Answer chosen by FSamorgan
Dhanya NDhanya N
Hi,

If you are trying to redirect then you can try this once - 
​<a href="{!$Site.BaseUrl}/{!obj.Id}"> </a>

Thanks,
Dhanya

All Answers

Bryan JamesBryan James
You could create a variable "location" and assign it to "window.location"
then you could use the "location.origin" to set your criteria
FSamorganFSamorgan
I know how to use the url. That's not the problem. I'm trying to get the community name in order to build the url.
Dhanya NDhanya N
Hi,

If you are trying to redirect then you can try this once - 
​<a href="{!$Site.BaseUrl}/{!obj.Id}"> </a>

Thanks,
Dhanya
This was selected as the best answer
FSamorganFSamorgan
var url = "{!JSENCODE($Site.BaseUrl)}/" + obj.Id;

Thanks Dhanya, that works for me.