Search code examples
javascriptjquerygreasemonkey

Change onclick to open in new window


So I have a button on a website that looks like this;

 <input type="submit" class="MainButton" title="Change" id="ch01_changebutton" onclick="window.location.href='https://www.websiteurl.com'; return false;" value="Change" name="ch01$changebutton">

I'm wanting to use greasemonkey so I can open that url of the button in a new window.

Any help would be appreciated.

I've tried messing around with this but no luck: Using Javascript to click on a given button AND have it open in a new window (doesnt even work for that website example on there for me)


Solution

  • window.location sets the url of your current window. To open a new window, you need to use window.open. This should work:

    var btn = document.getElementById("ch01_changebutton");
    var x = btn.getAttribute("onclick");
    x = x.replace("location.href=","open(");
    x = x.replace(";",",'_blank');");
    btn.setAttribute("onclick",x);