I am making a website and I want a new tab to open at a specific address when I click a button.
This is the html for my button:
<button id="AsDownload" onclick="AsDownload();">Download</button>
And this is my javascript:
function AsDownload(){
chrome.tabs.create({url:"https://alexanderhawking.itch.io/asteroid-racer"});
};
But it is not working and I can't figure out why, can someone help me?
In addition to the other answers, add "_blank" to open in a new tab if you intend on using a JavaScript function.
function AsDownload() {
//window.open(pathString, target);
window.open("https://alexanderhawking.itch.io/asteroid-racer", "_blank");
};
<button id="AsDownload" onclick="AsDownload()">Download</button>
You can use window.open() to do that.
<input type="button" value="button name" onclick="window.open('http://www.website.com')" />
Your example:
function AsDownload() {
window.open("https://alexanderhawking.itch.io/asteroid-racer", "_blank");
};
Did you try window.open() ? It opens URL in new tab.
If you still want it to be a button rather than an anchor tag <a> use this in your function:
window.open("https://alexanderhawking.itch.io/asteroid-racer");
you can integrate a link and a button like that
<button id="AsDownload">load</button>
this would open up in a new tab
Related
I'm working on a website built using Laravel and AngularJS.
I want a certain link to open in a popup window.
The javascript code is
function popitup(url) {
newwindow=window.open(url,'test','height=400,width=550');
if (window.focus) {newwindow.focus()}
return false;
}
and the link is
<a ui-sref="test({id:test.id})" class="btn btn-primary fright" onclick="return popitup(this.href)" oncontextmenu="return false;">Resume</a>
when I click on the button the popup works fine but the link also opens up in the tab where I clicked it, but I don't want it to.
Is there a way to do it?
I would guess ui-sref will also bind a click-event and that event will trigger before yours.
You could skip the ui-sref and put the url in a data-attribute or inside the onclick-attribute. You could get the url using $state.href() instead. Then the problem may disappear.
Edit
You could bind it to a scope function and skip onclick all toghether. Something like this:
In the Controller (Also make sure you include $state in the controller first):
$scope.popitup = function(stateName, options) {
var url = $state.href(stateName, options);
newwindow=window.open(url,'test','height=400,width=550');
if (window.focus) {newwindow.focus()}
}
And in the HTML you invke the popuitup function with the state name and its parameters:
<a ng-click="popitup('test', {id:test.id})"
class="btn btn-primary fright" oncontextmenu="return false;">Resume</a>
Also see documentation for state.href.
<!DOCTYPE html>
<html>
<body>
Popup-window
</body>
</html>
try this code for popup window and change google.com to you site
I have a little problem regarding my code in wordpress. I am using a gravityform plugin in wordpress. But it seems that I cannot set it to open a new tab after I clicked the "submit" button.
below is the bottom properties when being inspect:
<input type="submit" id="gform_submit_button_5" class="button gform_button" value="Submit" tabindex="10">
I tried all the javascript here in stackoverflow but it didn't work.. :(
This is the code I used to call button to open in new tab.
<script type="text/javascript">
jQuery(function() {
jQuery(".gform_submit_button_5").attr( 'target', '_blank');
});
</script>
You should add target="_blank" to your <form> element and not on the button. Can you please try that see ?
try that:
DEMO
jQuery(function() {
jQuery("#gform_submit_button_5").attr( 'formtarget', '_blank');
});
i need to open two links when a button is clicked in the html page. I figured it as by calling onclick function and creating anchor tag using createElement in Javascript. But how to include another link?? Is there a way to give a href in button tag??
You can simply do that with javascript
window.open(url1);
window.open(url2);
And if you want to open one of that links in curren window you can replace window.open by this
window.location = url1;
<input type="button" value="Double Clicker" onclick="window.open("http://www.google.com/"); window.open("http://www.youtube.com/");" />
see this link for further information,
You need to use a javascript event to make the page go somewhere
<input type="button" name="button1" value="Go To Url" onClick="window.navigate('URL')">
You can also use
location.href=" ";`
Try to see what the location object can do => http://www.w3schools.com/jsref/obj_location.asp
You should be able to load two tab.
why dont u use bootstrap
<a href="#" class="btn btn-default">
It displays a button with link going to what is mentioned in href
I would like to have a button that redirects to a given URL and opens in a new tab. How can this be done?
Use this:
<input type="button" value="button name" onclick="window.open('http://www.website.com/page')" />
Worked for me and it will open an actual new 'popup' window rather than a new full browser or tab. You can also add variables to it to stop it from showing specific browser traits as follows:
onclick="window.open(this.href,'popUpWindow','height=400,width=600,left=10,top=10,,scrollbars=yes,menubar=no'); return false;"
In javascript you can do:
window.open(url, "_blank");
Adding target="_blank" should do it:
<a id="myLink" href="www.google.com" target="_blank">google</a>
You can forget about using JavaScript because the browser controls whether or not it opens in a new tab. Your best option is to do something like the following instead:
<form action="http://www.yoursite.com/dosomething" method="get" target="_blank">
<input name="dynamicParam1" type="text"/>
<input name="dynamicParam2" type="text" />
<input type="submit" value="submit" />
</form>
This will always open in a new tab regardless of which browser a client uses due to the target="_blank" attribute.
If all you need is to redirect with no dynamic parameters you can use a link with the target="_blank" attribute as Tim Büthe suggests.
My preferred method has the advantage of no JavaScript embedded in your markup:
CSS
a {
color: inherit;
text-decoration: none;
}
HTML
<input type="button" value="Link-button">
Use window.open instead of window.location to open a new window or tab (depending on browser settings).
Your fiddle does not work because there is no button element to select. Try input[type=button] or give the button an id and use #buttonId.
Open in new tab using javascript
<button onclick="window.open('https://www.our-url.com')" id="myButton"
class="btn request-callback" >Explore More </button>
I just used target="_blank" under form tag and it worked fine with FF and Chrome where it opens in a new tag but with IE it opens in a new window.
try this
<a id="link" href="www.gmail.com" target="_blank" >gmail</a>
Use button as anchor tag
<button
target="_blank"
rel="noreferrer"
as="a"
href="https://example.com"
>
Open
</button>
Try this HTML:
<input type="button" value="Button_name" onclick="window.open('LINKADDRESS')"/>
You can't. This behaviour is only available for plugins and can only be configured by the user.
<BUTTON NAME='my_button' VALUE=sequence_no TYPE='SUBMIT' style="background-color:transparent ; border:none; color:blue;" onclick="this.form.target='_blank';return true;"><u>open new page</u></BUTTON>
This button will look like a URL and can be opened in a new tab.
USE this code
function openBackWindow(url,popName){
var popupWindow = window.open(url,popName,'scrollbars=1,height=650,width=1050');
if($.browser.msie){
popupWindow.blur();
window.focus();
}else{
blurPopunder();
}
};
function blurPopunder() {
var winBlankPopup = window.open("about:blank");
if (winBlankPopup) {
winBlankPopup.focus();
winBlankPopup.close()
}
};
IT works fine in Mozilla,IE and chrome on and less than 22 version; but doesn't work in Opera and Safari.
I am using self.location="www.google.com"; in my code to open google page. How can i open same page in another window.
You can use the window object's open method like so:
window.open("www.google.com");
You can use an <a> tag and set the target="_blank" property to open a page in anew tab/window. Whether this will be opened in a new tab/windows depends entirely on the settings in the user agent.
Google
try this
window.open ("www.google.com","mywindow");
<script>
function fullwin(targeturl) {
window.open(targeturl,"","fullscreen,scrollbars")
}
</script>
<form>
<input type="button" onClick="fullwin('mypage.html')" value="Show My Frameset">
</form>
i see at least one freaky method:
<a id='newwnd' href='goole.com' style='display:hidden' target='blank'></a>
...
document.getElementById('newwnd').click();