0

我需要从a元素中调用 Javascript 函数.. 像这样:

href='javascript:myjavascriptfunction();'. 我需要 Javascript 函数从几个 datepicker 控件中获取值,构建和 urlencode 查询字符串并将其返回,以便它在新窗口中打开。到目前为止,我认为我拥有一切,但我正在努力构建 URL 并正确返回它。a到目前为止,这是我的元素。

<a href='javascript:myjavascriptfunction();' id="myhyperlink" target="_blank">

在我的 Javascript 函数中,我正在构建字符串并像这样返回它:

return queryString;

结果:单击链接并打开一个新窗口,其中包含我的父窗口的 URL,并附加了函数调用名称?

4

2 回答 2

1

你需要使用 window.open() 来强制一个新窗口,我相信它表现得像你的唯一原因是你告诉它在新窗口中打开那个javascript,它正在做你正在做的事情告诉它。

在你得到你的 url 组装后,做一个window.open(urlvar);,你应该得到你正在寻找的东西。

另外,查看这篇文章,了解如何通过<a href="">优雅降级的 onclick 进行整理。至少,做href="javascript:void(0)",然后onclick="myjavascriptfunction(); return false;"

于 2009-11-07T02:46:55.107 回答
0

JavaScript 函数的返回值不能作为链接的 HREF 提供给浏览器。为了使这项工作如您所愿,您必须在 JavaScript 中获取 A 元素并干预其 HREF。这是可能的:

 var link = document.getElementById ("myhyperlink");
 link.href = "http://example.com/" + jiggerypokery ();

jiggerypokery ()您想创建的任何 URL 都在哪里。

However the above is stupid, since it overwrites the link to itself, and the user would probably have to click on it twice anyway. As Pablo says you should be using window.open here, so in myjavascriptfunction ()

var gotopage = "http://example.com/" + jiggerypokery ();
window.open (gotopage);
于 2009-11-07T03:03:33.430 回答