hyperlink - Click link and call javascript and then "Open in new tab" - Stack Overflow

I have a link in my page.<a onclick="Click()">Click me<a>When I click this link,

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 badges
Add a ment  | 

3 Answers 3

Reset to default 3

The 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742309554a4419616.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信