treehouse : what would you like to learn today?
Web Design Web Development iOS Development

[Java Script Help] URL to Root

  • Hi there, i'm not similar with js, i want to set url to root (of course without write domain name) is there a way?

    look at my e.g:

    AudioPlayer.embed("audioplayer_1", {soundFile: "http://css-tricks.com/Player/player.mp3"});
    

    i want to set address to root folder instead url (domain name)

    something like this:

    AudioPlayer.embed("audioplayer_1", {soundFile: "~/Player/player.mp3"});
    
  •  AudioPlayer.embed("audioplayer_1", {soundFile: "/Player/player.mp3"});
    

    doesn't work? I don't know about the AudioPlayer but normally starting an url with / means it's relative to root.

  • it does, but the problem is audio player normally works with DOMAIN URL, not path address. but i'm working on a MOD for audio player, need to use a global script (or url) to root, look at the path of this mod:

    Root/Player/Song.mp3

    Root/Themes/Default/Scipts/Player.js

    so that embed script placed into player.js , and i can't write a certain url, cause i don't know the all people url :) , with php we write something like this:

    global $siteurl; echo '
    src="'$siteurl'/player/Song.mp3
    

    but i need to do this via java script, cause audio player based on JS. so, i need a global call script to root, or domain name, for eveyone, not only myselft, that mean, when using of this MOD, it load songs from Root, and of course i can't tell the people, edit it manually your self.

  • I didn't use 'Root' in my example. You might wanna try this window.location.hostname but I'm not sure about browser compatibility.

  • Important is how it should placed in link, is it correct?

    AudioPlayer.embed("audioplayer_1", {soundFile: "window.location.hostname/Player/player.mp3"});
    

    or must placed between '' or "" , i don't know anyway, it's not working

  • If that's in JavaScript its more like {soundFile: window.location.hostname + "/Player/player.mp3"} or something. Can you make a codepen?

  • You can get the root location very easily, however because IE is a pain in the neck you'll need to check both location.hostname and location.host. Here is a function that will take care of that for you.

    function getLocation() {
      var hostname = document.location.hostname,
      host = document.location.host;
      return hostname == '' ? host == '' ? '' : host : hostname;
    }
    

    To use this you would do the following.

    AudioPlayer.embed("audioplayer_1", {
      soundFile: getLocation() + "/Player/player.mp3"
    });
    

    Hope this helped.

  • Thanks for reply, but not working neither, it's weird, when i add this code, player disappeared. try it on my code pen :(

    http://codepen.io/Pedram/pen/rslKG