this.testing = function(link) {
window.location = link;
window.addEventListener('load', function() {
alert("It's loaded!");
});
//OR
myFunctionCall()
}
This is the code I'm trying to implement but after the window is redirected to a link Im not getting the alert.
My aim is to call a function which is dependent on the window after getting redirected
I have listUser.jsp, it has a link that "calls" user.jsp. user.jsp have a form that saves in my DB and listUser.jsp shows it. The problem is: i want user.jsp to open in a modal, is that possible? i've tried iframe inside the modal, but when i Submit in user.jsp, inside the iframe, it "returns" to listUser.jsp and doesn't close.
with the iframe i've tried this (didn't work):
<script>
function yourFunctionName () {
var div = document.getElementById('dialog');
if(document.getElementById('noop').src === 'user.jsp'){
div.style.display = 'none';
} else {
div.style.display = 'block';
}
}
</script>
<script>
function myFunction() {
setInterval(yourFunctionName, 1000);
}
</script>
i'm learning, so i don't know much
You can use jquery for this purpose,
listUser.jsp
<div id=content></div>
Use jquery instead of yourFunctionName (), like this
document.ready(function(){
$('#button').on("click", function() {
$("#content").load(absolutePath/user.jsp);
});
});
hope this will helpful!
I have a tracking pixel and want to load it with jquery. After the successful loading I want to make a redirect to one url.
Now I have this code:
<script language="javascript" type="text/javascript">
document.write("<img src=\'PIXEL_TRACKING_URL\' width=\"1\" height=\"1\" \'> ");
function redirect(){
window.location.replace(URL);
}
setTimeout(redirect, 2500);
</script>
The problem is, when the tracking pixel tooks more then 2,5 seconds, then there is the redirect happen and I can not track the redirect.
Some help please, so I only redirect, when the image is loadet?
Try this approach:
var image = new Image();
image.src = 'PIXEL_TRACKING_URL';
if (image.addEventListener) {
image.addEventListener('load', function() {
window.location.replace(URL);
});
} else {
// it's IE!
image.attachEvent('onload', function() {
window.location.replace(URL);
});
}
I have made this with jQuery in this form:
$(document).ready(function() {
var image = $("IMAGE");
$("body").append(image);
image.load(function() {
setTimeout(redirect, 1000);
});
});
function redirect(){
window.location.replace("URL");
}
The page URL has to change when the loadTest() function is called on a ahref click. But the page is getting refreshed only when alert is included in the code. If I remove the alert the same page is getting refreshed and not the new URL which I need.
<script type="text/javascript">
function loadText(prod_id)
{
curUrl = document.location.href.split("?");
document.location = curUrl[0]+'?prodId='+prod_id;
alert("Test");
}
</script>
use window.location instead of document.location
function loadText(prod_id)
{
curUrl = window.location.split("?");
window.location = curUrl[0]+'?prodId='+prod_id;
}
window.location = curUrl[0]+'?prodId='+prod_id;
I have a page with a "Print" link that takes the user to a printer-friendly page. The client wants a print dialog box to appear automatically when the user arrives at the print-friendly page. How can I do this with javascript?
window.print();
unless you mean a custom looking popup.
You could do
<body onload="window.print()">
...
</body>
I like this, so that you can add whatever fields you want and print it that way.
function printPage() {
var w = window.open();
var headers = $("#headers").html();
var field= $("#field1").html();
var field2= $("#field2").html();
var html = "<!DOCTYPE HTML>";
html += '<html lang="en-us">';
html += '<head><style></style></head>';
html += "<body>";
//check to see if they are null so "undefined" doesnt print on the page. <br>s optional, just to give space
if(headers != null) html += headers + "<br/><br/>";
if(field != null) html += field + "<br/><br/>";
if(field2 != null) html += field2 + "<br/><br/>";
html += "</body>";
w.document.write(html);
w.window.print();
w.document.close();
};
If you just have a link without a click event handler:
Print Page
I do this to make sure they remember to print landscape, which is necessary for a lot of pages on a lot of printers.
Print Me...
or
<body onload="alert('Please be sure to set your printer to Landscape.');window.print();">
etc.
</body>
You can tie it to button or on load of the page.
window.print();
I know the answer has already been provided. But I just wanted to elaborate with regards to doing this in a Blazor app (razor)...
You will need to inject IJSRuntime, in order to perform JSInterop (running javascript functions from C#)
IN YOUR RAZOR PAGE:
#inject IJSRuntime JSRuntime
Once you have that injected, create a button with a click event that calls a C# method:
<MatFAB Icon="#MatIconNames.Print" OnClick="#(async () => await print())"></MatFAB>
(or something more simple if you don't use MatBlazor)
<button #onclick="#(async () => await print())">PRINT</button>
For the C# method:
public async Task print()
{
await JSRuntime.InvokeVoidAsync("printDocument");
}
NOW IN YOUR index.html:
<script>
function printDocument() {
window.print();
}
</script>
Something to note, the reason the onclick events are asynchronous is because IJSRuntime awaits it's calls such as InvokeVoidAsync
PS: If you wanted to message box in asp net core for instance:
await JSRuntime.InvokeAsync<string>("alert", "Hello user, this is the message box");
To have a confirm message box:
bool question = await JSRuntime.InvokeAsync<bool>("confirm", "Are you sure you want to do this?");
if(question == true)
{
//user clicked yes
}
else
{
//user clicked no
}
Hope this helps :)
<script>
const _print = () => {
window.print();
}
</script>
or
<body onload="window.print();"></body>
see the documentation here : https://developer.mozilla.org/en-US/docs/Web/API/Window/print
I know this is an old question, but after fighting with this similar issue, I figured out a way to open a print screen and NOT have to open a new tab, and not have to enable popups.
Hopefully, this helps someone else.
/*
Example:
Print
*/
//LISTEN FOR PRINT URL ITEMS TO BE CLICKED
$(document).off('click.PrintUrl').on('click.PrintUrl', '.print-url', function(e){
//PREVENT OTHER CLICK EVENTS FROM PROPAGATING
e.preventDefault();
//TRY TO ASK THE URL TO TRIGGER A PRINT DIALOGUE BOX
printUrl($(this).attr('href'));
});
//TRIGGER A PRINT DIALOGE BOX FROM A URL
function printUrl(url) {
//CREATE A HIDDEN IFRAME AND APPEND IT TO THE BODY THEN WAIT FOR IT TO LOAD
$('<iframe src="'+url+'"></iframe>').hide().appendTo('body').on('load', function(){
var oldTitle = $(document).attr('title'); //GET THE ORIGINAL DOCUMENT TITLE
var that = $(this); //STORE THIS IFRAME AS A VARIABLE
var title = $(that).contents().find('title').text(); //GET THE IFRAME TITLE
$(that).focus(); //CALL THE IFRAME INTO FOCUS (FOR OLDER BROWSERS)
//SET THE DOCUMENT TITLE FROM THE IFRAME (THIS NAMES THE DOWNLOADED FILE)
if(title && title.length) $(document).attr('title', title);
//TRIGGER THE IFRAME TO CALL THE PRINT
$(that)[0].contentWindow.print();
//LISTEN FOR THE PRINT DIALOGUE BOX TO CLOSE
$(window).off('focus.PrintUrl').on('focus.PrintUrl', function(e){
e.stopPropagation(); //PREVENT OTHER WINDOW FOCUS EVENTS FROM RUNNING
$(that).remove(); //GET RID OF THE IFRAME
if(title && title.length) $(document).attr('title', oldTitle); //RESET THE PAGE TITLE
$(window).off('focus.PrintUrl'); //STOP LISTENING FOR WINDOW FOCUS
});
});
};
if problem:
mywindow.print();
altenative using:
'<scr'+'ipt>print()</scr'+'ipt>'
Full:
$('.print-ticket').click(function(){
var body = $('body').html();
var ticket_area = '<aside class="widget tickets">' + $('.widget.tickets').html() + '</aside>';
$('body').html(ticket_area);
var print_html = '<html lang="tr">' + $('html').html() + '<scr'+'ipt>print()</scr'+'ipt>' + '</html>';
$('body').html(body);
var mywindow = window.open('', 'my div', 'height=600,width=800');
mywindow.document.write(print_html);
mywindow.document.close(); // necessary for IE >= 10'</html>'
mywindow.focus(); // necessary for IE >= 10
//mywindow.print();
mywindow.close();
return true;
});