splitUrl() simplifies URL manipulation

Here’s a small UDF to split any URL into it’s base, query string, and anchor parts. Hope you find it useful.


function splitUrl(inUrl) {

var s = inUrl;

var refUrl = "";
var refQS = "";
var refAnchor = "";

var st = structNew();

var i = find("?", s);
if (i) {
refUrl = left(s, i-1);
refQS = mid(s, i+1, 99999);

i = find("##", refQS);
if (i) {
refAnchor = mid(refQS, i+1, 99999);
refQS = left(refQS, i-1);
} else {
refAnchor = "";
}
} else {

i = find("##", s);
if (i) {
refUrl = left(s, i-1);
refAnchor = mid(s, i+1, 99999);
} else {
refUrl = s;
}
}

st.url = refUrl;
st.queryString = refQS;
st.anchor = refAnchor;

return st;
}

18 thoughts on “splitUrl() simplifies URL manipulation

  1. Had a request in the MM forums for a few more details, so I added on to your function. This returns Anchor, Domain, Path, Querystring and URL

    function splitUrl(inUrl) {

    var s = inUrl;

    var refUrl = “”;
    var refQS = “”;
    var refAnchor = “”;

    var st = structNew();

    var i = find(“?”, s);
    if (i) {
    refUrl = left(s, i-1);
    refQS = mid(s, i+1, 99999);

    i = find(“##”, refQS);
    if (i) {
    refAnchor = mid(refQS, i+1, 99999);
    refQS = left(refQS, i-1);
    } else {
    refAnchor = “”;
    }
    } else {

    i = find(“##”, s);
    if (i) {
    refUrl = left(s, i-1);
    refAnchor = mid(s, i+1, 99999);
    } else {
    refUrl = s;
    }
    }

    if (find(“?”, s) and Find(“/”,inUrl,9)) {
    st.path = Mid(inUrl,Find(“/”,inUrl,9),find(“?”, s)-Find(“/”,inUrl,9));
    } else if (find(“?”, s)) {
    st.path = “/”;
    } else {
    st.path = RemoveChars(inUrl,1,Find(“/”,inUrl,9));
    }

    st.domain = Left(refUrl,find(“/”,inUrl,9)-1);

    st.url = refUrl;
    st.queryString = refQS;
    st.anchor = refAnchor;

    return st;
    }

  2. By Philip Pullella and Thomas Grove. ISTANBUL, Turkey (Reuters) – Pope Benedict embarked on Wednesday on his second mission in Turkey, to improve ties between Roman Catholics and Orthodox Christians, after …
    gym-class-heroes

  3. Your guestbook is example of middle-class guestbooks. Congratulation! I’ll show your site and guestbook to my friends.d

  4. ???????????? ? ???? ????,??????? ???????????? ? ????,??????? ???????????? ? ???? ???????,?????? ???????????? ? ????,?????? ???????????? ? ???? ???????,???????????? ???? ???? ??????,???????????? ? ???? ???? ??????,???????? ?????????? ????,???????? ???? ?????????? ????,??????? ????? ? ????,??????? ????? ? ???? ??? ????????,??????????? ????? ? ????,??????????? ????? ? ???? ????

  5. ???????????? ? ???? ????,??????? ???????????? ? ????,??????? ???????????? ? ???? ???????,?????? ???????????? ? ????,?????? ???????????? ? ???? ???????,???????????? ???? ???? ??????,???????????? ? ???? ???? ??????,???????? ?????????? ????,???????? ???? ?????????? ????,??????? ????? ? ????,??????? ????? ? ???? ??? ????????,??????????? ????? ? ????,??????????? ????? ? ???? ????

  6. ???????????? ? ???? ????,??????? ???????????? ? ????,??????? ???????????? ? ???? ???????,?????? ???????????? ? ????,?????? ???????????? ? ???? ???????,???????????? ???? ???? ??????,???????????? ? ???? ???? ??????,???????? ?????????? ????,???????? ???? ?????????? ????,??????? ????? ? ????,??????? ????? ? ???? ??? ????????,??????????? ????? ? ????,??????????? ????? ? ???? ????

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>