How to open a new tab / window using JavaScript?

goal

I want to open the url in a new tab / window in EXACT just like target = "_ blank".

Code

I am using a PHP condition that runs the following JavaScript:

<script type="text/javascript"> window.open ("http://www.google.com/","_blank", "status=1,toolbar=1"); </script> 

My problem

window.open is NOT SAME as the target = "_ blank" hyperlink.

  • It presents a problem with popup blockers.
  • The window requires the parameters to look like target = "_ blank".
  • After running JavaScript, some font colors of the containing document are lost.

My question

How do I EXACTLY simulate what target = "_ blank" is created?

+4
source share
1 answer

Instead of opening a window from JavaScript, use JavaScript to update the href link and then click on the link. This way you get the same behavior as the user by clicking on the link.

Add a link to your page with the id and target="_blank" . When you want to open a new window, refresh the href this link, and then invoke a click like this (from here ).

 function clickLink(link) { var cancelled = false; if (document.createEvent) { var event = document.createEvent("MouseEvents"); event.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null); cancelled = !link.dispatchEvent(event); } else if (link.fireEvent) { cancelled = !link.fireEvent("onclick"); } if (!cancelled) { window.location = link.href; } } 
+1
source

Source: https://habr.com/ru/post/1392815/


All Articles