Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Apr 2011
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    get external id from url

    hi there,

    any body knows how to help me get started on how to get the external id of a specific url? like if the site is www.test.com/article/external id=49603/ufgk59nsn/

    also after extracting, i would like to add another site with the extracted external id like www.id.com/key/49603

    can this be possible?

    i'll place the URL www.test.com/article/external id=49603/ufgk59nsn/ on a textarea and the output would be in another textarea.

    i hope i dont sound confusing

    thanks

  • #2
    Senior Coder DanInMa's Avatar
    Join Date
    Nov 2010
    Location
    Salem,Ma
    Posts
    1,577
    Thanks
    13
    Thanked 248 Times in 248 Posts
    cant take credit for the code so Ill jsut incldue the link : http://papermashup.com/read-url-get-...ithjavascript/

  • #3
    New Coder
    Join Date
    Apr 2011
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts
    hey DanInMa,

    thanks for the quick reply...ok so i got this from the link you have provided.
    Code:
    function getUrlVars() {
        var vars = {};
        var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
            vars[key] = value;
        });
        return vars;
    }
    so how am I going to call this function in html form?

    regards,

  • #4
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,335
    Thanks
    11
    Thanked 588 Times in 569 Posts
    i modified the function to work with non-location strings.

    usage is pretty simple, you just need to read and write the form values.


    Code:
    <html>
    IN:  <textarea id=ta1 ></textarea>	 <br />
    OUT: <textarea id=ta2 ></textarea>	<br />
    <button id=btnGo>Extract!</button>	
    
    <script>
    function getUrlVars(u) {
        var vars = {};
        var parts = u|| window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
            vars[key] = value;
        });
        return vars;
    }
    
    btnGo.onclick=function(){
       ta2.value=getUrlVars(ta1.value).id;
    }
    
    </script>
    </html>
    Last edited by rnd me; 04-10-2013 at 04:13 PM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #5
    New Coder
    Join Date
    Apr 2011
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts
    hey rndme,

    just tried your sample code but it always returns an "undefined" output value whenever i paste the link on the "IN" textarea. anything wrong with the code? it should basically get the external id and add a part of an url.

  • #6
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,335
    Thanks
    11
    Thanked 588 Times in 569 Posts
    Quote Originally Posted by verve View Post
    hey rndme,

    just tried your sample code but it always returns an "undefined" output value whenever i paste the link on the "IN" textarea. anything wrong with the code? it should basically get the external id and add a part of an url.
    oops, only-half-converted the supplied function.
    sorry about that, i got pulled away from my post and didn't have time to test it.

    just needed four more chars of syntax:
    tested:
    Code:
    <html>
    IN:  <textarea id=ta1 ></textarea>	 <br />
    OUT: <textarea id=ta2 ></textarea>	<br />
    <button id=btnGo>Extract!</button>	
    
    <script>
    function getUrlVars(u) {
        var vars = {};
        var parts = (u|| window.location.href).replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
            vars[key] = value;
        });
        return vars;
    }
    
    btnGo.onclick=function(){
       ta2.value=getUrlVars(ta1.value).id;
    }
    
    </script>
    </html>
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #7
    New Coder
    Join Date
    Apr 2011
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts
    rndme,

    not sure what im doing wrong...just copied and pasted your coding in a notepad and save as getid.html however the result still remains "undefined" sorry for the pain

  • #8
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,043
    Thanks
    0
    Thanked 251 Times in 247 Posts
    It would be good to parse the inputted url string to ensure validity of url format entered.

    Code:
    function getUrlVars(u) {
        var vars = {}, qs = parseUrl((u || location.href)).search;
        var parts = qs.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
            vars[key] = value;
        });
        return vars;
    }
    
    function parseUrl(url) {
        var a = document.createElement('a');
        a.href = url;
        return a; //caller can now get the individual parts of the url - protocol, host, hostname, port, pathname, search, and hash
        //or we can pass back all those info only instead of the entire members of the a object.
        //return {protocol: a.protocol, host: a.host, port: a.port, pathname: a.pathname, search: a.search, hash: a.hash};
    }

  • #9
    New Coder
    Join Date
    Apr 2011
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts
    hi glenn,

    tried your codes however, still i get an "undefined" message output whenever i click the extract button....hmm what am i missing here? i just simply substituted rndme's code with your code however there is no external id extracted together with the out put that i wish to do..

  • #10
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,335
    Thanks
    11
    Thanked 588 Times in 569 Posts
    Quote Originally Posted by verve View Post
    rndme,

    not sure what im doing wrong...just copied and pasted your coding in a notepad and save as getid.html however the result still remains "undefined" sorry for the pain
    hmm, it tested fine in firefox for me. maybe there's something else blocking it's execution. post the html file from <head> to </body> and we'll take a look.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •