admin管理员组文章数量:1332374
I have a link in my page.
<a onclick="Click()">Click me</a>
When I click this link, fires onclick event that call Click function. In this function, I have some javascript code and then, I want to redirect to another page.
function Click() {
//some code
window.location = url;
}
When user right click on link and select "Open in new tab", new tab opens, and url is: "about:blank".
How to I correct this? Thanks.
I have a link in my page.
<a onclick="Click()">Click me</a>
When I click this link, fires onclick event that call Click function. In this function, I have some javascript code and then, I want to redirect to another page.
function Click() {
//some code
window.location = url;
}
When user right click on link and select "Open in new tab", new tab opens, and url is: "about:blank".
How to I correct this? Thanks.
Share Improve this question asked May 14, 2014 at 6:25 TavousiTavousi 15.5k19 gold badges54 silver badges71 bronze badges3 Answers
Reset to default 3The key is not to replace anchor functionality with Javascript. Anchors are designed to take you to a new URL. If you have an anchor, use it as an anchor (eg, set a href
on it). Don't bind a click event that uses window.location
to redirect to a new url.
Your current anchor has no href
. So when you try and open it in a new tab, there's no surprise that nothing loads. You're literally opening nothing in the new tab.
If you really need to set the URL dynamically, then change the href of the element using javascript. For example:
document.getElementById('my-element').href = new_href;
It goes without saying that this needs to be done before the anchor is clicked (not on click). For example, on window.load
, or the pletion of the function that generates the dynamic URL.
Use window.open
function and pass _blank
in second argument function
function Click() {
//some code
window.open(url,'_blank');
}
you should correct your anchor tag to be something like this:
<a href="http://yourLink." target="_blank" onclick="Click();">link text</a>
this way when user click on the link it'll open in new tab and you'll execute the Click function.
本文标签: hyperlinkClick link and call javascript and then quotOpen in new tabquotStack Overflow
版权声明:本文标题:hyperlink - Click link and call javascript and then "Open in new tab" - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742309553a2450592.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论