开发者

How to Open Multiple stand alone browser windows using JavaScript?

I have html page with 2 links like

<a href="http://www.w3schools.com/">Visit W3Schools</a>
<a href="http://www.w4schools.com/">Visit W4Schools</a>

I want when user clicks on one of them to open new fully powerful browser window with that link. I want user to be开发者_运维百科 capable of opening more than one window from my page. I need it to work via pure JS or using jQuery. It needs to work in Safari 3 and Internet explorer 6,7,8. How to create such thing?


You can set the target attribute as _blank.

<a href="http://www.w3schools.com/" target="_blank">Visit W3Schools</a>

This will open the new page in a new window or a tab depending upon the browser setting.


Target="_blank" is invalid

in js you can do this with window.open()

window.open(URL,WidowName,Options)

You have this options 1. width=300 Use this to define the width of the new window.

  1. height=200 Use this to define the height of the new window.

  2. resizable=yes or no Use this to control whether or not you want the user to be able to resize the window.

  3. scrollbars=yes or no This lets you decide whether or not to have scrollbars on the window.

  4. toolbar=yes or no Whether or not the new window should have the browser navigation bar at the top (The back, foward, stop buttons..etc.).

  5. location=yes or no Whether or not you wish to show the location box with the current url (The place to type http://address).

  6. directories=yes or no Whether or not the window should show the extra buttons. (what's cool, personal buttons, etc...).

  7. status=yes or no Whether or not to show the window status bar at the bottom of the window.

  8. menubar=yes or no Whether or not to show the menus at the top of the window (File, Edit, etc...).

  9. copyhistory=yes or no Whether or not to copy the old browser window's history list to the new window.

http://www.pageresource.com/jscript/jwinopen.htm

a valid jQuery and html solution is

$("document").ready(function(){
   $(".blankTarget").live("click",function(){
      window.open($(this).attr("href"));
      return false;
   });
});


You can use <a href="blah" Target="_blank">Some text</a> which will prompt the browser to open a new window but this can be overriden by specific user settings/configurations in some browsers.

Beyond that, you cannot force the operating system to launch a new instance of a browser from Javascript - that would be a massive security risk!


target="_blank" is the best way to solve this as it doesn't involve javascript. Please bear in mind that this attribute is frowned upon and you should really let the user decide if they want to open the linked page in a new window or the window they are currently using. W3C agree with this and as such your code will not validate using their validation tool.

Alternatively this post describes how to implement a jQuery solution.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜