简体   繁体   中英

Open href link in popup

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

<a href="http://www.google.com" target="popup" 
    onclick="window.open('http://www.google.com','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.com' target='popup' onclick='window.open('http://www.google.com','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 ?

在jsfiddle上检查此示例 -问题在引号中

$('#services').append("<a href='http://www.google.com' target='popup' onclick=window.open('http://www.google.com','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.com' onclick=\"window.open('http://www.google.com','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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM