locked
Get current Web url through javascript RRS feed

  • Question

  • Hi,

    I need to have my current website url through Javascript.

    My site is url is,

    http://people.dev.com/PublicCommunity/default.aspx

    I need to get it till, http://people.dev.com/PublicCommunity/

    I have written function like,

                function changeLink()
                {
                  var siteUrl = "<%= SPContext.Current.Web.Url %>";              
                  document.getElementById('anchor').href= siteUrl +"/_vti_bin/ng/activityfeed.svc/Context/"
                  document.getElementById('anchor').target="_blank"
                }

    But its not giving me siteUrl as http://people.dev.com/PublicCommunity/

    Let me know if anybody have idea.

    Thanks


    Rajavi

    Monday, March 19, 2012 8:03 AM

Answers

  • Hi Rajavi,

    Use the following code to get the URL as "http://people.dev.com/PublicCommunity/":
        <script type="text/javascript" language="javascript">
            window.onload = function () {
                var currentUrl = window.location.href;
                currentUrl = currentUrl.substring(0, currentUrl.lastIndexOf('/')+1);
                alert(currentUrl);
            }
        </script>

    Thanks,
    Lhan Han
    • Proposed as answer by SivaMit Tuesday, March 20, 2012 10:14 AM
    • Marked as answer by Lhan Han Monday, March 26, 2012 12:57 AM
    Tuesday, March 20, 2012 8:34 AM

All replies

  • Hi Rajavi,

    check the below links might help you,

    http://www.wictorwilen.se/Post/Working-with-URLs-in-SharePoint-2010-JavaScripts.aspx

    http://snahta.blogspot.in/2010/08/client-object-model-javascript-and-web.html

    you can use javascript as below for url

    window.location.href and get the before lastindexof('/') it will return your desired link.


    Anil

    • Proposed as answer by bendawed Tuesday, July 30, 2013 3:33 PM
    Monday, March 19, 2012 8:10 AM
  • Hi Rajavi,

    Use the following code to get the URL as "http://people.dev.com/PublicCommunity/":
        <script type="text/javascript" language="javascript">
            window.onload = function () {
                var currentUrl = window.location.href;
                currentUrl = currentUrl.substring(0, currentUrl.lastIndexOf('/')+1);
                alert(currentUrl);
            }
        </script>

    Thanks,
    Lhan Han
    • Proposed as answer by SivaMit Tuesday, March 20, 2012 10:14 AM
    • Marked as answer by Lhan Han Monday, March 26, 2012 12:57 AM
    Tuesday, March 20, 2012 8:34 AM
  • what url it gives you? Note that this technique will work only if javascript is added on the page within <script type="text/javascript"> tags. It won't work if it is located in separate .js file. In the last case you need to pass url as parameter:

    // this is on aspx page
    var siteUrl = "<%= SPContext.Current.Web.Url %>";
    ...
    changeLink(siteUrl);
    
    // this is in separate .js file
    function changeLink(siteUrl)
    {
        document.getElementById('anchor').href= siteUrl +"/_vti_bin/ng/activityfeed.svc/Context/";
        document.getElementById('anchor').target="_blank";
    }
    And do not forget semicolon ";" at the end of each line. In your function you missed it.

    Blog - http://sadomovalex.blogspot.com
    CAML via C# - http://camlex.codeplex.com

    Tuesday, March 20, 2012 3:11 PM
  • Every SharePoint page has javascript context variable named '_spPageContextInfo'

    For retrieving current website url you can use 'webServerRelativeUrl' property of this variable:

    var webUrl = _spPageContextInfo.webServerRelativeUrl;
    You can find more info here:
    http://johnliu.net/blog/2012/2/3/sharepoint-javascript-current-page-context-info.html
    Wednesday, March 21, 2012 3:19 AM
  • While _spPageContextInfo is really useful, it is only available since SharePoint 2010, as this is a pre-2010 forum this wouldn't be the right approach.

    If you are using SharePoint 2010 or later then you'd have no excuse not to use this variable.


    jliu - http://johnliu.net - http://sharepointgurus.net

    Wednesday, May 9, 2012 1:11 AM
  • This won't work if I am putting client side code hosted in an HTML file in a document library, which is a very common use case at least for me.

    Friday, March 29, 2013 1:12 AM
  • BROKEN ANSWER

    This does not return the web, as requested by the OP, other than coincidentally. If the page was being run from a document library, for example, "http://people.dev.com/PublicCommunity/projects/proj1/default.aspx" it would return "/PublicCommunity/projects/proj1/" when the web is "/PublicCommunity/".    

    Tuesday, March 24, 2020 9:33 PM
  • To get the current web as requested you may simply use 

        var relUrl = _spPageContextInfo.webServerRelativeUrl;

    Test this by pasting in the console of your fav SP site. 

    Tuesday, March 24, 2020 10:02 PM