0

The following markup is generated by my ASP.NET MVC page.

<button onclick="window.open='/Client/ReleaseForm';" type="button" class="btn btn-primary">
 View/Print Release Form
</button>

However, clicking the button has no effect. No window is opened, no new tabs are opened, and no errors are shown in the console of my Chrome browser.

I know there are pop-up blockers, but I had got the impression that this worked as long as it was in response to a user action (such as clicking the button).

Can anyone tell me how I can display content in a new window?

Madara's Ghost
176k52 gold badges274 silver badges314 bronze badges
asked Feb 1, 2015 at 16:34
13
  • @SecondRikudo: Yes, what about it? Commented Feb 1, 2015 at 16:37
  • window.open is a function - window.open("/Client/ReleaseForm") Commented Feb 1, 2015 at 16:38
  • @Pointy: Well dang, I tried that and got an error. Commented Feb 1, 2015 at 16:38
  • 1
    @JonathanWood no repro, I get a new tab as expected. Commented Feb 1, 2015 at 16:45
  • 1
    @JonathanWood if your broken "click" handler has already run, then you've overwritten the normal value of window.open with a string. Try in a freshly-loaded page. Commented Feb 1, 2015 at 16:51

1 Answer 1

2

I'm pretty sure window.open is a function, so you want:

window.open('/Client/ReleaseForm');

Also, it's probably even better to set an event handler in JavaScript code and not inline.

<button id="print" type="button" class="btn btn-primary">
 View/Print Release Form
</button>
<!-- Elsewhere, in a JS file -->
document.getElementById('print')
 .addEventListener('click', function viewOrPrintReleaseForm() {
 window.open('/Client/ReleaseForm');
 });
answered Feb 1, 2015 at 16:38
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, this is working now. I tried typing window.open('/Client/ReleaseForm') in the browser (Chrome) and got the error "Uncaught TypeError: string is not a function". Not sure what happened there but it threw me off. I added a handler using jQuery and it works as expected.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.