Button url action to new tab
You want to create a button that, when clicked, opens a URL in a new tab. Here are a few ways to achieve this:
Method 1: Using HTML and JavaScript
HTML:
<button onclick="window.open('https://www.example.com', '_blank')">Open in new tab</button>
In this example, the onclick
attribute is used to specify a JavaScript function that opens the URL in a new tab. The window.open()
function takes two arguments: the URL to open and the target attribute (_blank
in this case, which opens the URL in a new tab).
Method 2: Using HTML and the target
attribute
HTML:
<a rel="nofollow" target="_blank" href="https://www.example.com" target="_blank"><button>Open in new tab</button></a>
In this example, the target
attribute is used to specify that the link should open in a new tab. The href
attribute specifies the URL to open.
Method 3: Using JavaScript and the window.open()
function
JavaScript:
document.getElementById("myButton").addEventListener("click", function() {
window.open("https://www.example.com", "_blank");
});
HTML:
<button id="myButton">Open in new tab</button>
In this example, the JavaScript code adds an event listener to the button element. When the button is clicked, the window.open()
function is called with the URL and target attribute specified.
Method 4: Using a library like jQuery
JavaScript:
$("#myButton").on("click", function() {
window.open("https://www.example.com", "_blank");
});
HTML:
<button id="myButton">Open in new tab</button>
In this example, the jQuery library is used to add an event listener to the button element. When the button is clicked, the window.open()
function is called with the URL and target attribute specified.
These are just a few examples of how you can create a button that opens a URL in a new tab. The method you choose will depend on your specific use case and requirements.