javascript - Open href link in popup - Stack Overflow

I'm trying to open the link in popup, i've write this static html code :<a href="&quo

I'm trying to open the link in popup, i've write this static html code :

<a href="" target="popup" 
    onclick="window.open('','popup','width=600,height=600');   
    return false;"> Open Link in Popup
</a>

this code works, it open the link in popup.

But, if i want to add this code in javascript (with jquery) like this :

$('#services').append("<li><a href='' target='popup' onclick='window.open('','popup','width=600,height=600'); return false;'>Open Link in Popup</a></li>");

The link is opened in new tab and not in popup.

Why ?

I'm trying to open the link in popup, i've write this static html code :

<a href="http://www.google." target="popup" 
    onclick="window.open('http://www.google.','popup','width=600,height=600');   
    return false;"> Open Link in Popup
</a>

this code works, it open the link in popup.

But, if i want to add this code in javascript (with jquery) like this :

$('#services').append("<li><a href='http://www.google.' target='popup' onclick='window.open('http://www.google.','popup','width=600,height=600'); return false;'>Open Link in Popup</a></li>");

The link is opened in new tab and not in popup.

Why ?

Share Improve this question asked Apr 18, 2017 at 9:18 Pasja95Pasja95 931 gold badge1 silver badge15 bronze badges 4
  • remove target='popup' from append clause and set href="#" – diavolic Commented Apr 18, 2017 at 9:21
  • when i remove target, the current href is replaced by the new href – Pasja95 Commented Apr 18, 2017 at 9:24
  • set href="#" as i wrote before: $('#services').append("<li><a href='#' onclick='window.open('http://www.google.','popup','width=600,height=600'); return false;'>Open Link in Popup</a></li>"); – diavolic Commented Apr 18, 2017 at 9:25
  • Nothing happen when i set href to # – Pasja95 Commented Apr 18, 2017 at 9:29
Add a ment  | 

3 Answers 3

Reset to default 2

Check this example on jsfiddle - Problem is in quotes

$('#services').append("<a href='http://www.google.' target='popup' onclick=window.open('http://www.google.','popup','width=600,height=600'); return false;>Open Link in Popup</a>");

You missed to escape the quotes in onClick of your jQuery. Update the script as below.

$('#services').append("<li><a href='http://www.google.' onclick=\"window.open('http://www.google.','popup','width=600,height=600'); return false;\">Open Link in Popup</a></li>"); 

Change your algorithm, instead of putting the javascript inside onclick, add a class or ID, with data-, so you can attach your jQuery event handler on that class/ID, and use the data to create new window. Imagine the data as data-destination="http://www.destination".

That should fix your problem.

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

相关推荐

  • javascript - Open href link in popup - Stack Overflow

    I'm trying to open the link in popup, i've write this static html code :<a href="&quo

    7天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信