URL encoding in Javascript
  • timrogerstimrogers September 2010

    In the API documentation, it says that I need to encode URLs for the "uri" parameter of send_notification? What kinds of URL encoding does this support, and how can I encode in the correct way in Javascript?

    Thanks,

    Tim

  • StammyStammy September 2010

    I have been using encodeURIComponent for that. But I'll do ya one better, here's part of the function I wrote and have been using for our Chrome to Notifo extension:

    creds is just "username:apikey" that I did a creds.split(":") on earlier. i have the service event ("title") hardcoded in this function.

    or - with formatting: http://gist.github.com/raw/564058/4df889c0dd4ddd10d338b09b6dfcb356da962543/gistfile1.txt

    function sendNotifo(label,msg,uri) {
    var https = new XMLHttpRequest();
    var creds = localStorage.getItem("notifo");
    var auth = "Basic " + Base64.encode(creds);
    var push = 'to=' + encodeURIComponent(creds[0]) + '&msg=' + encodeURIComponent(msg) + '&uri=' + encodeURIComponent(uri) + '&label=' + encodeURIComponent(label) + '&title=' + encodeURIComponent("Page");
    https.onreadystatechange = function() {
    if (this.readyState == 4)
    switch (https.status) {
    case 200:
    //Success do what you want here
    break;
    default:
    // Error - JSON.parse(https.responseText)['response_message'];
    break;
    }
    };
    https.open('POST', 'https://api.notifo.com/v1/send_notification', true);
    https.setRequestHeader('Authorization', auth);
    https.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    https.send(push);
    }

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In Apply for Membership

In this Discussion