Opening a new tab/window after an AJAX call - javascript

I need help making an ajax call and using the response to open a new tab; the code I have sort of works but if the function is called more than once, it will simply replace the contents of the first tab instead of opening another one.
function openInNewTab(url){
let newTab = window.open("about:blank", "new_tab")
$.ajax('api/path').done(function(response){
newTab.location = url + '&data='+response;
})
}
How can I make it so a new tab will be opened each time the function is called?

Try this (url should be full qualified url, e.g. http://www.example.com/path/to/your/file/and/so/on)
function openInNewTab(url){
$.ajax('api/path').done(function(response){
window.open( url + '&data='+response, "_blank");
})
}

Related

Opening the result of GET-request with data in a new tab

I am struggling to find a way in Ajax and JQuery to make a GET-request with data parameters, and to render the response in a new tab in the browser. Something like this:
$('#test-button').on('click', function(e){
var data = {'some_param': [1,2,55,44,3]}
$.get('/test_url/', data, function(html_response) {
// render the HTML contained in html_response in a new tab
});
});
You can do it with:
var newWindow = window.open();
newWindow.document.write(html_response);
But the browser will show a popup alert and you will need to allow it manually.
Working Example:
https://jsfiddle.net/hc3b38bu/

Why opening a product page in eBay redirects to a different URL?

When I open the following url in browser,
http://pages.ebay.com/link/?nav=item.view&id=141628727383&alt=web
It redirects to a different url
http://www.ebay.com/itm/141628727383
What's the mechanism?
Because it uses some kind of JS redirection:
var eULin;
window.onload = function() {
eULin = new eUL();
eULin.version = '1.4.1+1424198141014';
eULin.redirect();
}
eUL is defined in http://pages.ebay.com/link/univlink.min.js
eUL.prototype.redirect calls eUL.prototype.winRedirect, which calls location.replace. That replaces the current page with a new one, in a way that the current one won't be accessible using the back button.

Chrome extension to copy current URL and open tab to perform actions

I am new to chrome extensions and trying to work this out.
When I click on the extension button, it should copy the current url and open google.com in a new tab then place this url in searchbox or console print the url. Basically,
Copy the url to new tab, which can be used to perform further actions.
We can open new tab using.
chrome.browserAction.onClicked.addListener(function(tab) {
var action_url = "http://www.google.com"
chrome.tabs.create({ url: action_url });
});
Not sure how to call functions/perform actions after the new tab openx
You want to do two things :
1) Copy the current url.
2) Do something with it in the new tab opened with google.com as the url.
You can get the current url in javascript using: window.location.href
You have a callback function for chrome.tabs.create(object createProperties, function callback) as here and use this callback function ( which is called once the tab is successfully created) to pass current url to content script using message passing.
copy the current url using ** window.location.href**
make a new string by concating the string obtained in step 1 with https://www.google.co.in/search?q= i.e new string would be https://www.google.co.in/search?q=www.google.com (if you want to search google.com in new tab)
write a javascript function in content script to open new tab and search for the string obtained in step 2.

what is return type of window.open() in Javascript

I am writing code to download a PDF file using window.open(). I am passing the URL path of the pdf file on a server.
window.open(url, name, "width=910,height=750,scrollbars=yes");
I want to check if the downloading of file is successful or not. What is the return type of window.open()?
I tried like this
try
{
window.open(url, name, "width=910,height=750,scrollbars=yes");
alert("success");
}
catch(e)
{
alert("failer");
}
When I change the URL to a wrong URL, it shows the same result as a success.
http://www.javascripter.net/faq/openinga.htm
The return value is the reference to your new window. You can use
this reference later, for example, to close this window
(winRef.close()), give focus to the window (winRef.focus()) or perform
other window manipulations.
Window.open either returns a handle to the new window opened or null, it will not tell you if the page within the window loaded successfully. If you where opening an html page (from the same domain) you could use this to look into the document
var newWin = window.open();
if(newWin == null) {
alert("darn");
}
newWin.document.getElementById("anElement").innerText = "Fish";

Open link in new browser?

I have the following code below in a javascript file and need to have the link that is being generated open in a new window.
if (currentSearchType === 'extSearch') {
extSearchSearchValue = extSearchSearchInput.val();
window.location.href = replaceByObject(global.uhg.data['general'].body.extSearchSearchUrl, {
q: extSearchSearchValue
});
Normally with javascript I believe you'd use a window.open type of function, but not sure how to incorporate that with this type of code.
However you do it, opening a new browser window with javascript will most probably be blocked by popup blockers, so perhaps you should rethink your approach to the user himself clicking a regular link, then you can use target="...".
Just use a var to hold the URL and then pass it to window.open()...
if (currentSearchType === 'extSearch') {
extSearchSearchValue = extSearchSearchInput.val();
var url = replaceByObject(global.uhg.data['general'].body.extSearchSearchUrl, {
q: extSearchSearchValue
});
window.open(url, 'searchWindow');
}

Categories