admin管理员组文章数量:1406177
I am using this code to open a new tab:
<a href="javascript:window.open('','_blank')">linktext</a>
The new page is also loaded within a new tab however at the source page it is shown [object]
instead of the link. What is wrong?
I do not want to use
<a href="" target="_blank")">linktext</a>
because I generate a list of links and the target page is called differently for each link. The most flexible way for me is to use the give Syntax.
EDIT: This is the generated linklist:
<a href="javascript:window.open('','_blank')">linktext 1</a>
<a href="javascript:jsfunc1(param1)">linktext 2</a>
<a href="javascript:jsfunc2(param2,param3)">linktext 3</a>
The href attribute is generated. I get a from a table.
I am using this code to open a new tab:
<a href="javascript:window.open('https://example.','_blank')">linktext</a>
The new page is also loaded within a new tab however at the source page it is shown [object]
instead of the link. What is wrong?
I do not want to use
<a href="https://example." target="_blank")">linktext</a>
because I generate a list of links and the target page is called differently for each link. The most flexible way for me is to use the give Syntax.
EDIT: This is the generated linklist:
<a href="javascript:window.open('https://example.','_blank')">linktext 1</a>
<a href="javascript:jsfunc1(param1)">linktext 2</a>
<a href="javascript:jsfunc2(param2,param3)">linktext 3</a>
The href attribute is generated. I get a from a table.
Share Improve this question edited Nov 8, 2016 at 10:26 zuluk asked Nov 8, 2016 at 10:16 zulukzuluk 1,5778 gold badges30 silver badges53 bronze badges 4- The example at the top works just fine; proof. We can't help you without an example of what isn't working. – T.J. Crowder Commented Nov 8, 2016 at 10:19
- where do you see [object]? your question is pletely unclear. Are you populating the url in the href looping an array? show the code – Lelio Faieta Commented Nov 8, 2016 at 10:20
- @T.J.Crowder I think OP is sending the url to the href from an array and the script is placing Object instead of the expected value from the array. We miss the whole necessary code – Lelio Faieta Commented Nov 8, 2016 at 10:21
- you see the generated code. When clicking on the first link the target page is loaded correctly, but on the source page (where I e from) I see the text [object] instead of the linklist. In the jsfiddle by @T.J.Crowder the effect is also visible. – zuluk Commented Nov 8, 2016 at 10:23
2 Answers
Reset to default 5The point of a javascript:
scheme URL is to generate a replacement page from JavaScript. It isn't designed to run JavaScript in response to a client while leaving the page alone — that is what event handlers are for.
The new "page" you are seeing is the result of converting the reference to the new window to a string.
This is best avoided by not using JavaScript for this:
<a href="https://example." target='_blank'>linktext</a>
You can also do it by avoiding using javascript:
scheme URIs and binding your JS separately (with a sensible fallback in the href):
<a href="https://example.">linktext</a>
<script>
document.querySelector("a").addEventListener("click", your_function);
function your_function(evt) {
evt.preventDefault();
// Then do whatever else you want the function to do
}
</script>
If you really want to use the JavaScript URL scheme (which, as I suggested above, is a dirty hack), you can ensure that the script returns nothing using the void
operator:
<a href="javascript:void (window.open('https://example.','_blank'))">linktext</a>
The Above Example Works Well. Checkout Below Code :
<a href="javascript:window.open('https://example.','_blank')">linktext</a>
Other Ways To Open Link in New Tab :
<a href="#" onClick="window.open('http://www.yahoo.', '_blank')">test</a>
Without JS :
<a href="http://yahoo." target="_blank">test</a>
本文标签: javascriptwindowopen on atag for new tabStack Overflow
版权声明:本文标题:javascript - window.open on a-tag for new tab - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744967883a2635059.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论