So i am trying in JS and in HTML to make a button or a tag to open new tab in Default browser Androind and it's not working
This is my html and js code:
HTML
<button class="btn btn-success a-new-tab" data-href="{{ $url }}">My btn</button>
JS
$(document).ready(function () {
$('.a-new-tab').on('click', function (event) {
event.preventDefault();
window.open($(this).attr('data-href'), '_blank');
window.focus();
})
});
Please help me!
I had a same issue on iPhone/iPad Safari browser, so the below solution worked for me. Perhaps, it might help you as well.
HTML
<button class="a-new-tab" data-href="https://www.google.com/">My btn</button>
On button click, I'm getting the data-href and then set onclick attribute to the same button, and inside the attribute opening a new window
$(".a-new-tab").on("click", function () {
var url = $(this).attr("data-href");
$(this).attr( "onclick", "window.open('" + url + "'); return false;" );
});
Related
I want to open a success page in a new tab after clicking on the download link, but target _blank not working with download attribute. Please Help Me......
Button HTML
<a class="btn download-btn" id="myButton" href="https://www.uiuxstream.com/download/404-error-page-not-found-design-html.html" target="_blank" download>Download</a>
JavaScript
<script type="text/javascript">
document.getElementById("myButton").onclick = function () {
location.href = "/demo/thank-you-for-downloading.php";
};
in the <a>, remove the href, add `onclick="go()", and in a script, add:
function go() {
open("http://www.google.com", "_blank");
location.href = "http://mail.google.com";
}
Good luck!
if you are familiar with event listeners, you could use that instead to call go().
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
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 tried using a href=javascript:function() in a button, with a function to execute it. It works in Chrome but it doesn't work in Firefox.
Firefox doesn't alert and open blank tab.
Anyone can help me?
<script>
function verifyisbot() {
alert("test.");
var win = window.open("http://yahoo.com", '_blank');
win.focus();
}
</script>
Below is button code
<div class="frb_textcenter">
<a target="_blank" class="frb_button frb_round frb_center frb_fullwidth" href="Javascript:verifyisbot();">
click here
</a>
</div>
Update
I should have added that im using a live editor(profitbuilder) in wordpress to generate the page and button. There is no area for me to insert additional javascript onclick function to the button. So i figure out to use "ahref" blank field in the live editor to input javascript call function to fire up the function.
Is there any way i can make this work through the ahref without using onclick event? Or can i specify onclick function in the ahref field?
Sorry the test() is actually verifybot() function, typo mistake
You can achieve the goal using only the href attribute:
<a href="javascript:void(verifyisbot())" class="frb_button frb_round frb_center frb_fullwidth">
click here
</a>
It works, because when a browser follows a javascript: URI, it evaluates the code in the URI and then replaces the contents of the page with the returned value, unless the returned value is undefined. The void operator can be used to return undefined.
Use onclick event instead of a href=javascript.It works on firefox.See below:
<div class="frb_textcenter">
<a target="_blank" class="frb_button frb_round frb_center frb_fullwidth" onclick="test()">click here</a></div>
<script>
function test() {
alert("test.");
var win = window.open("http://yahoo.com", '_blank');
win.focus();
}
</script>
UPDATE 1: You can do it without use javascript.You just add the link in the href attribute.See below:
<a target="_blank" class="frb_button frb_round frb_center frb_fullwidth" href="http://yahoo.com">click here</a></div>
Give serious consideration to separating your JavaScript and your HTML such that the problem goes away. For instance, add an ID to your anchor and add an event handler through script:
<div class="frb_textcenter">
<a id="verify" target="_blank" class="frb_button frb_round frb_center frb_fullwidth" href="http://yahoo.com">
click here
</a>
</div>
Later...
<script>
function test() {
alert("test.");
var win = window.open("http://yahoo.com", '_blank');
win.focus();
}
window.onload = function () {
document.getElementById('verify').addEventListener('click', test);
};
</script>
Do note that with the example provided, you don't actually need JavaScript at all. The HTML itself will cause a new window/tab to open with Yahoo! loaded...
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');
});