Grow your CSS skills. Land your dream job.

[Java Script Help] URL to Root

  • # March 13, 2013 at 9:02 pm

    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″});

    # March 13, 2013 at 9:10 pm

    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.

    # March 14, 2013 at 6:23 am

    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.

    # March 14, 2013 at 9:05 am

    I didn’t use ‘Root’ in my example. You might wanna try this [`window.location.hostname`](https://developer.mozilla.org/en-US/docs/DOM/window.location) but I’m not sure about browser compatibility.

    # March 15, 2013 at 5:57 am

    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

    # March 15, 2013 at 7:45 am

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

    # March 15, 2013 at 1:02 pm

    Sure, here:

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

    # March 15, 2013 at 3:44 pm

    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.

    # March 15, 2013 at 4:58 pm

    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

Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".