在JS函数中传递URL作为参数

时间:2013-09-18 13:49:59

标签: javascript jquery

这是我的代码:

var url = f.href.value;
ed.dom.setAttribs(e, {
    href : 'javascript:void(0)',
    onclick: 'doSomething(' + url + ')'
});

doSomething方法:

function doSomething(url) {
    windowMy = dhxWins.createWindow("externalLink", 10, 10, 630, 630);
    windowMy.setText("Title");
    windowMy.attachURL(url);
}

当调用doSomething时,我收到错误:Uncaught SyntaxError:Unexpected token :, url =“http:// somewebsite / site”。

我该怎么办?如何在JS函数中传递url作为参数?

1 个答案:

答案 0 :(得分:6)

我认为你的网址周围需要一些额外的引号:

var url = f.href.value;
ed.dom.setAttribs(e, {
    href : 'javascript:void(0)',
    onclick: 'doSomething("' + url + '")'
});

否则,该函数最终会像这样:

doSomething(http://www.google.com);

而不是:

doSomething("http://www.google.com");