Open anchor href always in same tab [duplicate] - javascript

I am facing some challenge-
Lets consider I have a menu and under that I have 3 links-
About
Contact Us
FAQs
By default home page is About menu item.
Now when I am clicking on "Contact Us" , I want to open a new tab on same browser window. And without closing this if again I am clinking on "Contact Us" from the first tab, this time I do not want to open new tab because I have already an open tab for Contact us, I just wanted to navigate to the same one which is earlier opened.
How to handle this scenario in jsp.
Is there any way to do this. please help me.

you can always put a name in the target so it will always go to the same window/tab
<a href="somerandomurl.com" target="mywindow">Contact Us<\a>
Its not possible to control this on Internet explorer.
You can however change how IE behaves:
-Internet Options
-Tabs
-Always open pop-ups in a new tab
-Open links from other programs in: A new tab in the current window
target="_blank" will always make sure it opens in a new window
http://www.w3schools.com/tags/att_a_target.asp
It will however refresh the page with the same url
You can have the link return a javascript window object and be able to return to the window anytime you want without refreshing the page
var newtab = null;
function openwindow(){
if (newtab==null) {
newtab = window.open("www.example.com");
} else {
newtab.focus();
}
}
<a onclick="openwindow()">click me</a>

This solution will work if someone refresh the parent page also, it will close the child tab if that tab is already open on refresh of parent page.
Javascript:
<script>
var newtab = null;
window.open('', 'business-list', '').close();
function openwindow() {
if (!newtab || newtab.closed) {
newtab = window.open("/admin/business-list.aspx","business-list");
} else {
newtab.focus();
}
}
</script>
HTML:
<a onclick="openwindow()" style="cursor:pointer">Business List</a>

Related

Redirect user to home page without reloading page

After clicking okay on my window.prompt a new tab opens using the code below. I want the user to go to the home page without opening a new tab.
if (window.confirm("Do you really want to leave?")) {
window.open("./Tutorial2", "Thanks for Visiting!");
}
The window.open() (method), will open in a new window. You can use window.location (object) along with .href = "urladdress" to redirect your browser to a new url address without opening a new tab.
See the following article on MDN: Window.location for more info.

Select browser tab that was opened by current tab

I have a web page with a link that opens another page in a new tab. In the newly opened page, I have a link that, when clicked, should bring focus to the original tab.
The new tab is opened via the target attribute:
new tab
I thought this should work:
switch tabs
$('.back-link').click(function() {
if(window.opener) {
console.log('switching tabs');
window.opener.focus();
}
});
The console.log fires, but focus stays on the current tab. What am I doing wrong?
Could you try adding the following event listener to your new tab button instead of target="_blank" :
$('.new-tab-button').click(function(e) {
e.preventDefault();
window.open('/new-page/');
});
This way you actually open a new window instead of new tab, according to the stackoverflow thread linked below, for security reasons, it seems to only be possible to focus to (and maybe from) windows/pop-ups. I'm not sure if what you're trying to do is possible but if so this could be it.
How to change browser focus from one tab to another

Can i call a java script function to redirect to a particular view if browser refresh button is clicked?

If at any point of time the user clicks on refresh icon on the browser, I want to redirect the user to a view ("UploadFilesView").
Is it possible to achieve? if yes, any code example would make my day!
My code is in MVC, using JavaScript and ajax.
$("#btnRefreshIcon").click(function () {
var url = '#Url.Action("UploadFilesView", "<ControllerName>")';
var win = window.open(url, '_blank'); // _blank - To open view in next tab and
// _self - To open view in same tab
win.focus();
});

How do you create a link which when clicked takes you to a page and also opens a new tab going to another url?

I want to have a link which when clicked preforms two actions:
redirects the current tab of the browser to url A.
opens a new tab directing the browser to url B
How can I do this? Is there HTML for this? Should I use javascript?
You would need to use javascript to achieve this, something along the lines of this:
$('#foo').click(function(e) {
e.preventDefault(); // stop the normal link behaviour so you can open a new window
window.open('http://foo.com/new-page'); // open new tab
window.location.assign($(this).prop('href')); // go to new page in current tab
});
Without jQuery
link // add onclick event
<script>
function f(){
document.location='/a.html'; // open in same tab
window.open('/b.html','_blank'); // open new tab
}
</script>

How to refresh another page using javascript without opening the same page in a new tab

Is it possible to refresh a page from another page using Javascript or JQuery without opening the same page in a new tab.
JS:
var newtab = window.open('http://localhost:8081/app/home');
newtab.document.location.reload(true);
I tried the above, but here, it will open a new tab, with the same page, which is already opened in the browser.
Please suggest a method.
I got the idea from a previous Question , here they used window Object Reference to reload the popup window, but for me it wont work, because, the parent window and child window runs in 2 different ports. So using the same trick, what i did is :
HTML:
<a onclick="openNewTab()">app2</a>
<a onclick="refreshExistingTab()">Refresh</a>
JS:
<script>
var childWindow = "";
var newTabUrl="http://localhost:8081/app/home";
function openNewTab(){
childWindow = window.open(newTabUrl);
}
function refreshExistingTab(){
childWindow.location.href=newTabUrl;
}
</script>
refreshExistingTab() this instend of refreshExistingTab
take a look at https://developer.mozilla.org/en-US/docs/Web/API/Window.open
basically if you do window.open and specify a window name it will overwrite that window with the url you provided.
so if you open the page each time with same window name, it should overwrite it each time you do it again from that other page.

Categories