I am wondering whether it's possible to close a tab as soon as a new site has loaded, without having to use js on the new site. I basically want to close the tab when we receive any content from the new site.
I use this to trigger a click event which submits a form:
$('#target_attack').click();
I tried putting window.close() right after this, but the tab closed without having loaded the new site.
I also tried to pause the script for 3 seconds and then close the tab, but for some reasons the site then won't load.
I also thought about using sessions but this means I would have to use js on the other site too, which I want to avoid.
I hope you guys can help a little javascript noob C:
Thanks in advance!
Guessing you are using window.open to open it in a new tab.
var winPop = window.open(url);
$(winPop.document).ready(function() {
window.close();
});
If javascript didn't open the window, javascript cannot close the window. Otherwise, window.close() is what you use.
You can try to bypass this security restriction (bad plan), but I do not believe this works on newer versions of any browser:
window.top.opener=null;
window.close();
See the docs - Firefox: https://developer.mozilla.org/En/DOM:window.close, IE: http://msdn.microsoft.com/en-us/library/ms536367%28VS.85%29.aspx
Related
I am having an issue where seemingly the way JavaScript executes is changing depending on whether I open a file from a brand new browser tab (copy-pasting the link in) versus if I open the page by clicking a link from another page.
A summarized version of my code is:
<html>
<head>
<script>
try {
// ...other code...
throw new Error("error");
} catch (e) {
location.href =
"https:\/\/google.com"; // would be "https:\/\/mysite.com"
}
window.close();
</script>
</head>
</html>
If I open this in a brand new tab by copy-pasting the link, the tab will navigate to https://google.com
However, if I open the link via a hyperlink, the new tab closes immediately.
Ideally, I want the redirect to be successful regardless of how the page is loaded.
Would love to understand what's going on here as the above code snippet is from an external library that I can't modify
Appreciate any insight or help!
From MDN:
This method can only be called on windows that were opened by a script using the Window.open() method, or on top-level windows that have a single history entry. If the window doesn't match these requirements, an error similar to this one appears in the console: Scripts may not close windows that were not opened by script.
In other words, a page opened by clicking a link on another site cannot use window.close(). However, if you create it with window.open(), that restriction does not apply and you can use window.close(), which is overriding your redirect. Ctrl clicking on a link to open it in a new tab also satisfies this condition. You can verify that by running window.close() in the devtools console and seeing whether or not it closes the tab.
OK so I'm opening a new tab on delay with the following:
setTimeout(function(){x=window.open('http://sample.com');}, 1000);
On that new page there is a link I would like to click after the window opens. I'm able to open the browser dev tools and input this to click the link I want:
$('.view-public')[0].click();
I would like to open the window then apply the click on delay, so I've tried to combine what I have as such:
setTimeout(function(){x=window.open('http://sample.com');}, 1000);
setTimeout(function(){x.$('.view-public')[0].click(); }, 3000);
However this is giving me the "SecurityError: Blocked a frame with origin..." which I have looked up and understand, however it seems most people who encounter this are working with an iframe, I'm not using an iframe just want to open a new window and click a specific link. Is there any solution to this or way around this?
So I found a chrome browser plug-in that is allowing me to accomplish what I want.
Custom JavaScript
I'm performing the new window opening with JS on my site and with the plugin I can create custom scripts to run on specified websites. So Custom JS is performing the click for me.
Its a bit of a hack but its giving me the result I need.
I have the following code in which I am trying to open multiple mailclient
It works in Firefox but dont work in Chrome
<button class="button">Open Email</button>
$(document).ready(function(){
$('.button').on('click',function(){
window.location.href = "mailto:user#example.com?subject=Subject&body=message%20goes%20here";
window.location.href = "mailto:user#example.com?subject=Subject2&body=message%20goes%20here";
});
});
Here is the fiddle to it
Anyone knows whats the reason behind this , or is there any other technique to do this?
Thanks
Chrome allows only one opened window per user action. If opening new browsers window was the issue, you could tell the popup blocker to allow it, but this is not possible when launching email windows. You could either require two user actions (e.g. two buttons) or you could make a web based mail form to do the same thing (if you're using the mail client of tracing purposes, just make it send a copy to yourself.
If you want to open multiple links you shouldn't use window.location.href, it opens link in current window and you cant really have more than one link opened in one window. You should use window.open(your_url) for that, but beware, it will create popup windows.
From a browser perspective mailto is a link like any other, so assigning it to window.location.href twice in a row is like fast-clicking two links in a page, browser will process only one of them.
And the last, code from your question not working even in FF if you use browser based mail client, like gmail.
You could supply 2 links for the user tp open the email clients manually, you could also open the client, refresh the page (indicating something on the url for the second one) and then open the second one?
I know someone else asked this recently, but I haven't seen an answer up there yet. I have a popup music player working with a WordPress website. Getting it to work was tricky and now I'm told the client sees it loading as a tab instead of a new window, which he absolutely won't accept. I wonder if there is absolutely no way to make this work?
function popUp() {
var popup=window.open('http://websitehere.com?id=somevar&more=anothervar','','width=495,height=250,scrollbars=0');
}
And here's the call, in a link:
<img src="theimage.png">
Am I doing something wrong? Is there better popup code out there? Thanks!
No. Whether window.open() opens the url in a new window or a new tab is controlled by the user's browser's preferences, and cannot be overridden.
You can put it in an object element, or an iframe, in a div tricked out to look and act like a draggable, closable 'window'. Wait'll the client finds out some setups prevent any new windows being opened via script.
I have the following javascript bookmarklet which opens a new popup window with a facebook post page in side of it.
javascript:var d=document,f='http://www.facebook.com/share',l=d.location,e=encodeURIComponent,p='.php?src=bm&v=4&i=1261526047&u='+e(l.href)+'&t='+e(d.title);1;try{if (!/^(.*\.)?facebook\.[^.]*$/.test(l.host))throw(0);share_internal_bookmarklet(p)}catch(z) {a=function() {if (!window.open(f+'r'+p,'sharer','toolbar=0,status=0,resizable=1,width=626,height=436'))l.href=f+p};if (/Firefox/.test(navigator.userAgent))setTimeout(a,0);else{a()}}void(0)
I just add that code into the URL of a shortcut link in my browser and it opens the facebook post page and passes the URL and some info about the page I am on to it.
I need to do a much simpler task. I need to get the URL of the page I am on and either open a new tab or even just use the tab I am in and then open a link like this
http://mydomain.com/labs/iframe_header.php?url= PUT THE CURRENT PAGES URL RIGHT HERE
As you can see I just need to make a bookmarklet that will take the page I am on and pass it into my sites page. Can anyone help me, I don't know much javascript at all, would greatly appreciate any help.
javascript: location.href = 'http://mydomain.com/labs/iframe_header.php?url=' + escape(location.href);
This will open in a new window, which will use a new tab if your browser is set up that way:
javascript: window.open('http://mydomain.com/labs/iframe_header.php?url=' + escape(location.href));