Para possivelmente vos poupar 20min (isto se googlarem antes de meter mãos à obra), deixo abaixo um pedaço de código em JavaScript que permite reescrever as variáveis passadas pelo URL.
O Princípio é básico: doURL recebe uma string no formato variavel1=valor1&variavel2=valor2 (com um número variável de variáveis) actualizando aquelas que existirem no URL da página onde é executado e adicionando as demais. Formado o novo URL o browser é redireccionado com a nova query string.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | <script type="text/javascript"> /* Script Name: Script Version: 1.0 Script Date: 24/November/2009 Author Name: Paulo A. Silva Author Email: pauloasilva[at]gmail[dot]com Author Website: http://www.pauloasilva.com */ /* Transfroms an URL query string (var1=val1&var2=val2) into an associative array, indexed by variable name */ function str2Array (str) { // first split by '&' returning an array of strings var FSplit = str.split('&'); // associative array which will be returned var Args = new Array(); for(var i=0;i<FSplit.length;i++) { var kv = FSplit[i].split('='); Args[ kv[0] ] = kv[1]; } return Args; } /* Transforms an associative array into an URL query string */ function array2SrchStr (Array) { var str = ""; for (var arg in Array) str += "&" + arg +"="+ Array[arg]; return "?"+ str.substr(1); } /* Merges source into destination return the second one */ function merge (destination,source) { for (var property in source) destination[property] = source[property]; return destination; } function doURL (aditional) { // prepare aditional string var AdArgs = str2Array(aditional); var currentURL = document.location; //remove lead '?' var str = currentURL.search.substr(1); // prepare actual search string var ActualArgs = str2Array(str); // merge new aditional args merge(ActualArgs,AdArgs); // get a valid URL search string str = array2SrchStr(ActualArgs); // prepare destination URL and redirect the browser with (currentURL) { var newURL = protocol +"//" + hostname; newURL += (port != '')? ":"+ port : ""; newURL += pathname + str; document.location = newURL; } } </script> |
Comentários Recentes