using js to open links in current window, new window, or in the background

There are times to open links in the current window, sometimes in a new window, sometimes in background. Here is how to do it.

function open2(url, opt){
  if (opt == 0) // current window
    window.location = url;
  else if (opt == 1) // new window
    window.open(url);
  else if (opt == 2) // background window
    {window.open(url); self.focus();}
}

The syntax is a bit confusing, I guess these should be standardized in the future.

Ref link: http://snippets.dzone.com/posts/show/2915

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s