我需要在 javascript 函数中构建我的 url,然后将字符串返回到 href
我需要从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要使用 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, dohref="javascript:void(0)"
and thenonclick="myjavascriptfunction(); return false;"
.JavaScript 函数的返回值不可作为链接的 HREF 提供给浏览器。为了使这项工作按您的预期进行,您必须获取 JavaScript 中的 A 元素并干预其 HREF。这是可能的:
其中
jiggerypokery ()
是您想要创建的任何 URL。然而,上面的内容很愚蠢,因为它覆盖了自身的链接,并且用户可能无论如何都必须单击它两次。正如 Pablo 所说,你应该在这里使用
window.open
,所以在myjavascriptfunction ()
中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:
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 inmyjavascriptfunction ()