Question

I am using the code from another post on Stack Overflow.

How to I redirect to another page *after* printing document?

The page gave me a solution, to write the

window.location.href = "";

to be written after the window.print(); method. So I did. Following is the code of mine

$('.start-printing').click(function () {
   event.preventDefault();
   window.print();
   var url = window.location.href;
   var formId = url.split('/')[4];
   var newPage = "";
   if (url.split('/')[3] == 'form') {
       newPage = 'form_2';
   } else if (url.split('/')[3] == 'form') {
       newPage = 'form_2';
   } else if (url.split('/')[3] == 'form_2') {
       newPage = 'form_3';
   } else if (url.split('/')[3] == 'form_3') {
       newPage = 'form_4';
   } else if (url.split('/')[3] == 'form_4') {
       newPage = 'form_5';
   }
   url = url + newPage + formId;
});

I am using an anchor tag, to do this job. But I am preventing the default behaviour of it. You can see that in the very first line.

Here is the HTML

<a href="~/#startPrinting" class="start-printing">START PRINTING</a>

What goes is, that it does allow me to print the document when I click on it. I can see the Chrome's Print popup and I click Print button. But after that, it really doesn't redirect me to anywhere.

I have tried watching that in the Console and trying to get to the point, where the problem actually is. But I was not able to find the problem.

I am also making a logic to redirect to the next page of the form depending on the current URL of the page.

What is the error here?

Était-ce utile?

La solution

By using event.preventDefault(), you're inhibiting the anchor tag from doing what it wants to do naturally, which is hyperlink you to another resource. As a result, you won't be redirected to your desired location.

That said, why not just add some additional Javascript to perform the redirection at the bottom of your click function?

http://www.tutorialspoint.com/javascript/javascript_page_redirect.htm

$(document).ready(function () {
    $('.start-printing').click(function () {
        event.preventDefault();
        window.print();
        var url = window.location.href;
        var formId = url.split('/')[4];
        var newPage = "";
        if (url.split('/')[3] == 'form') {
            newPage = 'form_2';
        } else if (url.split('/')[3] == 'form') {
            newPage = 'form_2';
        } else if (url.split('/')[3] == 'form_2') {
            newPage = 'form_3';
        } else if (url.split('/')[3] == 'form_3') {
            newPage = 'form_4';
        } else if (url.split('/')[3] == 'form_4') {
            newPage = 'form_5';
        }
        url = url + newPage + formId;
        window.location = url;
    });
});
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top