2011-04-26 111 views
0

我正在使用的JavaScript函數似乎沒有響應,或者我在某處有不正確的語法。我使用的Parse_URL函數是從http://phpjs.org/functions/parse_url:485XUL使用已解析的URI從當前打開的選項卡打開新選項卡

我希望能夠做到的是;從當前打開的選項卡;採取的URL,解析它,然後將該URL一起傳遞到另一臺服務器上的腳本(在一個新的選項卡)

我設法讓它打開一個新標籤.. 但我嘗試解析URL的每個方法大作短..

這裏是我當前的代碼:

CheckWhois = { 

1: function() { 
//parse this URI 
function parse_url (str, component) { 
// http://kevin.vanzonneveld.net 
// +  original by: Steven Levithan (http://blog.stevenlevithan.com) 
// + reimplemented by: Brett Zamir (http://brett-zamir.me) 
// + input by: Lorenzo Pisani 
// + input by: Tony 
// + improved by: Brett Zamir (http://brett-zamir.me) 
// %   note: Based on http://stevenlevithan.com/demo/parseuri/js/assets/parseuri.js 
// %   note: blog post at http://blog.stevenlevithan.com/archives/parseuri 
// %   note: demo at http://stevenlevithan.com/demo/parseuri/js/assets/parseuri.js 
// %   note: Does not replace invalid characters with '_' as in PHP, nor does it return false with 
// %   note: a seriously malformed URL. 
// %   note: Besides function name, is essentially the same as parseUri as well as our allowing 
// %   note: an extra slash after the scheme/protocol (to allow file:/// as in PHP) 
// *  example 1: parse_url('http://username:[email protected]/path?arg=value#anchor'); 
// *  returns 1: {scheme: 'http', host: 'hostname', user: 'username', pass: 'password', path: '/path', query: 'arg=value', fragment: 'anchor'} 
var key = ['source', 'scheme', 'authority', 'userInfo', 'user', 'pass', 'host', 'port', 
        'relative', 'path', 'directory', 'file', 'query', 'fragment'], 
    ini = (this.php_js && this.php_js.ini) || {}, 
    mode = (ini['phpjs.parse_url.mode'] && 
     ini['phpjs.parse_url.mode'].local_value) || 'php', 
    parser = { 
     php: /^(?:([^:\/?#]+):)?(?:\/\/()(?:(?:()(?:([^:@]*):?([^:@]*))[email protected])?([^:\/?#]*)(?::(\d*))?))?()(?:(()(?:(?:[^?#\/]*\/)*)()(?:[^?#]*))(?:\?([^#]*))?(?:#(.*))?)/, 
     strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))[email protected])?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/, 
     loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/\/?)?((?:(([^:@]*):?([^:@]*))[email protected])?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/ // Added one optional slash to post-scheme to catch file:/// (should restrict this) 
    }; 

var m = parser[mode].exec(str), 
    uri = {}, 
    i = 14; 
while (i--) { 
    if (m[i]) { 
     uri[key[i]] = m[i]; 
    } 
} 

if (component) { 
    return uri[component.replace('PHP_URL_', '').toLowerCase()]; 
} 
if (mode !== 'php') { 
    var name = (ini['phpjs.parse_url.queryKey'] && 
      ini['phpjs.parse_url.queryKey'].local_value) || 'queryKey'; 
    parser = /(?:^|&)([^&=]*)=?([^&]*)/g; 
    uri[name] = {}; 
    uri[key[12]].replace(parser, function ($0, $1, $2) { 
     if ($1) {uri[name][$1] = $2;} 
    }); 
} 
delete uri.source; 
return uri; 
} 
var URI = currentBrowser.currentURI.spec; 

//unsure if this will work 
var tab = document.getElementById("content").addTab("tools.whois.com.au/whois/?domain=" . parse_url("test.com", 'host')); 
document.getElementById("content").selectedTab = tab; 
document.getElementById("urlbar").focus(); 

    }, 

} 

我不習慣JavaScript和這絕對是一個noob問題:)

任何幫助將不勝感激:)

+0

eeh!請澄清,有人願意幫忙 – nemesisfixx 2011-04-26 22:52:37

回答

0

,甚至沒有接觸解析器,這裏有一個問題:

var tab = document.getElementById("content").addTab("tools.whois.com.au/whois/?domain=" . parse_url("test.com", 'host')); 

JavaScript不使用.作爲加法運算符,它採用+。在您的控制檯(CTRL + SHIFT + J)中可能會出現錯誤,它會在您運行腳本時告訴您這一點。

嘗試:

var tab = document.getElementById("content").addTab("tools.whois.com.au/whois/?domain=" + parse_url("test.com", 'host')); 

這是假設parse_url()函數傳遞正確的琴絃。你可以測試:

window.alert(parse_url("test.com", 'host')) 
相關問題