首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XUL从当前打开的选项卡中打开带有已解析URI的新选项卡

XUL从当前打开的选项卡中打开带有已解析URI的新选项卡
EN

Stack Overflow用户
提问于 2011-04-26 11:00:15
回答 1查看 614关注 0票数 0

我正在使用的javascript函数似乎没有响应,或者我在某个地方有错误的语法。我使用的Parse_URL函数来自http://phpjs.org/functions/parse_url:485

我想要做的是;从当前打开的选项卡中,获取URL,解析它,然后将该url传递给另一个服务器上的脚本(在新的选项卡中)

我设法让它打开了一个新的标签..但是我尝试解析URL的每一种方法都失败了。

下面是我当前的代码:

代码语言:javascript
复制
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:password@hostname/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: /^(?:([^:\/?#]+):)?(?:\/\/()(?:(?:()(?:([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?()(?:(()(?:(?:[^?#\/]*\/)*)()(?:[^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
        strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
        loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/\/?)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\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问题:)

任何帮助都将不胜感激:)

EN

回答 1

Stack Overflow用户

发布于 2011-05-08 01:16:12

在不接触解析器的情况下,这里有一个问题:

代码语言:javascript
复制
var tab = document.getElementById("content").addTab("tools.whois.com.au/whois/?domain=" . parse_url("test.com", 'host'));

javascript不使用.作为加法运算符,它使用+。可能在您的控制台(CTRL+SHIFT+J)中有一个错误,在您运行脚本时会告诉您这一点。

尝试:

代码语言:javascript
复制
var tab = document.getElementById("content").addTab("tools.whois.com.au/whois/?domain=" + parse_url("test.com", 'host'));

这是假设parse_url()函数传递了正确的字符串。您可以使用以下命令进行测试:

代码语言:javascript
复制
window.alert(parse_url("test.com", 'host'))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5785443

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档