Can we get community base url without apex?












4















I want to get community base url in my lightning component without apex. Right now I am using the design component to store the value but that is not optimal as I have multiple page references and typing each of them in the community builder is really hard and error prone.










share|improve this question



























    4















    I want to get community base url in my lightning component without apex. Right now I am using the design component to store the value but that is not optimal as I have multiple page references and typing each of them in the community builder is really hard and error prone.










    share|improve this question

























      4












      4








      4








      I want to get community base url in my lightning component without apex. Right now I am using the design component to store the value but that is not optimal as I have multiple page references and typing each of them in the community builder is really hard and error prone.










      share|improve this question














      I want to get community base url in my lightning component without apex. Right now I am using the design component to store the value but that is not optimal as I have multiple page references and typing each of them in the community builder is really hard and error prone.







      lightning-aura-components community






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 14 '18 at 16:37









      sfdcLynxsfdcLynx

      1356




      1356






















          1 Answer
          1






          active

          oldest

          votes


















          5














          Yes, you can retrieve the current page url in your JavaScript controller. And set the value of attribute in your component for later use according to your requirement.



          Component



           <aura:attribute name="cbaseURL" type="String"/>


          JS Controller



           var urlString = window.location.href;
          var baseURL = urlString.substring(0, urlString.indexOf("/s"));
          component.set("v.cbaseURL", baseURL);


          Hope this helps.



          EDIT:



          As RedDevil highlighted that using window.location can create issues with Microsoft browsers. I tend to search a bit and find out interesting result. So I'd like to all the readers must go through this stackoverflow link to read about the details and use whatever suits you best.



          However the widely accepted answer says:




          document.location is a synonym for window.location that has been
          deprecated for almost as long as JavaScript has existed. Don't use it.



          location is a structured object, with properties corresponding to the
          parts of the URL. location.href is the whole URL in a single string.
          Assigning a string to either is defined to cause the same kind of
          navigation, so take your pick.



          I consider writing to location.href = something to be marginally
          better as it's slightly more explicit about what it's doing. You
          generally want to avoid just location = something as it looks
          misleadingly like a variable assignment. window.location = something
          is fine though.







          share|improve this answer





















          • 1





            use document.location instead of window.location since this has issues in Microsoft browsers.

            – RedDevil
            Nov 14 '18 at 16:45











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "459"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f239372%2fcan-we-get-community-base-url-without-apex%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          5














          Yes, you can retrieve the current page url in your JavaScript controller. And set the value of attribute in your component for later use according to your requirement.



          Component



           <aura:attribute name="cbaseURL" type="String"/>


          JS Controller



           var urlString = window.location.href;
          var baseURL = urlString.substring(0, urlString.indexOf("/s"));
          component.set("v.cbaseURL", baseURL);


          Hope this helps.



          EDIT:



          As RedDevil highlighted that using window.location can create issues with Microsoft browsers. I tend to search a bit and find out interesting result. So I'd like to all the readers must go through this stackoverflow link to read about the details and use whatever suits you best.



          However the widely accepted answer says:




          document.location is a synonym for window.location that has been
          deprecated for almost as long as JavaScript has existed. Don't use it.



          location is a structured object, with properties corresponding to the
          parts of the URL. location.href is the whole URL in a single string.
          Assigning a string to either is defined to cause the same kind of
          navigation, so take your pick.



          I consider writing to location.href = something to be marginally
          better as it's slightly more explicit about what it's doing. You
          generally want to avoid just location = something as it looks
          misleadingly like a variable assignment. window.location = something
          is fine though.







          share|improve this answer





















          • 1





            use document.location instead of window.location since this has issues in Microsoft browsers.

            – RedDevil
            Nov 14 '18 at 16:45
















          5














          Yes, you can retrieve the current page url in your JavaScript controller. And set the value of attribute in your component for later use according to your requirement.



          Component



           <aura:attribute name="cbaseURL" type="String"/>


          JS Controller



           var urlString = window.location.href;
          var baseURL = urlString.substring(0, urlString.indexOf("/s"));
          component.set("v.cbaseURL", baseURL);


          Hope this helps.



          EDIT:



          As RedDevil highlighted that using window.location can create issues with Microsoft browsers. I tend to search a bit and find out interesting result. So I'd like to all the readers must go through this stackoverflow link to read about the details and use whatever suits you best.



          However the widely accepted answer says:




          document.location is a synonym for window.location that has been
          deprecated for almost as long as JavaScript has existed. Don't use it.



          location is a structured object, with properties corresponding to the
          parts of the URL. location.href is the whole URL in a single string.
          Assigning a string to either is defined to cause the same kind of
          navigation, so take your pick.



          I consider writing to location.href = something to be marginally
          better as it's slightly more explicit about what it's doing. You
          generally want to avoid just location = something as it looks
          misleadingly like a variable assignment. window.location = something
          is fine though.







          share|improve this answer





















          • 1





            use document.location instead of window.location since this has issues in Microsoft browsers.

            – RedDevil
            Nov 14 '18 at 16:45














          5












          5








          5







          Yes, you can retrieve the current page url in your JavaScript controller. And set the value of attribute in your component for later use according to your requirement.



          Component



           <aura:attribute name="cbaseURL" type="String"/>


          JS Controller



           var urlString = window.location.href;
          var baseURL = urlString.substring(0, urlString.indexOf("/s"));
          component.set("v.cbaseURL", baseURL);


          Hope this helps.



          EDIT:



          As RedDevil highlighted that using window.location can create issues with Microsoft browsers. I tend to search a bit and find out interesting result. So I'd like to all the readers must go through this stackoverflow link to read about the details and use whatever suits you best.



          However the widely accepted answer says:




          document.location is a synonym for window.location that has been
          deprecated for almost as long as JavaScript has existed. Don't use it.



          location is a structured object, with properties corresponding to the
          parts of the URL. location.href is the whole URL in a single string.
          Assigning a string to either is defined to cause the same kind of
          navigation, so take your pick.



          I consider writing to location.href = something to be marginally
          better as it's slightly more explicit about what it's doing. You
          generally want to avoid just location = something as it looks
          misleadingly like a variable assignment. window.location = something
          is fine though.







          share|improve this answer















          Yes, you can retrieve the current page url in your JavaScript controller. And set the value of attribute in your component for later use according to your requirement.



          Component



           <aura:attribute name="cbaseURL" type="String"/>


          JS Controller



           var urlString = window.location.href;
          var baseURL = urlString.substring(0, urlString.indexOf("/s"));
          component.set("v.cbaseURL", baseURL);


          Hope this helps.



          EDIT:



          As RedDevil highlighted that using window.location can create issues with Microsoft browsers. I tend to search a bit and find out interesting result. So I'd like to all the readers must go through this stackoverflow link to read about the details and use whatever suits you best.



          However the widely accepted answer says:




          document.location is a synonym for window.location that has been
          deprecated for almost as long as JavaScript has existed. Don't use it.



          location is a structured object, with properties corresponding to the
          parts of the URL. location.href is the whole URL in a single string.
          Assigning a string to either is defined to cause the same kind of
          navigation, so take your pick.



          I consider writing to location.href = something to be marginally
          better as it's slightly more explicit about what it's doing. You
          generally want to avoid just location = something as it looks
          misleadingly like a variable assignment. window.location = something
          is fine though.








          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 14 '18 at 16:59









          sfdcLynx

          1356




          1356










          answered Nov 14 '18 at 16:43









          LetMeCodeYouLetMeCodeYou

          525310




          525310








          • 1





            use document.location instead of window.location since this has issues in Microsoft browsers.

            – RedDevil
            Nov 14 '18 at 16:45














          • 1





            use document.location instead of window.location since this has issues in Microsoft browsers.

            – RedDevil
            Nov 14 '18 at 16:45








          1




          1





          use document.location instead of window.location since this has issues in Microsoft browsers.

          – RedDevil
          Nov 14 '18 at 16:45





          use document.location instead of window.location since this has issues in Microsoft browsers.

          – RedDevil
          Nov 14 '18 at 16:45


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Salesforce Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f239372%2fcan-we-get-community-base-url-without-apex%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Tangent Lines Diagram Along Smooth Curve

          Yusuf al-Mu'taman ibn Hud

          Zucchini