Search code examples
jqueryexternal-url

Jquery: how to open external link in same window


I have the following code set up to open a URL in a the same Window but requires two clicks to open, any idea's why?

            $('button.more-details-link').on("click",function(){
                window.open(url, '_self')
            });

Solution

  • Try wrapping it in a document ready function: https://jsfiddle.net/z2b08x7t/

    $(function(){ 
    $('button.more-details-link').on("click",function(e){
         e.preventDefault();
         var url = "google.com"
                    window.open(url, '_self')
                });
        });
    

    Or if you want to open it in a blank window:

     $(function(){ 
        $('button.more-details-link').on("click",function(e){
             e.preventDefault();
             var url = "google.com"
                        window.open(url, '_blank')
                    });
            });