Skip to content

Commit

Permalink
support JSON-P in loadJSON()
Browse files Browse the repository at this point in the history
Example:
  loadJSON(url, function(json){ ... }, { jsonp: true })

Other changes:
  - ajax() function now supports URL instances as well as
    strings for the `url` parameter
  - added URL methods: `external`, `absolutize`, `addQuery`
  - `URL#domain` property renamed to `URL#host`
  • Loading branch information
mislav committed Jul 1, 2009
1 parent 1661226 commit 77c33a7
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 23 deletions.
4 changes: 2 additions & 2 deletions toolkit/analytics.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ function applyAnalytics($, gat, account) {
} }
$.trackPageview = function(path) { $.trackPageview = function(path) {
if (path) { if (path) {
var url = (path instanceof URL) ? path : new URL(path) var url = new URL(path)
path = url.pathWithQuery() path = url.pathWithQuery()
if (url.domain && url.domain != 'twitter.com') path = '/' + url.domain + '/' + path if (url.external()) path = '/' + url.host + path
} }
try { pageTracker._trackPageview(path) } catch(err) {} try { pageTracker._trackPageview(path) } catch(err) {}
} }
Expand Down
74 changes: 53 additions & 21 deletions toolkit/toolkit.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -159,10 +159,7 @@ function ajax(params) {


params = extend(defaults, params) params = extend(defaults, params)
params.headers = extend(defaultHeaders, params.headers || {}) params.headers = extend(defaultHeaders, params.headers || {})

params.url = new URL(params.url).absolutize().toString()
if (!/^https?[:]/.test(params.url)) {
params.url = window.location.protocol + '//' + window.location.host + params.url
}


if (typeof params.data == 'object') { if (typeof params.data == 'object') {
params.data = objectToQueryString(params.data) params.data = objectToQueryString(params.data)
Expand All @@ -173,24 +170,41 @@ function ajax(params) {
} }


function loadJSON(url, onload, params) { function loadJSON(url, onload, params) {
params = extend({ url: url, onload: onload }, params || {}) url = new URL(url)
var handler = params.onload


params.onload = function(response) { if (params.jsonp) {
if (typeof response.getResponseHeader == 'function') { var head = document.getElementsByTagName('head')[0],
// native XMLHttpRequest interface script = document.createElement('script'),
var responseType = (response.getResponseHeader('Content-type') || '').split(';')[0] jsonp = ('string' == typeof params.jsonp) ? params.jsonp : '_callback',
} else { callback = 'loadJSON' + (++loadJSON.$uid)
// GM_xmlhttpRequest interface
var responseType = (response.responseHeaders.match(/^Content-[Tt]ype:\s*([^\s;]+)/m) || [])[1] window[callback] = function(object) {
} onload(object)
if (responseType == 'application/json' || responseType == 'text/javascript') { window[callback] = null
var object = eval("(" + response.responseText + ")") }
if (object) handler(object, response) script.src = url.addQuery(jsonp + '=' + callback)
head.appendChild(script)
} else {
params = extend({ url: url, onload: onload }, params || {})
var handler = params.onload

params.onload = function(response) {
if (typeof response.getResponseHeader == 'function') {
// native XMLHttpRequest interface
var responseType = (response.getResponseHeader('Content-type') || '').split(';')[0]
} else {
// GM_xmlhttpRequest interface
var responseType = (response.responseHeaders.match(/^Content-[Tt]ype:\s*([^\s;]+)/m) || [])[1]
}
if (responseType == 'application/json' || responseType == 'text/javascript') {
var object = eval("(" + response.responseText + ")")
if (object) handler(object, response)
}
} }
return ajax(params)
} }
return ajax(params)
} }
loadJSON.$uid = 0


function strip(string) { function strip(string) {
return string.replace(/^\s+/, '').replace(/\s+$/, '') return string.replace(/^\s+/, '').replace(/\s+$/, '')
Expand Down Expand Up @@ -246,11 +260,15 @@ function positionCursor(field, start, end) {
} }


function URL(string) { function URL(string) {
var match = string.match(/(?:(https?):\/\/([^\/]+))?([^?]*)(?:\?(.*))?/) if (string instanceof URL) return string

var match = string.match(/(?:(https?:)\/\/([^\/]+))?([^?]*)(?:\?([^#]*))?(?:#(.*))?/)
string = match[0] string = match[0]
this.domain = match[2] this.protocol = match[1]
this.host = match[2]
this.path = match[3] this.path = match[3]
this.query = match[4] this.query = match[4]
this.hash = match[5]


this.toString = function() { this.toString = function() {
return string return string
Expand All @@ -259,4 +277,18 @@ function URL(string) {


URL.prototype.pathWithQuery = function() { URL.prototype.pathWithQuery = function() {
return this.path + (this.query ? '?' + this.query : '') return this.path + (this.query ? '?' + this.query : '')
} }
URL.prototype.external = function() {
return this.host && (this.host != window.location.host ||
this.protocol != window.location.protocol)
}
URL.prototype.absolutize = function() {
if (this.host) {
return this
} else {
return new URL(window.location.protocol + '//' + window.location.host + this)
}
}
URL.prototype.addQuery = function(string) {
return new URL(this.toString() + (this.query ? '&' : '?') + string)
}

0 comments on commit 77c33a7

Please sign in to comment.