简体   繁体   中英

Use href NOT onClick in hyperlink - Javascript

I have the following HTML that pops up a window when I click the 'Open Sesame' link:

<a href="/about-us/" title="Find out About Us" onclick="javascript:void window.open('/about-us/','1329304840803','width=1050,height=500,toolbar=0,menubar=0,location=0,status=1,scrollbars=1,resizable=1,left=0,top=0');return false;">Open Sesame</a></li>

However, I was wondering is it possible I could remove the 'onClick' part and just have the javascript within the href & still get the same new window effect. Is this possible?

Many thanks for any pointers

Yes you can:

<a href="javascript:void window.open('/about-us/','1329304840803','width=1050,height=500,toolbar=0,menubar=0,location=0,status=1,scrollbars=1,resizable=1,left=0,top=0');" title="Find out About Us">Open Sesame</a>

http://jsfiddle.net/Curt/NqfMX/


However, I would recommend an event listener for this. It makes your HTML code much tidier and seperates the function from the HTML design:

<a class="opensesame" href="/about-us/" title="Find out About Us">Open Sesame</a>​​​​​​​​​​​​​​​​​​​​​​

​$(function(){
    $("a.opensesame").click(function(e){
        e.preventDefault();
        window.open('/about-us/','1329304840803','width=1050,height=500,toolbar=0,menubar=0,location=0,status=1,scrollbars=1,resizable=1,left=0,top=0');
    });
});​

http://jsfiddle.net/Curt/NqfMX/1/

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