I'd like to open a pre-determined webpage and execute a click event on it when it opens.
As an example, here's an experiment I did with a google power search:
$("#lnkSearch").click(function() {
console.log("you clicked Search");
$(this).attr("target", "_blank");
// build the search
var search = $("#txtSearch").val();
search = search + "+site%3Aloganyoung.za.net";
$(this).attr("href", "http://www.google.com/search?q=" + search);
//window.open($(this).attr("href"));
});
Now to add onto this, I have it in mind to open the first link in the search results automatically.
Is there a way that I can do this in the new window before I open it?
Related
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 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 have been asked to track when a user drags a link on our site in to a brand new tab. Is there a drag property that I can use to tell if a new tab was created?
I have tried looking at the dragEnd event but the thing that I could find that might help would be the pageX and pageY. Could it be possible to use these to work out if the link was used to create a new tab?
Cheers
Using TestComplete with C#Script might help you.You can simulate a click to this link and then check whether a new page object with the target URL has appeared.
function Test1()
{
var browser = Sys.Browser("firefox");
var numOfTabs = browser.FindAllChildren("ObjectType", "Page").toArray().length;
var page = browser.ToUrl("http://www.w3schools.com/html/tryit.asp?filename=tryhtml_links_target");
var pageUrl = page.Url;
frame = page.Panel(0).Panel(1).Panel(0).Panel(1).Frame("iframeResult");
frame.Link(0).Click();
if (page.Url != pageUrl)
Log.Error("The page's URL has been changed!");
if (browser.FindAllChildren("ObjectType", "Page").toArray().length == numOfTabs)
Log.Error("A new tab has not been opened!");
}
In my MVC project I have a button which opens a page in new tab. On button click, the page is opened on new page and the button should be disabled. When the user closes the tab, I want to enable the button. Is there any way I could do this in jquery or javascript? I have searched the web but no answer.
Conceptual idea would be to maintain a reference to the opened window in outer context (module or global variable), so that it can be easily accessed from your code. Here is the example:
var windowsArray = [];
function addWindowInTab() {
var newWindow = window.open('page.html', '_newtab');
windowsArray[windowsArray.length] = newWindow;
newWindow.onunload=enableButton;
}
function removeWindowTab(newWindow){
var idx = array.indexOf(newWindow);
if(idx != -1) {
windowsArray.splice(idx, 1);
}
}
function enableButton(){
$("#myButton").removeAttr("disabled");
}
//somewhere in MVC page
$(function(){
$("#buttonOpenWindow").click(function(){
addWindowInTab();
});
//later, when a button should be disabled...
if (windowsArray.length){
$("#myButton").attr("disabled", "disabled");
}
});
Note also, that here you can perform some more advanced checks, such as if window with particular name is opened or if multiple windows are opened. Bear in mind that in order to open a new tab, the AddWindowInTab function must be called in response to user action, such as pressing Ctrl+ clicking the left mouse button
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'