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'
Related
Guys I am trying to open a div in new window and I tried with window.open ,its working as expected.
HTML:
<div id="pass">pass this to the new window</div>
click
JS:
function openWin() {
var divText = document.getElementById("pass").outerHTML;
var myWindow = window.open('', '', 'width=200,height=100');
var doc = myWindow.document;
doc.open();
doc.write(divText);
doc.close();
}
http://jsfiddle.net/KZYJU/
Issue : If the window is already opened, when I click the button again, it should not open a new window again.
User should be able to see only one window.
In my case if I click the button 10 times ,10 windows is getting opened.
Guys please assist me on this.
You can do it like this:
var popup;
function openWin() {
if (popup && !popup.closed) {
popup.focus();
/* or do something else, e.g. close the popup or alert a warning */
} else {
var divText = document.getElementById("pass").outerHTML;
popup = window.open('', '', 'width=200,height=100');
var doc = popup.document;
doc.open();
doc.write(divText);
doc.close();
}
}
This will check if the popup window is open or not, and if it is then it will focus it.
Demo
Since you can write some script into the new window, you could use cookies to store a flag-like data in it so that you know the window is currently open.
On the main page, you first set the flag (I will name it windowOpen) to false, and check every time the link is clicked. If it is false then open new window; otherwise, do nothing.
On the subsequent window, set the flag to true on open and set it to false on close.
Set a window name in your call to window.open
window.open('', 'popupWindowName', 'width=200,height=100');
windowName is a DOMString specifying the name of the browsing context (window, or tab) into which to load the specified resource; if the name doesn't indicate an existing context, a new window is created and is given the name specified by windowName. This name can then be used as the target of links and forms by specifying it as the target attribute of or elements. The name should not contain whitespace. Keep in mind that this will not be used as the window's displayed title.
If the string is empty, the browser will create a new window every time (this behaviour doesn't work when the string is replaced with NULL).
Source
Just play with the Boolean flag. make it true when you click it first time. and restrict it with that.
var flag = false;
function openWin() {
if(!flag) {
var divText = document.getElementById("pass").outerHTML;
var myWindow = window.open('', '', 'width=200,height=100');
var doc = myWindow.document;
doc.open();
doc.write(divText);
doc.close();
flag = true
}
}
<div id="pass">pass this to the new window</div>
click
I the below script, once the button is pressed, a new browser tab with Google is opened. I then want to fill the search box on this newly opened tab.
How do I switch focus to this new tab, so that I can wait for it to load completely and then fill the search box?
<button id="start">Google</button>
<script>
function open_google_in_new_tab() {
var url = "https://www.google.com";
window.open(url);
window.onload(fill_search_box()); // here it will try to operate on the old tab
};
function fill_search_box() {
var search_box = document.getElementsByName("q")[0];
search_box.value = "Some text";
};
var start_button = document.getElementById("start");
start_button.addEventListener("click", open_google_in_new_tab);
</script>
You can't exactly target another element in another document from your script, however, you can set the textbox value of the Google textbox using a query string:
www.google.com?q=SearchTerm
When creating the SearchTerm you should also use encodeURI to ensure that your attached search query meets URL standards
Also, if you wish to open the window in another tag you can use window.open(url, "_blank") to open.
See example below:
Note:- The page will not open due to snippet restrictions - so run in your own browser.
<button id="start">Google</button>
<script>
function open_google_in_new_tab() {
var text = "Some text";
var url = "https://www.google.com?q=" + encodeURI(text);
console.log(url)
window.open(url, '_blank');
};
var start_button = document.getElementById("start");
start_button.addEventListener("click", open_google_in_new_tab);
</script>
You cannot do that with Javascript unfortunately.
However, this might make you achieve something similar if you are willing to try.
You can manipulate the search string via parameter named "q" in the query string of the Google website. So, in order to make the user to go the Google and search this particular word, you attach the keyword to it. Something like this:
var url = "https://www.google.com?q=searchKeyword";
I am using this bit of jQuery code to get href of the link:
var url = $(this).attr('href');
-- and this bit of code to go to that href:
window.location = url;
Everything is just the way I want it, except the new page opens in the same window as the previous one, and I want it to open in a new window or tab (something that in plain html would have been achieved by using target="_blank" formula).
Question: How can I open the href in the new window or tab with jQuery?
Thank you for your help!
You need to open a new window:
window.open(url);
https://developer.mozilla.org/en-US/docs/DOM/window.open
Use,
var url = $(this).attr('href');
window.open(url, '_blank');
Update:the href is better off being retrieved with prop since it will return the full url and it's slightly faster.
var url = $(this).prop('href');
Question: How can I open the href in the new window or tab with
jQuery?
var url = $(this).attr('href').attr('target','_blank');
The .ready function is used to insert the attribute once the page has finished loading.
$(document).ready(function() {
$("class name or id a.your class name").attr({"target" : "_blank"})
})
Detect if a target attribute was used and contains "_blank". For mobile devices that don't like "_blank", this is a reliable alternative.
$('.someSelector').bind('touchend click', function() {
var url = $('a', this).prop('href');
var target = $('a', this).prop('target');
if(url) {
// # open in new window if "_blank" used
if(target == '_blank') {
window.open(url, target);
} else {
window.location = url;
}
}
});
If you want to create the popup window through jQuery then you'll need to use a plugin. This one seems like it will do what you want:
http://rip747.github.com/popupwindow/
Alternately, you can always use JavaScript's window.open function.
Note that with either approach, the new window must be opened in response to user input/action (so for instance, a click on a link or button). Otherwise the browser's popup blocker will just block the popup.
Try using the following code.
$(document).ready(function(){
$("a[#href^='http']").attr('target','_blank');
});
url='/controller/action';
window.open(location.origin+url,'_blank');
I have the landing page with some buttons to function.
When I click the Send Money button, it opens the sendMoney.html in the same tab. The code is following,
$("#sendMoney").click(function () {
var address = $('#address').find(":selected").text();
location.href = 'sendMoney.html#' + address;
});
How to open the sendMoney.html in the new tab?
To do what you require you can use window.open(). Although the name suggest it'll open a popup window, the request will actually be redirected to a new tab in all modern browsers. Try this:
$("#sendMoney").click(function () {
var address = $('#address').find(":selected").text();
window.open('sendMoney.html#' + address);
});
$("#sendMoney").click(function () {
var address = $('#address').find(":selected").text();
window.open('sendMoney.html#' + address);
});
Instead of using window.location, use window.open("your link");. With window.location its not possible to do that.
You can use <a> button text here <a/> to make a button and use attribute target as _blank to open page in new tab
Or
you can use window.open() function as
window.open(URL, name,)
Value for name can be
blank - URL is loaded into a new window. This is default
_parent - URL is loaded into the parent frame
_self - URL replaces the current page
_top - URL replaces any framesets that may be loaded name - The name of the window (Note: the name does not specify the title of the new
window)
You need to use window.open() which will open new tab in most of the new browsers. Here is the updated code:
$("#sendMoney").click(function () {
var address = $('#address option:selected').text();
window.open('sendMoney.html#' + address);
});
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");
})