1

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)

Community
  • 1
  • 1
Pengiuns
  • 131
  • 2
  • 9

3 Answers3

2

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);
Munawir
  • 3,346
  • 9
  • 33
  • 51
1

I recommend using

<a href="https://www.websiteurl.com" target="_blank">

without JavaScript, but if there is a form you need to submit it by clicking the button, this is the correct way of opening a new window using JavaScript:

window.open("https://www.websiteurl.com");
  • Since you mentioned greasemonkey I'm assuming that you don't have control over the page's initial markup. Using `window.open` should work in this case. – Neema Peyda Jun 03 '16 at 16:55
0

I would make a javascript functon to change location, and have the button fire that instead.

var changeLocation = function(url) {
    window.location.href = url;
}

then in your onclick:

onclick="changeLocation('www.url.com'); return false;"

BTW: some browsers do not like onclick. Take a look at add onclick event to newly added element in javascript

Community
  • 1
  • 1
Jon Crawford
  • 193
  • 11