0

In a tabbed browser (FF 3.5 in for me), I often open links in new tabs with CTRL-click or middle-click. When the link contains a Javascript function, either:

  • A blank new tab opens with javascript:window.print() or what-have-you in the address bar.
  • The function executes successfully and a blank new tab opens.

Thing is, this doesn't seem to happen consistently for all Javascript functions. For example:

  1. The "Print" button on this page, when CTRL-clicked, opens a print dialogue and generates an empty new tab.
  2. The Whitepaper links on this page, when CTRL-clicked, only generates an empty new tab.

The links in question contain the following code, respectively:

  1. javascript:window.print();
  2. javascript:_hbRedirect('PDF Downloads','White Paper','URL.pdf','&c1=TITLE|US-en')

This SO question is somewhat similar. The best-rated answer details an AJAX-based solution, but I could not find an explanation for why some Javascript functions behave differently with tabs than others.

Community
  • 1
  • 1
Daniel F. Thornton
  • 3,687
  • 2
  • 28
  • 41

2 Answers2

1

JavaScript shouldn't be mixed with XHTML. Let the href attribute of a link just be the page or the document you want to link to. If you want to do something special with an a tag, use event registering.

A la MooTools:

a.addEvent ('click', function (e) {

});
dutch
  • 212
  • 1
  • 5
1

That happens because window.print() is already defined by default so it is called normally.

The javascript:_hbRedirect('PDF Downloads','White Paper','URL.pdf','&c1=TITLE|US-en') link calls an undefined function so nothing happens.

Eli Grey
  • 35,104
  • 14
  • 75
  • 93