Created
September 9, 2011 10:21
-
-
Save aerith/1205897 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jQuery.fn.postize = function (option) { | |
var buildData = function (query) { | |
var queries = query.split(/&/); | |
var result = {}; | |
for (var i = 0, l = queries.length; i < l; i++) { | |
var pieces = queries[i].split(/=/, 2); | |
var name = decodeURIComponent(pieces[0]) | |
var value = decodeURIComponent(pieces[1]); | |
value = value ? !isNaN(value) ? Number(value) : value : '' ; | |
result[name] = value; | |
} | |
return result; | |
}; | |
return this.each(function () { | |
var self = jQuery(this); | |
var target = self.attr('href'); | |
if (self.is('form')) { | |
self.attr('action', 'POST'); | |
return; | |
} | |
if (!target) return; | |
self.click(function (event) { | |
event.stopPropagation(); | |
event.preventDefault(); | |
var data = null; | |
if ("query_replaces" in option && Boolean(option.query_replaces)) { | |
var pieces = target.split('?'); | |
target = pieces[0]; | |
data = buildData(pieces[1]); | |
} | |
var form = jQuery('<form method="post" />') | |
.css({ width: '1px', height: '1px', top: '-1000px', left: '-1000px', position: 'absolute' }) | |
.attr('action', target) | |
.appendTo(document.body); | |
if ("params" in option) { | |
for (var key in option.params) { | |
jQuery('<input type="hidden" />') | |
.attr('name', key) | |
.val(option.params[key]) | |
.appendTo(form); | |
} | |
} | |
if (data !== null) { | |
for (var key in data) { | |
jQuery('<input type="hidden" />') | |
.attr('name', key) | |
.val(data[key]) | |
.appendTo(form); | |
} | |
} | |
if ("data_names" in option && jQuery.isArray(option.data_names)) { | |
var names = option.data_names; | |
for (var i = 0, l = names.length; i < l; i++) { | |
var name = names[i] | |
jQuery('<input type="hidden" />') | |
.attr('name', name) | |
.val(self.attr('data-' + name)) | |
.appendTo(form); | |
} | |
} | |
form.submit(); | |
return false; | |
}); | |
return this; | |
}); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.3/jquery.min.js"></script> | |
<script type="text/javascript" src="https://raw.github.com/gist/1205897/jquery.postize.js"></script> | |
</head> | |
<a href="http://example.com/?created_at=2011-01-01" data-user_id="1">test</a> | |
<script type="text/javascript"> | |
$('a').postize({ data_names: ['user_id'], params: { username: 'who' }, query_replaces: true }) | |
</script> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment