Samuel Baldus
Samuel Baldus

Reputation: 21

Javascript - redirect problem

I have this script to print a page, which it does fine.

However, after the script runs, the browser redirects to the folder root:

<SCRIPT Language="Javascript">
//PRINT PAGE FUNCTION
function printit(){  
if (window.print) {
    window.print() ;  
} else {
    var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';
document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
    WebBrowser1.ExecWB(6, 2);//Use a 1 vs. a 2 for a prompting dialog box    WebBrowser1.outerHTML = "";  
}
}
</script>

How can I stop this happening? I want remain on the current page!

I can't get this to work on my Apache nor (more importantly) my IIS remote server. Pages are written in PHP. Javascript is called after clicking a button. Once pressed, the print page option appears. If you print or close the option dialog, the browser redirects to the directory root (browser lists the contents of directory) Hope this makes sense.

Upvotes: 1

Views: 1233

Answers (1)

Mark Bell
Mark Bell

Reputation: 29735

At a guess, I suspect you are not returning false within the onclick event of the link/button. If you click the following link:

<a href="/yourrootdirectory" onclick="printit();">Test</a>

the browser will still navigate to the link href when the print dialog closes. However, if you do this:

<a href="/yourrootdirectory" onclick="printit(); return false;">Test</a>

then the link's default action is cancelled and you'll stay where you are. You could also return false within the printit() function body.

Upvotes: 6

Related Questions