JavaScript to open two web pages using one link - javascript

I need JavaScript code or HTML to make two websites open in two new browser tabs when clicking on one link. I do not want them to open in new windows, or on the current page that the link is on.

It probably won't work because the browser might consider it a popup and block it.
If the user allows popups you can do:
window.open(url, '_blank');
Like:
<a id="mydoublelink" href="http://site1.com" target="_blank">foo</a>
document.getElementById("mydoublelink").onclick=function(){
window.open('http://site2.com', '_blank');
}

If you call window.open in the onclick event you should be fine. Built-in popup blockers allow those. The kind of popups that get blocked come from other events or from scheduled events like a setTimeout.
document.getElement("my_link").onclick = function () {
window.open(/*..*/); // works
}
document.getElement("my_link").onclick = function () {
setTimeout(function () {
window.open(/*..*/); // will probably get blocked
});
}
This means, for instance, that if you open a popup after an AJAX call it will very likely get blocked. A workaround in this case is to open the popup right away and fill in the content later. This is outside the scope of this question but I feel like this is information that everyone should know.

Something like this?
<!DOCTYPE html>
<html>
<head>
<script>
function open_win()
{
window.open("URL");
open_win_two();
}
function open_win_two()
{
window.open("URL");
}
</script>
</head>
<body>
<a onclick="open_win()">luyfl</a>
</body>
</html>

Yu can try the new target _newtab:
blabla
It works in Firefox, don't know if it's supported in other browsers.

Related

How to automatically click log in button using JavaScript

I want to save some JavaScript code as a bookmark in chrome so it automatically opens my university login site and clicks on on the login button. I am completely inexperienced in JavaScript, so I have no clue how to do this. I snipped together the following code, which opens the correct website, but then does not click on anything. The first URL automatically puts me to the login site (third URL in the code) in case I have not logged in yet in this window.
(function() {
window.location.replace("https://lms.uzh.ch/auth/MyCoursesSite/0");
window.onload = function(){
if (current_url.startswith('https://lms.uzh.ch/auth/MyCoursesSite/0')) {
return;
}
if (current_url.startsWith('https://lms.uzh.ch/dmz/')) {
document.getElementById("wayf_submit_button").click();
}
};
})();
I'm sorry if this is too obvious a question and annoys any experts but as I said I am a complete beginner. I would of course add the "javascript:" at the beginning for chrome to understand the bookmark.
When you use window.location.replace, you change the address and you code can't work anymore.
I can suggest using some browser extension, your "click function" should work then.
I guess you could also try to make some simple html page with iframe, you call your "click function" at this page, but you target it to the iframe. After that you can change browser's location to the university's webpage as you should already be logged in.
<html>
<head>
</head>
<body>
<iframe src="your_university_address.com" id="some_id" onload="click_button()"></iframe>
<script>
function click_button()
{
my_iframe=document.getElementById('some_id');
my_iframe.contentDocument.getElementById('your_button_id').click();
}
</script>
</body>
</html>
Very simple but it should do that job.
(You can achieve similar result by using window.open() I guess)

Leaving website callback in JavaScript

Is there a way to run function when user leaves the specific website? Also how handle another tabs are in web browser opened with same URL? I completely don't know how to go about it. For example, I have two tabs with www.burito.com. I want to run function when last tab with www.burrito.com is closed. Thanks.
In html
<script>
function myUnLoad() {
}
</script>
<body onunload="myUnLoad()">
Are you looking for this? (using jQuery)
$( window ).unload(function() {
//your stuff
});

Javascript pop-up login cross-browser issue

I have a page with an iframe in it. Within the iframe, the default page runs some javascript to open a child window, login to an external service, then display some content from that external service back in the iframe.
My code works fine in Chrome and IE but in Safari and Firefox it does not. In those browsers the parent window seems to ignore that fact that the user is now logged in to the external service and displays their "Not logged in" message instead of the info that is supposed to display.
Here's the code for what I'm trying to do.
HTML (main page):
<iframe id="brief" name="brief" src="contents/somepage.php?var=WXYZ" width="962" height="600">
<p>Your browser does not support iframes.</p>
</iframe>
somepage.php
<html>
<head>
<script type="text/javascript">
window.onload = function()
{
var code = 'WXYZ';
var login = http://www.externalsite.com/brief/html.asp?/cgi-bin/service?msg=0048&usr=username&pwd=pass';
//OPEN CHILD WINDOW AND LOGIN USING VARIABLES ABOVE, THEN CLOSE WINDOW
childWindow=window.open(login,'','width=30,height=30,location=no');
var cmd = 'childWindow.close()';
setTimeout(cmd,2000);
//REDIRECT THIS IFRAME TO BRIEFING INFORMATION
var uri = 'http://www.externalsite.com/brief/html.asp?/cgi-bin/service?msg=0092&AID=NOSEND&MET=1&NTM=1&LOC='+code;
self.location.href=uri;
}
</script>
</head>
<body>
</body>
</html>
I have tried adjusting various setTimeout functions to try to delay certain aspects of the script to wait for something else to happen but it doesn't seem to help.
Is there some cross-browser problem with this code that I am missing?
Thanks
setTimeout(cmd,2000); is not going to block script execution for two seconds; rather, it sets up an event that will fire in approximately 2 seconds. Immediately after your call to setTimeout, the remaining parts of the script will execute:
// This happens right away
uri = 'http://www.externalsite.com/brief/html.asp?/cgi-bin/service?msg=0092&AID=NOSEND&MET=1&NTM=1&LOC='+code;
self.location.href = uri;
The fact that it works in any browser is just lucky timing. If you want the iframe to refresh after the popup closes, add that code to your callback (you don't need to and shouldn't use a string for your timer handler, by the way):
setTimeout(function() {
childWindow.close();
var uri = 'http://www.externalsite.com/brief/html.asp?/cgi-bin/service?msg=0092&AID=NOSEND&MET=1&NTM=1&LOC='+code;
self.location.href = uri;
}, 2000);
Even this solution won't work if the popup's content doesn't load within two seconds. The best solution is to have the popup close itself after loading. Then, you could detect when it closes and know that you're ready to reload your iframe. But if this is a third-party site that you don't have control over, you're probably stuck using a less-than-ideal solution like this.

Javascript function isn't running

Have been trying to fix this for the past hour.
Here goes:
<script type="text/javascript">
function openAd(adType, urlToGo) {
pageTracker._trackPageview(adType);
window.open(urlToGo, '_blank');
return false;
}
</script>
Anyone have any idea why this doesn't work?
Called it like onclick="openAd('/Ads/MMA_Front_Page.com', 'http://www.anrdoezrs.net/click-4706163-10919130');"
I uploaded a live version at:
http://www.easymuaythai.com/
I have went to your page in Opera 11.60 and called openAd('/Ads/MMA_Front_Page.com', 'http://www.anrdoezrs.net/click-4706163-10919130'); through Dragonfly and the information was displayed in my browser that the popup has been blocked. When I click to display the popup anyway, the new windows with your add target is displayed correctly.
Thus, I would suggest to seek different method to open the ad or accept the fact you may be blocked by popup / ad blocker.
if your problem is that the browser loads the url in the href as well as opens a new window then you need to add return false as so:
onclick="openAd('/Ads/MMA_Front_Page.com', 'http://www.anrdoezrs.net/click-4706163-10919130'); return false;"

opening new windows after specifc interval of time using window.open()

I am looking for javascript code which will open new tabs(windows) automatically after specific interval of time.
i have few websites over here, in this code which open automatically when i press the button on the html page.
I want these websites to open after specific interval.
like, 1st website will open when user will press button "Open Windows",
2 nd website after 1 minute and 3 rd websites after 2 minutes.
eg.
<html>
<head>
<script type="text/javascript">
function open_win() {
window.open("http://www.google.com")
window.open("http://www.yahoo.com")
window.open("http://www.bing.com")
}
</script>
</head>
<body>
<form>
<input type=button value="Open Windows" onclick="open_win()">
</form>
</body>
</html>
Thank you,
sangram
In most modern browsers you are not allowed to call window.open programatically, like through a setTimeout.
The browser will simply ignore the window.open statement if it is not inside a callstack that is initiated by a direct user interaction, for instance a mouse click event.
The reason for this is that it is very annoying behavior - you probably won't find a single person who enjoys using a site that opens windows by itself.
So: reconsider what you are trying to do, there is bound to be a better way - one where you can work with the browser/user and not against it/him/her :)
function open_win() {
window.open("x.com");
setTimeout("window.open('y.com')",60000);
setTimeout("window.open('z.com')",120000);
}
This should open x.com then after one minute y.com and after two it should open z.com.

Categories