JS open new tab and avoid popup blocker - javascript

This is the code:
function openUrl(url) {
var newTab = window.open(url, '_blank');
if (!newTab || newTab.closed) {
}
}
}
if there is popup blocker the tab isnt opening so i need to do something that it will be open with popup blocker on.
If you have any solution that doesnt involve new button I will be happy to hear!
If there is no solution so maybe add confirm and work with that.
I tried to add confirm in the if and open new tab with the click trigger but nothing.
I tried to put this in the if:
var a = document.createElement('a');
a.target="_blank";
a.href=url;
a.click();
but nothing
Please help
Thank you!

var windowReference = window.open("http://www.example.com", "_blank");
if (!windowReference) {
// popup was blocked
window.location.href = "http://www.example.com";
}

Related

How to open a new popup and change it to new tab

I want to open a new window but instead of having that window as like like it does, i want it to make it as a tabbed view.
I have tried the following:
var strWindowFeatures = "location=yes,height=570,width=520,scrollbars=yes,status=yes";
var URL = "https://www.website.com/cws/share?mini=true&url=" + location.href;
var win = window.open(URL, "_blank", strWindowFeatures);
The above opens as a new window i.e popup. And i tried the below for new pop
var win = window.open(url, '_blank');
win.focus();
But i tried combining the above to procedures but with no luck. could someone tell me if its possible at all and or how to go about it. Thanks!
EDIT:
Actually i went through all the answers before asking, and this will explain how my question is different.
A pop up looks like this (and i know how to open it):
And a new tab looks like this:
Simply put, i want to convert the popup in image 1 to the new tab structure in image 2.
Someone answered here.
function openInNewTab(url) {
var win = window.open(url, '_blank');
win.focus();
}
openInNewTab('http://www.google.com');
I've tried this code sample my self, in my browser's console and it worked fine.
Like the person who answered in the link above said, try calling openInNewTab() in onclick event of your DOM to prevent pop-up blockers.
You can try copy pasting the following line in your browser's console (tested on Firefox and Chrome).
It should open google in a new tab:
window.open('http://www.google.com', '_blank').focus();

Open link in new tab when user clicks

I want to create a jQuery script that opens a specific url in a new tab if the user clicks somewhere (no matter where). After that, the user should be able to click anywhere without getting a new tab at every click again.
Unfortunately I have no idea how to create such a script, and therefore I would appreciate all your answears :)
You can use the .one() event like
$(window).one('click', function() {
var url = "http://google.com";
window.open(url, "_blank");
})
_blank in window.open() is used to open the link in a new tab.
DEMO
Refer to following links for documentation on .one() and window.open()
$(window).one('click', function() {
var url = "http://google.com";
window.open(url, "_blank");
})

Allow user to change URL in window.open()

I have the following code which opens a new window when a user clicks on a button:
$('.loginButton').click(function () {
var strString = $("#medlineSearch").val()
var strSearch = "http://google.com&query=";
var url = strSearch + strString;
alert(strSearch+strString);
var windowName = $(this).attr('id');
window.open(url, windowName, "height=750,width=1250,scrollbars=1,resizable=1,location=1");
// custom handling here
return false;
});
What I am looking to do is allow the user to enter something else in the address bar which seems to be read only.
Is there any way to change that?
If you want to be able to change it, set the target to _blank.
This will prevent the new page to open as popup, but as new page.
window.open
( 'http://google.com'
, '_blank'
);
The jsfiddle.
You cannot change the url in popup window.
Read change url of already opened popup
Trick to do so
open console and type location.href='http://link.com'

How to open a new window in the same page

I have a button:
<button>Click</button>
I want click it and open a new window in the same page, I bind the click event an event handle:
$('button').click(function () {
var newurl="http://" + window.location["host"] + ";
window.open(newurl, '_parent');
})
But is always open in new tab or new window, the second parameter I have try _self _top.
I even have try window.location.href(newurl)
So how can I solve this problem?Does it matter with browser or OS? I view it in Mac OS's firefox and chrome.
Well that's not possible technically, it also depends on browser tab settings, window settings and third party tab manipulation plugins or addons.
Update
OP has cleared the confusion of all through his comment below his question, this is what you need to set a new url for current window:
$('button').click(function () {
var newurl="http://" + window.location["host"];
window.location.href = newurl; // or simply window.location
})
window.location =newurl; will open a new window replacing the parent
Have you tried this?
<script type="text/javascript">
window.open ('test.htm','_self',false)
</script>
If you mean you want to replace the current page, window.location = newLocation; will do.
If you want a modal popup, try JQuery UI Dialog.

how to open in new window

i have another problem :(
this is my script
<script type="text/JavaScript">
function first()
{
var first = confirm("Are you sure you want to navigate away from this page?");
if (first)
window.location = "http://www.yahoo.com.sg"
else
window.close;
}
function second()
{
var second = confirm("Are you sure you want to navigate away from this page?");
if (second)
window.location = "http://www.google.com"
else
window.close;
}
</script>
<p><b>Click the following link to enter yahoo</b></p>
<p>yahoo</p>
<p><b>Click the following link to check google</b></p>
<p>google</p>
its doesnt open in a new window.
please i need some help. any suggestion or recommendation?
Try window.open. This will open a new window:
window.open("http://www.google.com");
See also:
MDN: window.open usability issues
Use the window.open method and specify _blank as the target:
window.open('http://www.yahoo.com.sg', '_blank');
This will open the page in a new window or a new tab, depending on the user settings in the browser.
If you want it to open in a new popup window always, you can specify a parameter string with width and height for the window. However, most browsers will block such a popup that opens a page from a different domain.
If you are opening in a new window, you don't need to check with the user about navigating away.
function first()
{
window.open("http://www.yahoo.com.sg")
}
function second()
{
window.open"http://www.google.com")
}

Categories