1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Base URL for routes via JavaScript

Discussion in 'XenForo Development Discussions' started by digitalpoint, Nov 6, 2011.

  1. digitalpoint

    digitalpoint Well-Known Member

    I tried finding an existing variable, but I couldn't find one...

    Is there a JavaScript variable set somewhere that is the current base URL for routes? For example... with rewritten friendly URLs enabled, it would be "http://xenforo.com/community/" here... without mod_rewrite going, it would be "http://xenforo.com/community/index.php?"

    Want to make an AJAX request initiated from a JS file, but want to make sure the request is going to the right place...
     
  2. Rigel Kentaurus

    Rigel Kentaurus Well-Known Member

    I think that is mostly not needed because javascript does not build the URL, instead, it is embedded in the page itself (which uses the link building mechanism). I am doing a similar thing in my addons, for example ...

    I added a URL in my postbit, that is
    Code:
    <a class="dpclass" href="{xen:link digitalpoint/whatever $data}">....</a>
    
    And inside my javascript code, I bind to the click event ....
    Code:
    !function($, window, document, _undefined) {
    
        XenForo.DPClass = function($link) {
            $link.click(function(e) {
                e.preventDefault();
                XenForo.ajax($link.attr('href'), {}, callback);
            });
        };
        
        XenForo.register('a.dpclass', 'XenForo.DPClass');
    
    }(jQuery, this, document);
    
    What that would do ... is that if somebody clicks on the link, the action would not get executed, but instead, an AJAX call would be made, to the href of the link
     
  3. Rigel Kentaurus

    Rigel Kentaurus Well-Known Member

    If that is not useful to you .... I guess you can try embedding in your script (before you include your own javascript, most likely)
    Code:
    <script>
    var ajaxUri = '{xen:link digitalpoint/whatever $data}';
    </script>
    
    .... and I guess you could also do
    Code:
    <script>
    var baseUri = '{xen:link canonical:index}';
    </script>
    
     
  4. digitalpoint

    digitalpoint Well-Known Member

    Well, this particular AJAX call isn't triggered from a link, click or anything else on the page. I've been skimming through the xenforo.js file, and it looks like it uses hard-coded "safe" URLs with the "index.php?" in it...

    For example:

    Code:
    location = 'index.php?logout/&_xfToken=' + XenForo._csrfToken;
    Code:
    					window.location = XenForo.canonicalizeUrl(
    						'index.php?register/facebook&t=' + escape(session.access_token)
    						+ '&redirect=' + escape(window.location)
    					);
    etc...

    I guess I'll do the same unless someone tells me a better way...
     
  5. ragtek

    ragtek Guest

    digitalpoint likes this.
  6. vbresults

    vbresults Well-Known Member

    HTML:
    $('base').attr('href');
     
  7. James

    James Well-Known Member

    XenForo does specify a base URL:

    HTML:
    <base href="http://xenforo.com/community/" />
    <script>
    	var _b = document.getElementsByTagName('base')[0], _bH = "http://xenforo.com/community/";
    	if (_b && _b.href != _bH) _b.href = _bH;
    </script>
    
    not sure if it's of use to you though.
     
  8. Robbo

    Robbo Well-Known Member

    You shouldn't need it in JavaScript. If you are working with markup it should be done through the templates and passed through an ajax call. If you need to build the URL then the ajax call can specify any parameters needed and then return the URL.

    Sounds like a design problem in your JS handling imo. Or, well, using something that you would use in another App but XenForo is designed differently.
     
  9. digitalpoint

    digitalpoint Well-Known Member

    Yeah, not really since it's not a base URL for a *route* (ie. it will never have index.php? in it depending on the URL type structure you have.

    It's not done through markup, nor is it triggered by any action by a user so nothing about it exists within templates.

    It's not really a design issue... XenForo has core functionality that does calls that are initiated from the JavaScript files themselves (like I mentioned earlier in this thread). They simply always use "index.php?" for those URLs (even if the forum URL's aren't "normally" setup that way).

    For example part of the logout process within the JS files always redirects users to http://www.yoursite.com/index.php?logout/

    Code:
    location = 'index.php?logout/&_xfToken=' + XenForo._csrfToken;
    AJAX does not always need to be initiated from a user action (like a click) or from the HTML of the page you happen to be looking at currently (it's common they DO, but it's certainly not a requirement).
     
    1 person likes this.

Share This Page