skip to Main Content

My JavaScript was working fine until I implemented jQuery.
I implemented jQuery to add the contents of another HTML file for my header and sidebar menu so I don’t have to change everything on every page. Since I added it, window.open doesn’t work.

Here’s my script:

function OpenSite(site, newTab) {
    if (newTab == false) {
        console.log("Going to " + site);
        window.open(site, '_self');
    }
    else {
        console.log("Going to " + site + " in a new tab");
        window.open(site, '_blank');
        
    }
}

I’m getting this error in the console:
Uncaught TypeError: window.open is not a function

I’m guessing jQuery is overwriting the open function somewhere? I’m using jQuery with a CDN so I can’t change anything in jQuery itself. The script above should have nothing to do with jQuery.

2

Answers


  1. Chosen as BEST ANSWER

    Whoops, I was using jQuery to load another HTML file, and in that I had a script which was overwriting the open function.


  2. Like you said it’s likely due to a conflict with jQuery or another library. To avoid conflicts, you can try using jQuery’s noConflict method.

    // make sure jQuery CDN script is here (above all the other scripts)
    <script>
        
        var $j = jQuery.noConflict();
    
        // rest of your code
    </script>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search