Get absolute base URL


(Simon Cossar) #1

I’m trying to get uploaded audio files to return their absolute URLs. I’m doing this by overwriting the Discourse.Utilities.getUploadMarkdown function, in a plugin. It works, but I’m having to hardcode my site’s base URL into the function and then append it to upload.url. Is there a site setting for this that is available on the client side?

I am currently doing it like this, but there must be a better way.

// javascripts/lib/utilities.js

...

if (Discourse.Utilities.isAudioFile(upload.original_filename)) {
      return  'http://' + Discourse.BaseUrl + upload.url;
    } else {

...

 isAudioFile: function(path) {
    return (/\.(mp3|wav|ogg)$/i).test(path);
  },

(Jeff Atwood) #2

I think @zogstrip would be the best person to answer this.


(Simon Cossar) #3

It turn out you can always get the absolute url from window.location.


(Régis Hanol) #4

window.location is nice but only works on the client-side. If you want it to work on the server-side too, Discourse.BaseUrl is what you’re looking for.


(Simon Cossar) #5

Yeah, except it doesn’t return the protocol part of the URL.


(Régis Hanol) #6

You can use the Discourse.use_https site setting to know that :wink: