Open New Tab When User Clicks on Link to External Website [duplicate] - javascript

This question already has answers here:
Open a URL in a new tab (and not a new window)
(33 answers)
Closed last year.
I'm developing a recommendations application written in React, and want the user to be able to be sent to external websites when the component containing the website's information has been clicked.
Currently, I have a wrapper that, when clicked, pushes the new URL to the history object from the React library. The source code is shown below.
<VStack onClick={() => history.push(<url here>)}>
<inside of component>
</VStack>
Unfortunately, the URL is being loaded in the same window, but I want the user to remain on the website, in the original tab.

Update your onClick function to
onClick={() => window.open(urlHere)}
see more of window.open here

You can perhaps try setting the target attribute to "_blank" in the Link tag itself.
example:
<Link to="/somewhere" target="_blank">anything</Link>

Related

window.open is opening a new tab with orgin/url [duplicate]

This question already has answers here:
window.open() simply adds the url to my current url
(4 answers)
Closed 4 years ago.
I have a button that is located inside iframe with url="loclahost:3000" for example.
I have an event on this button that should open a new tab with another url for example "www.google.com". But when I click the button I am getting the new tab opened with the following url "http://localhost:3000/www.google.com"
I am using a window.open('www.google.com") function in order to open the new tab.
How can I remove the "http://localhost:3000/" from the new tab url?
The best way to do so would be to use an absolute link, which cancels out any relativity the URL might have:
var url = "https://www.google.com";
window.open(url);
Here the https:// signifies it's a completely different page to the current page, so the browser should open that link, not try and find a file by that name.

Why is my URL called in JavaScript opening as pop-up [duplicate]

This question already has answers here:
Open a URL in a new tab (and not a new window)
(33 answers)
Closed 8 years ago.
Browser settings vary for allowing popups and I want my URL which is called within a JavaScript function to open in a new tab and not be considered a popup.I am opening the url as:
window.open(my_global_link,'_blank');
In IE and FireFox, when I click on the image I get a notification Pop-up blocked and only by Allowing pop-up for the site the URL would open in a separate tab. How can I make it open in a separate tab and not consider it a pop-up?
It is a browser setting . try finding navigation options for new tabs in the browser you are using and set it to open in a new tab instead off a new window.

Is there any way to define back/forward button effects on a web page? [duplicate]

This question already has answers here:
Javascript : Change the function of the browser's back button
(5 answers)
Closed 8 years ago.
I have a page where the content is delivered mostly though javascript. Hitting the back button would basically exit the page to wherever the user came from instead of showing the previous content. Is there a way to take manual control over what the back/forwards buttons do on a web page?
This is a classic AJAX problem - each page is loaded asynchronously with no new GET from the browser, thus no history.
Look into History API of HTML5 to programatically push page state into the browser history. Other than that you are out of luck.

Open redirect page in new tab and reopen in the same tab on multiple redirects Django

I am making a blog app in Django and I want to do the following things
When the user submits the preview button I want to open the partially completed blog in a new tab in the same window.
Secondly if the user does not close the tab and again presses preview button, I want to display the updated blog in the tab which was previously opened.
How can I do this?
Edit:
As told in the answer below I am trying to use this code snippet to open it in a new window, but this does not work
<script>
var current_link = $(location).attr('href');
$('.preview_button').onClick(function() {
window.open(current_link,'preview_tab');
});
</script>
Also I currently have 3 submit buttons and I want to provide this feature only for 1 submit button i.e. preview So I cannot directly do (#form).onSubmit. For the the other two buttons, one updates the content via Ajax and the other redirects to a new page.
Try using a Javascript onSubmit to open the appropriate preview page with window.open, passing a name as the second parameter. It does almost exactly that. (If you want to support having different preview tabs associated with different editing tabs, include something in the name based on a tab ID of some kind -- maybe the article ID plus a random number, or something.)
You'll have to also send the updated content into the server via AJAX.

How to Open the link in new tab [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Programmatically open new pages on Tabs
I have a link in my page and i want it to be opened in a new tab when the user clicks it?
That depends on the users browser settings. You can't control that as far as I know. All you can do is get it to open in a new windows, but if the user has his/her browser setup to open new windows in a new tab, it will do that.
So basically, the best you can do is open in a new window.
target="_blank"
i.e.
link text here
Most browsers treat a _blank target as opening in a new tab.
Set the target to _blank:
Use the target attribute set to _blank:
example
The exact behavior depends on the browser and user settings, so this is the best one can do.
This is considered bad practive because you (the web developer) is trying to control what the users browser is doing. It should be up to me how I want to open the link. Other than that lecture (and I understand that this may not be your decision), it doesn't seem that there is a way to do this.

Categories