Question

I need to open a new window for my printing can any one let me know how to do this?

My button Click code:

My window.location works, but cant use the window.open

$('.btnFromIndexPrint').click(function () {
    if (document.URL.indexOf('index') > -1) {
         // window.location = '../Print/' + $(this).attr('id');
         window.open = '../Print/' + $(this).attr('id');
    } else {
        //window.location = 'Contract/Print/' + $(this).attr('id'); //Redirect  from Index page to print action
        window.open = 'Contract/Print/' + $(this).attr('id');
    }

});

My html:

I know there is something called target ="blank" id don't think it will work though.

<input type="button" value="Print" class="btnFromIndexPrint" id="@item.SalesContractId"/>

How would i open a redirect on a new page?

Important!!!!!!!

return RedirectToAction("Print", new { id = contractInstance.SalesContractId });
Was it helpful?

Solution

should be:

window.open(url_or_your_page);

See: Example

OTHER TIPS

Syntax for window.location is

window.location = "url";

For example:

window.location ="http://www.mozilla.org";

thus it is working fine in your code.

But the syntax for window.open() is

window.open(URL, windowName[, windowFeatures])

For example :

window.open ("http://www.javascript-coder.com","mywindow","status=1");

There problem was in your syntax.

Hope this helps.

You should try :

window.open(url, [window name], "height=x,width=y");

When width/height are specified , it opens it in a new window.See window.open .

To start with, I hope you have included the above code in jQuery's document.ready function or placed the code at the bottom of the page. This is because if the specified print button has not been loaded in the DOM, the selector ($) will not find it and as such your click listener will not be attached to it.

Secondly, window.open is a function and should NOT be assigned to like a variable (which you have done above). In other words it is

window.open( parameters ); //NOT window.open = value;

See my example code below which is more or less a correction and optimisation of yours above.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Window.Open</title>
  <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
  <script type="text/javascript">
    //@Author: Prof. No Time
    $(document).ready(function(){
      $('.btnFromIndexPrint').click(function () {
         var urlToOpen = '';
         var docURL = document.URL;

         if (docURL.indexOf('index') > -1) {
            urlToOpen = '../Print/' + $(this).attr('id');
         }
         else {
            urlToOpen = 'Contract/Print/' + $(this).attr('id');
         }

         //alert('urlToOpen > ' + urlToOpen);
         if (urlToOpen != '') window.open(urlToOpen);
      });
   });
  </script>
 </head>

 <body>
    <input type="button" value="Print" class="btnFromIndexPrint" id="@item.SalesContractId"/>
 </body>
</html>

Finally, I will advise against the use of such kind of IDs as shown above (@item.SalesContractId). I want to believe that somewhere that value was supposed to be substituted by the server side processing?

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top