I want to open a url in new tab.
I am using following code to open url in new tab.
var url = "/Billing/DownloadEDIForMultipleClaim?month=" + month + "&BillRequestIds=" + billRequestIds.join(',') + "&PatientIDs=" + patientIds.join(',');
window.open(url, '_blank'); //exception here
But this is not working for me it through an exception Uncaught TypeError: Cannot read property 'open' of undefined
I also used following.
var url = "/Billing/DownloadEDIForMultipleClaim?month=" + month + "&BillRequestIds=" + billRequestIds.join(',') + "&PatientIDs=" + patientIds.join(',');
var win = window.open(url, '_blank');
if(win) {
//Browser has allowed it to be opened
win.focus();
} else {
//Broswer has blocked it
alert('Please allow popups for this site');
}
I already seen related questions.
How to open a URL in a new Tab using javascript or jquery?
How to open a link in new tab using javascript
But this is not working for me.
This may be of use:
http://jsfiddle.net/vraCM/12/
it seems that this code snippet contains what you need for this to work.
$(function() {
$("a[href^='http']:not([href*='jsfiddle.net'])").each(function() {
$(this).click(function(event) {
event.preventDefault();
event.stopPropagation();
window.open(this.href, '_blank');
}).addClass('externalLink');
});
});
The js fiddle contains code that can be manipulated to fit your parameters regarding pop-ups within an alert or new window etc.
Also, this article may be worth a read. http://www.jqueryfaqs.com/Articles/Open-new-tab-in-browser-using-jQuery.aspx
Related
win giving me NULL value. I want open script in new tab but here window.open not working. Here I have use window.open method. But when I try to console it, it giving me null value.
$scope.viewScript = function (url) {
console.log(url);
$http.post(server_url + '/carrier_script', {
access_token: access_token,
carrier_id: url,
}).success(function(data)
{
console.log("data",data.data[0].script_file)
var script_url=data.data[0].script_file;
console.log("script_url",script_url);
setTimeout(function(){
console.log(script_url)
var win= window.open(script_url, "_blank");
// console.log(win);
// win.focus();
console.log(win)
},2000)
})
// var win = window.open(url, '_blank');
// win.focus();
I think there is no problem with your code, but in the modern web browsers the users can turn on/off popup windows. So you might enable the popups first and then your code will work.
Before I get into the long-winded explanation and the code, let me just say that I understand that my implementation of this system is a bit of a hack-job. The goal was to implement a linking feature on a SPA application without completely overhauling what was already done with Angular and the Bootstrap modals. I'll wager that I probably could have accomplished something better with directives, but my understanding of directives is lacking.
The following is a function that is launched when the system detects a change in the URL. The new URL parameters are passed and are used to query the back-end for content.
function handleUrlParamsModalLaunch(data) {
/*Ensure modal is not displaying any data*/
vm.modalData = {};
vm.selectedTab = null;
/*Show modal loading gif*/
vm.isModalLoading = true;
$("#contentPartModal").modal();
/*Call the content service to return the clicked content article*/
contentpartservice.getContentItem(data.id, data.type).then(function (contentItem) {
if (contentItem) {
vm.isModalLoading = false;
vm.modalData = contentItem;
return;
} else {
closeModal("#contentPartModal").then(function () {
vm.isModalLoading = false;
logger.error('An error occurred while fetching content');
});
return;
}
}, function (error) {
closeModal("#contentPartModal").then(function () {
vm.isModalLoading = false;
logger.error('An error occurred while fetching content');
});
return;
});
}
The following function is run when a link is clicked. It adds the parameters needed to retrieve content from the back-end to the URL.
function setUrl(contentId, contentType) {
var urlParams = $location.search();
if (urlParams.q) {
$location.search({ q: urlParams.q, type: contentType, id: contentId });
} else {
$location.search({ type: contentType, id: contentId });
}
return;
}
The following is where the solution starts to look like a hack job. I need to remove the parameters from the URL when the modal closes, but I couldn't find a way to catch the Bootstrap modal close event from the scope of my Angular controller (where the above functions are being called). Instead, I wrote the following JavaScript code in script tags that does it without Angular's $location dependency.
<script>
/*
* Detect the closing of a modal window and modify the URL to no longer display linking information.
* Not handled in Angular because Angular lacks a suitable way to detect a bootstrap modal close.
*/
$('#contentPartModal').on('hidden.bs.modal', function () {
var pageUrl = $.url();
var pageParams = pageUrl.param();
if (pageParams.q) {
if (history.pushState) {
var newurl = window.location.protocol + "//" + window.location.host + window.location.pathname + '?q=' + pageParams.q;
window.history.pushState({ path: newurl }, '', newurl);
}
} else {
if (history.pushState) {
var newurl = window.location.protocol + "//" + window.location.host + window.location.pathname;
window.history.pushState({ path: newurl }, '', newurl);
}
}
});
</script>
Here is the resulting bug. The first time you click on a link, all of these functions run fine. The modal is opened with the correct data being displayed. When you close the modal, the URL parameters are removed from view. When you go to click on another link, the setUrl function is called, but the URL doesn't actually change. This results in the modal pop-up not opening. A second click on any link, and everything works as expected. The resulting bug is that each link needs to be clicked twice after the first time the modal has been opened.
Any hints to the cause of this bug would be much appreciated. I'd also accept an idea for a better implementation that would help me circumvent the issue altogether.
Thanks,
Matt
I have researched on why we get "Uncaught Syntax error: ) missing after argument list" error and i went through my code throughly, after reading a few answers on stack overflow. i've been on this for more than a day, yet the error still persist. please help.
my code
function openinnewtab(filename) {
var url = #Url.Action("DisplayDocuments", new { Filename = filename });
var redirectWindow = window.open(url, '_blank');
redirectWindow.location.reload(true);
return false;
}
and i am calling it here in my View, basically im tring tto implement onclick functionality on the image so as when the image is clicked, it opens a new window and displays the image.
<a onclick="openinnewtab(#filename);"><img title="#filename" src="~/Content/images/image.png"></a>
Basically you are missing some single quotes and also you are mixing JS variables in Razor Code, your code should be -
<a onclick="openinnewtab('#filename');"><img title="#filename" src="~/Content/images/image.png"></a>
And
function openinnewtab(filename) {
var url = '#Url.Action("DisplayDocuments", new { Filename = "tempFileName" })';
// Replace templFileName with actual value which is being passed to this function
url = url.replace("tempFileName", filename);
var redirectWindow = window.open(url, '_blank');
redirectWindow.location.reload(true);
return false;
}
I want to open a new tab using JavaScript or jQuery.
I tried this code:
window.open("myurl", '_blank');
But browser gives me alert for pop-up blocked.
I have to open new tab without pop-up blocked alert.
Each and every client can't allow pop-up.
Can anyone help me please?
try this,
$('#myButton').click(function () {
var redirectWindow = window.open('http://google.com', '_blank');
redirectWindow.location;
});
working js fiddle for this http://jsfiddle.net/safeeronline/70kdacL4/2/
working js fiddle for ajax window open http://jsfiddle.net/safeeronline/70kdacL4/1/
The only way to overcome this is to perform a synchronous Ajax request which will block your browser while it runs, but will preserve the event context.
This will help--->
Open new tab without popup blocker after ajax call on user click
Here is the sample code for you --->
<table>
<tr>
<td>Works without warning in all browsers:</td>
<td><input type="button" onclick="performSyncronousRequest()" value="Syncronous request"/><td>
</tr>
</tr>
</table>
Scipt--->
/**
* This method will give open the popup without a warning.
*/
function performSyncronousRequest() {
$.ajax({
url: '/echo/html',
data: {},
success: function(){
window.open('http://www.w3schools.com');
},
async: false
});
}
Heres the working fiddle http://jsfiddle.net/23JNw/80/
var win = window.open('http://stackoverflow.com/', '_blank');
if(win){
//Browser has allowed it to be opened
win.focus();
}else{
//Broswer has blocked it
alert('Please allow popups for this site');
}
You can try like this
$(document).on('click', '.preview', function(event) {
event.preventDefault();
if (confirm("Are You Sure?"))
{
var daoGroup = $("#daoGroup").val();
if (daoGroup === undefined && daoGroup === null) {
alert("Select DAO Groups");
return false;
}
else
{
/* Act on the event */
var data = $(".frmContent").serialize();
var url = '<?php echo base_url() ?>reportViewPrint/sailorNominalRoll/htmlPreview';
window.open(url+'?'+ data, '_blank');
}
}
});
I am working on an asp.net web page which has a hyperlink. when ever that hyperlink is clicked, a new browser window is opened using javascript window.open. I want that If user clicks this link multiple times, then only one window is opened and not multiple windows. I just want that window to be highlighted when user clicks that hyperlink multiple times. Do I need to use window.open to detect if the url is opened in any other tab of the browser ? Is there any jQuery plugin built in for this so that I can use it for browser compatibility.
Here is the hyperlink url:
<a onclick="addClick()" href="javascript:void(0)">
New</a>
and here is the code I am using:
function addClick() {
var ID = jQuery("#ID").val();
var PSSWD = jQuery("#PSSWD").val();
var ACCID = jQuery("#ACCID").val();
var PASSWDINT = jQuery("#PASSWDINT").val();
window.open("LoginAPI?ID=" + encodeURIComponent(ID) + "&PSSWD=" + encodeURIComponent(PSSWD) + "&ACCID=" + encodeURIComponent(ACCID) + "&PASSWDINT=" + encodeURIComponent(PASSWDINT) + "", "LoginAPI");
}
Please suggest.
Try
window.open("<url>", "<window name>");
This should always open in the same window. See reference.
HTML:
open window
var wins = {};
function openwindow(){
var url = this.href;
if(typeof wins[url] === 'undefined' || wins[url].closed)
wins[url] = window.open(url);
}
<script>
var windowObjectReference = null; // global variable
function openFFPromotionPopup() {
if(windowObjectReference == null || windowObjectReference.closed)
/* if the pointer to the window object in memory does not exist
or if such pointer exists but the window was closed */
{
windowObjectReference = window.open("http://www.spreadfirefox.com/",
"PromoteFirefoxWindowName", "resizable,scrollbars,status");
/* then create it. The new window will be created and
will be brought on top of any other window. */
}
else
{
windowObjectReference.focus();
/* else the window reference must exist and the window
is not closed; therefore, we can bring it back on top of any other
window with the focus() method. There would be no need to re-create
the window or to reload the referenced resource. */
};
}
</script>
click here
Check the reference https://developer.mozilla.org/en-US/docs/Web/API/window.open
To open only one instance of a popup window in an HTML page, use the windowName parameter of the window.open method.
For example
window.open('http://www.abc.com')
will open a new window each time the user clicks the link containing the window.open code.
In constrast,
window.open('http://www.abc.com','abc')
will open only one instance of the window, no matter how many times users click the link.
you can also use focus function as used below
<script language="javascript" type="text/javascript">
<!--
function popitup(url) {
newwindow=window.open(url,'name','height=200,width=150');
if (window.focus) {newwindow.focus()}
if (!newwindow.closed) {newwindow.focus()}
return false;
}
// -->
</script>
Edit 1
<a onclick="return addClick()" href="javascript:void(0)">New</a>
and here is the code I am using:
function addClick() {
var ID = jQuery("#ID").val();
var PSSWD = jQuery("#PSSWD").val();
var ACCID = jQuery("#ACCID").val();
var PASSWDINT = jQuery("#PASSWDINT").val();
window.open("LoginAPI?ID=" + encodeURIComponent(ID) + "&PSSWD=" + encodeURIComponent(PSSWD) + "&ACCID=" + encodeURIComponent(ACCID) + "&PASSWDINT=" + encodeURIComponent(PASSWDINT) + "", "LoginAPI");
return false;
}