Javascript: Open Several Urls With One Button Click

How to open multiple external URLs in one button click but not get blocked by browsers popup blocker?

Write onclick event for any anchor tags with multiple windows.open for opening the links in new window/tab.

<a href="http://bloggersentral.blogspot.com/" target="_blank" onclick="window.open("http://www.tourism.gov.my/"); window.open("http://www.tic.kelantan.gov.my/");">This anchor opens three links in a single click</a>

Above anchor will open three links in single click.you can write multiple window.open for same.

How to make a link open multiple pages when clicked

HTML:

<a href="#" class="yourlink">Click Here</a>

JS:

$('a.yourlink').click(function(e) {
e.preventDefault();
window.open('http://yoururl1.com');
window.open('http://yoururl2.com');
});

window.open also can take additional parameters. See them here: http://www.javascript-coder.com/window-popup/javascript-window-open.phtml

You should also know that window.open is sometimes blocked by popup blockers and/or ad-filters.

Addition from Paul below: This approach also places a dependency on JavaScript being enabled. Not typically a good idea, but sometimes necessary.

Handle multiple urls with one (click)

he is emitting informations about the social media including the name when the icon clicked, i think it should be deleted , cause it's usless, you can add a switch-case statement to handle the redirection directly :

constructor(private router: Router) { }    

onIconClicked(ele: any) {
switch (ele) {
case 'twitter':
this.router.navigate(['your_link_here']);
// or window.open(link, '_blank'); to open an external link in a new tab
break;
case 'linkedIn':
this.router.navigate(['your_link_here']);
// or window.open(link, '_blank'); to open an external link in a new tab
break;
case 'faceBook':
this.router.navigate(['your_link_here']);
// or window.open(link, '_blank'); to open an external link in a new tab
break;
default:
break;
}
}

HTML button click opening 2 links in new tab

Clicking on an anchor element (a tag) instructs the browser to open the link. Having target attribute further tells the browser to open it in a new window/tab. Now your code captures the click event and explicitly opens a new window (with proper url). However, that doesn't prevent the browser from carrying out the original action intended for all 'anchor' tags.

Basically you see two tabs opening, because one is opened by your code, and the other one by browser because of the click on anchor tag. You can suppress the default browser behaviour by calling preventDefault on the event object passed to your handler. Your code should be something like this:

jQuery('#starter-monthly').on('click', function(e) {
e.preventDefault();
var href = jQuery(this).attr('href');
window.open(href + '?package='+24);
});

jQuery('#starter-yearly').on('click', function(e) {
e.preventDefault();
var href = jQuery(this).attr('href');
window.open(href + '?package='+27);
});


Related Topics



Leave a reply



Submit