User Tools

Site Tools


snippets:javascript

Populate <select> from JSON with jQuery

Using a returned JSON like this:

[{"id":1,"name":"PHP"},{"id":2,"name":"CSS"},{"id":3,"name":"Javascript"}]
$('#parentSelectId').change(function {
    $.getJSON('http://example.com/ws/functionName?callback=?', function (data) {
        var select = $('#childrenSelectId');
        var options = select.prop ? select.prop('options') : select.attr('options') ;
        $('option:not(:first)', select).remove();
 
        $.each(data, function (index, array) {
            options[options.length] = new Option(array.name, array.id);
        });
    });
});
User script: Add facebook tab on a facebook page

Call this from any facebook app page. The secure page url must be set.

fb-page-tab.user.js
// ==UserScript==
// @name           Facebook Add Page Tab
// @description    Insert a link to add a page tab on facebook
// @namespace      dan
// @include        htt*://*.facebook.com/*
// @match          https://developers.facebook.com/apps/*
// @exclude        htt*://*channel*.facebook.com*
// @exclude        htt*://*static*.facebook.com*
// @exclude        htt*://upload.facebook.com/*
// @exclude        htt*://*.facebook.com/ajax/*
// ==/UserScript==
 
var appId = location.pathname.split('/')[2];
var appUrl = document.getElementById('secure_page_tab_url').value;
var headMenu = document.getElementById('headerArea');
 
var link = document.createElement('a');
link.setAttribute('href', 'https://www.facebook.com/dialog/pagetab?app_id=' + appId + '&next=' + appUrl);
link.setAttribute('target', '_blank');
link.appendChild(document.createTextNode('Agregar Tab'));
 
headMenu.appendChild(link);

Bookmarklet:

javascript:(function(){var appId=location.pathname.split('/')[2];var appUrl=document.getElementById('secure_page_tab_url').value;var headMenu=document.getElementById('headerArea');if(appUrl==''){alert('Debes llenar el campo de "URL segura de la pesta%C3%B1a"');return false;}var link=document.createElement('a');link.setAttribute('href','https://www.facebook.com/dialog/pagetab?app_id='+appId+'&next='+appUrl);link.setAttribute('target','_blank');link.appendChild(document.createTextNode('Agregar Tab a Pagina'));headMenu.appendChild(link);})();
snippets/javascript.txt · Last modified: 2015/12/11 17:16 by osiux