我需要在 javascript 函数中构建我的 url,然后将字符串返回到 href

发布于 2024-08-10 19:48:26 字数 513 浏览 9 评论 0原文

我需要从 a 元素调用 Javascript 函数......如下所示:

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

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

在我的 Javascript 函数中,我正在构建字符串并返回它,如下所示:

return queryString;

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

I need to call a Javascript function from an a element.. something like this:

href='javascript:myjavascriptfunction();'. I need the Javascript function to get the values from a couple of datepicker controls, build and urlencode a querystring and return it so that it opens into a new window. So far I think I have everything, but I'm struggling with building the URL and returning it correctly. This is my a element so far.

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

In my Javascript function I am building the string and returning it like this:

return queryString;

Result: click the link and a new window opens with the URL of my parent window with the function call name appended to it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

还不是爱你 2024-08-17 19:48:27

您需要使用 window.open() 强制打开一个新窗口,我相信它的行为适合您的唯一原因是您告诉它在新窗口中打开该 javascript,并且它正在做您正在做的事情告诉它。

组装好 url 后,执行 window.open(urlvar);,您应该会得到您想要的内容。

另外,请查看这篇文章了解如何制作整洁的< ;a href=""> 具有优雅降级的 onclick。至少,执行 href="javascript:void(0)",然后执行 onclick="myjavascriptfunction(); return false;"

You need to use window.open() to force a new window, I believe the only reason it's behaving like it is for you is you're telling it to open that javascript in a new window, and it's doing exactly what you're telling it.

After you get your url assembled, do a window.open(urlvar); and you should get what you're looking for.

Also, check out this post for how to make a tidy <a href=""> with an onclick that degrades gracefully. At the minimum, do href="javascript:void(0)" and then onclick="myjavascriptfunction(); return false;".

谢绝鈎搭 2024-08-17 19:48:27

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

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

其中 jiggerypokery () 是您想要创建的任何 URL。

然而,上面的内容很愚蠢,因为它覆盖了自身的链接,并且用户可能无论如何都必须单击它两次。正如 Pablo 所说,你应该在这里使用 window.open ,所以在 myjavascriptfunction ()

var gotopage = "http://example.com/" + jiggerypokery ();
window.open (gotopage);

The return value of a JavaScript function is not available to the browser as the HREF for the link. In order to make this work as you intend, you would have to get the A element in JavaScript and meddle with its HREF. That is possible:

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

where jiggerypokery () is whatever URL you want to make.

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文