JavaScript opens new window blocked issue

Source: Internet
Author: User

New window open page, a very common effect, as for the code, the general first reaction is so written:
window.open (URL);
but the mainstream browser will intercept this effect (there may be too many pop-up ads in these years, if the browser does not intercept, users can not stand) in order to prevent the window from being intercepted, it is generally the use of a tag to open a new page, using the target= "_blank" attribute of the A tag, generally use the following two methods according to the situation:1. Triggered by the Click Event New window opens:----Of course you can use a tag directly on the page, this is just for the rest of the groundwork!
<href= "http://www.baidu.com/"  target= "_blank"  ></a>
You can change the button that triggered the click to:
<href= "javascript:void (0)"  target= "_blank"  ID = "Openwindow" ></ a >  
Click the event to write:
1 $ ("#openWindow"). Click (function(e) {  2     $ ( This ). attr ("href", "http://www.baidu.com/");   3 });  
in the order of event firing, the href of the A tag is modified when clicked, then the default event is triggered, then the default event of the A tag is fired, and a new page is opened. Note: If you need to open a new window, you cannot use return False,e.preventdefault () in the Click event, or the statement that interrupts the default event.  2, other situation: if there is no a tag to open the page, then we create a new a tag out, and then simulate this a-tag click eventjquery notation:
1 function Openwindow (URL) {  2      var link = $ ("<a></a>"). attr ("href", url). attr (" Target "," _blank ");   3      $ ("Body"). Append (link);   4      Link[0].click ();   5      link.remove ();   6  }
native JavaScript notation:
1 function Openwindow (URL) {  2     var link = document.createelement (' a ');   3     Link.target = "_blank";   4     link.href = url;   5     document.body.appendChild (link);   6     Link.click ();   7     document.body.removechild (link);   8 }  

JavaScript opens new window blocked issue

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.