Clicking in Safari - javascript

I need to open my app in new window by this link:
<a onclick="window.open('http://somesite1.com/', '_target', 'width=510,height=413,resizable=yes,scrollbars=yes').focus();">Link</a>
After opening the application handles events like:
$registerSubmitButton.on("click", function() {
var newWindow = window.open("https://somesite2.com", "_blank");
newWindow.opener.close();
return false;
});
But this click event doesn't work only in Safari.
Why?

I am not sure but if you cheat a little bit may this will work. I don't have a safari browser so just a try for you.
<a id="openWindow" onclick="window.open('http://somesite1.com/', '_target', 'width=510,height=413,resizable=yes,scrollbars=yes').focus();">Link</a>
<a id="fakeOpenWindow" style="display: none;" href="https://somesite2.com" target="_blank">
$('#openWindow').click(function(){
$('#fakeOpenWindow').click();
});

Related

Java Script inline function for Anchor tag

I'm trying to open a URL in new window and want to share the current page address using inline function as below:
a href="javascript:void(0);" onClick='(function()
{
var link = string.concat("example.com/UserStatus.phpid=99244613&utm_source=",window.location.href);
console.log(link);
});return false;'>click here</a>
But nothing is happening, please help.
The problem was that you were using string.concat but the right way to use is "".concat:
function fun(){
var link = "".concat("example.com/UserStatus.phpid=99244613&utm_source=", window.location.href);
console.log(link);
}
fun();
Output will be:
example.com/UserStatus.phpid=99244613&utm_source=https://playcode.io/
HTML:
<a href="javascript:void(0);" onClick='fun();'>click here
</a>
So in your code just use "".concat instead of string.concat.
This works fine.
<a href="javascript:void(0);" onClick="(function(){
var link = ''.concat(`example.com/UserStatus.phpid=99244613&utm_source=`,window.location.href); console.log(link);
return false;
})();return false;">click here
</a>
Working Codepen Link

Can't open PDF on new tab using target="_blank"

How do I open PDF on a new tab, target="_blank" alone does not work it still open the pdf in the same tab.
Method-1 : HTML
<a target="_blank" href="http://your_url_here.html">Link</a>
You can simply do that with setting target="_blank" for an example check this
More Details
Method-2 : Javascript
<a onclick="openInNewTab('Your URL');">Something To Click On</a>
function openInNewTab(url) {
var win = window.open(url, '_blank');
win.focus();
}
Without your code is hard to tell what's wrong but did a fast test and this worked for me..
<a target="_BLANK" href="pdf/your_pdf.pdf">YOUR PDF</a>
You have to know: "_Blank" is not working as a "new tab" on every browser.
To do that, you have to use js like this:
Lnk
(it will work on any browser, "_blank" will not)
EDIT: Of course, here the "link" in window.open will be the path to where your PDF file is stored.
EDIT2 (thanks to vlaz): Yep, it will work on any browser if JS is enabled, if he his not it will not.

a href=javascript:function() in firefox not working

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...

Why does the following script not open a new tab or page?

I am using a javascript function to navigate away from my website and to another website. I am also trying to open a new page when the button is clicked but instead it navigates on self instead of blank. Why does this happen?
HTML
<p style="text-align:center;">
<input style="font-size:large;" id="btamen" type="button" value="Visit Website">
</p>
JavaScript
<script type="text/javascript">
$("#btamen").click(function() {
window.location.href = 'http://www.google.com'
link.target = '_blank';
});
</script>
This line: window.location.href = 'http://www.google.com' will always replace current page with the new url.
I don't know what is link in the next page, but that code won't even execute because the page redirects and the context is lost.
If you want it like that, then do this:
window.open('http://www.google.com', '_blank').focus();
Also, you cannot guarantee its opening as browser might block it. Sort of kinda pop up blocker.
I don't think you can control it to open in a new tab. So you have to work around, what I did is:
<a href="" target="_blank" id="btamen_link">
<button type="button" id="btamen">
Click
</button>
</a>
And the jquery:
<script type="text/javascript">
$(document).ready(function(){
$('#btamen').click(function(){
$('#btamen_link').attr('href', 'http://www.google.com');
$('#btamen_link').click();
});
});
</script>

check if a browser tab is already open so I don't make extra tabs

When a user adds an item to our shopping cart it opens our store in a new tab. Different websites oddly enough.
I would like to check if the tab is already open and then repopulate it it with the second item instead of opening another tab with the updated cart.
Is there a way to check this with js? I imagine I can track that we opened the tab but I don't see how I can confirm that it wasn't closed in the time between adding items to the cart without doing some ajax requests pinging both pages etc. Which seems like overkill.
So simply how do you check if a browser tab is already open?
Edited with a solution:
First:
var tab = window.open('http://google.com','MyTab');
Then:
if(tab) {
var tab = window.open('http://yahoo.com','MyTab');
}
window.open has the following parameters: var tab = window.open(url, name, specs, replace)
As long as you use the same name the url will be loaded into that window/tab.
If you wanted to keep the descriptor/reference (tab above), that window.open returns, once the user refreshes the page, that reference is lost.
I think your best bet could be session storage / local storage, but it works only in newer browsers.
All you need is to save a reference to the opened tab that you can relate with some id that will make sense to you... Then when you need to reopen it again just use the saved reference from there you can access your parent or opener window from window.opener. Also to know when the child window is closed there is a default browser event "beforeunload" that when called can remove the window from your reference object in your parent so you know that you have to reopen it not just focus it.
I gone through each steps and I came up with some points.
I tested it on IE.
It did not worked as expected if you use URL like (htp://www.google.com) and it worked if you use your domain page.
While it worked well for Firefox and chrome.
Following example does not work:
<script type="text/javascript">
function myfunction1() {
window.open('http://www.google.com', 'f');
}
function myfunction2() {
window.open('http://www.yahoo.com', 'f');
}
</script>
<body>
<form id="form2" runat="server">
<div>
<a href="#" onclick='myfunction1();'>myfunction1</a>
<a href="#" onclick='myfunction2();'>myfunction2</a>
</div>
</form>
</body>
</html>
And Following example works:
<script type="text/javascript">
function myfunction1() {
window.open('WebForm1.aspx', 'f');
}
function myfunction2() {
window.open('WebForm2.aspx', 'f');
}
</script>
<body>
<form id="form1" runat="server">
<div>
<a href="#" onclick='myfunction1();'>myfunction1</a>
<a href="#" onclick='myfunction2();'>myfunction2</a>
</div>
</form>
</body>
</html>

Categories