How to get window.print() to run locallly? - javascript

Hi I have been writing a website for the last couple of weeks but cannot get the Javascript function window.print() to run locally in my browser. I do not have server space yet so I cannot check the online functionality. Here is my code:
<form>
<input type="button" value="Print this page" onclick="window.print();" />
</form>
Does anyone know if this function can run on a local machine? Or is there a problem with my code? Thanks

The syntax is fine. It should work.
You might also want to try onclick="javascript:window.print();" and see if that works.
Another option you can try then is to create a function inside a script tag and call it from your onclick event:
<input type="button" value="Print this page" onclick="PrintMe()" />
<script>
function PrintMe(){
window.print();
}
</script>

It might be a problem with the form tag..
try using button without form requirement
<button onclick="window.print();">Print this page</button>
Or if you are using the form
<form>
<input type="button" value="Print this page" onclick="window.print();return false;" />
</form>
if alert works and then enables this to work what happens if you try
onclick="console.log(window.print());return false;"

Capitilize the C in onClick. That might help. That's how I learned it, anyway.

Related

Javascript redirect function is not redirecting

function myFunction() {
location.replace("https://www.w3schools.com")
}
in the script tag
<button onclick='myFunction()'>Submit</button>
in the html
Any idea why this very basic command doesn't work?
check this example:
https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_redirect_webpage
If your button is inside the form then need to specify as type of button
<form>
<button onclick="myFunction()" type="button">Replace document</button>
</form
I tested your code and it works fine..
Try clearing your cache inspect and right click on reload choose clear cache.
Did u link your script file ?
<script src="script.js"></script>

How can automatically click on an hidden submint input tag when the user click on another shown input tag?

I am absolutely new in JavaScript and I have the following problem.
For some reason in a web page I have 2 input tag having type=submit, these:
<input type="submit" onclick="return myTestFunction();" value="Submit" id="submitEventButton">
<input id="submitButton" type="submit" style="display:none" onclick="return validateForm();" value="Submit" name="action:projectCreationAction">
As you can see the second one is hidden by the use of style="display:none"
Clicking on the first one I can enter into the myTestFunction() JavaScript method (that actually simply perform an alert).
I need to do the following thing: when the user click on the first button (the one having id="submitEventButton") automatically the hidden second button have to be clicked.
How can I do it?
Thanks!
You could place the following inside your myTestFunction, after the alert:
$('#submitButton').trigger("click");
Below is snippet you can run to check this out:
function myTestFunction(){
alert('hi from submit');
$("#submitButton").trigger("click") ;
}
function validateForm(){
alert('hi from validate form');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="submit" onclick="return myTestFunction();" value="Submit" id="submitEventButton">
<input id="submitButton" type="submit" style="display:none" onclick="return validateForm();" value="Submit" name="action:projectCreationAction">
Instead of simulating a click or even having a second hidden submit button at all, you can just call the function that the second button is hitting from the first function:
function myTestFunction() {
// code here with alert
validateForm();
}
function validateForm() {
// code here
}
functions calling other functions is better practice than buttons triggering other click events.
this way is also pure javascript (no jquery) since i dont think with the implementation of your input code you are currently using jquery.
Your question is not so clear about it, but it seems like you don't really need the second button at least for what you're describing.
What you can do to achieve what you're after is add a call to validateForm() in the body of myTestFunction.
If you do not want to change the function for some reason, you can also change the first button like so:
<input type="submit" onclick="myTestFunction();validateForm();" value="Submit" id="submitEventButton">
It ain't pretty, and note that i assume here that myTestFunction doesn't return something important otherwise you can't discard the return as i suggested here, but other than that it would probably do the trick.

Not able to submit form in chrome through javascript

Form submission is working good on both IE and Firefox, but recently i tested my application on chrome. In that i found chrome is not submitting form, there are no error messages in chrome console, can u help me. i had gone through server answers , still i didn't get proper solution. here is my js code
<script type="text/javascript">
function callModule(){
document.frmcheckUserDomain.submit();
}
</script>
<form name="frmcheckUserDomain" action="checkUserDomain.do" method="post">
// here is my form fields
<input type="button" name="subBtn" value="Submit" onClick="callModule();" />
</form>
Try changing your callModule method to this...
function callModule(){
document.getElementById("frmcheckUserDomain").submit();
}
Also, you will want to give your form tag an id attribute...
You could also access the form via index as well...
function callModule() {
document.forms[0].submit();
}
first of all why don't you use input submit button instead, like:
<input type="submit" name="subBtn" value="Submit" />
and if you have some stuff to do when the form gets submitted, you can use onsubmit event in your form.
And for your solution, you can pass it as an argument and then submit it:
<input type="button" name="subBtn" value="Submit" onClick="callModule(this);" />
your function:
function callModule(elm){
elm.parentNode.submit();
}
see, your code is working fine in my Google chrome. i think u have some issues with your browser or better u check the java Servlet configuration.

How do you assign the printThis plugin function to a button?

Sorry for the noob question...I've been searching for an example but I'm obviously doing something wrong (or am way off track in what I want to do!)
I am using this:
http://www.devx.com/webdev/Article/10483/0/page/2
to create a multipage form using visibility and div sections. Seems to work very well.
I now need to be able to print (printer) one page from another page. Possible?
I'm trying to use the printThis plugin by Jason Day.
I am calling the plugin like so:
$.getScript("printThis.js", function(){
});
(plugin is in the current directory...will address that later)
<input type="button" id="B1" value="Go Back" onClick="showLayer('page1')">
<input type="button" id="B2" value="Print App" onClick="$("#page1").printThis()">
First button works as expected...goes back to page1
Second button does nothing...is syntax right? Or am I just trying to do something that can't be done?
Your first button is correct. The second button is not however. Your double quotes " within your jQuery selector is causing the onClick attribute to close prematurely. Try one of the following methods:
Inline Option 1 - Escape Double Quotes
<input type="button" id="B2" value="Print App" onClick="$(\"#page1\").printThis()">
Inline Option 2 - Use Single Quotes
<input type="button" id="B2" value="Print App" onClick="$('#page1').printThis()">
Option 3 - Remove Obtrusive JavaScript (Strongly Recommended)
<!-- Place this code in the <head></head> section of your page -->
<script type="text/javascript">
$(document).ready(function () {
$('#B2').on('click', function () {
$('#page1').printThis();
});
});
</script>

Button onclick not redirecting to correct URL

<button id="promoCodeSubmit" onclick="window.location.href='http://www.test.com'+document.getElementById('promoCodeValue').value;">Apply</button>
Trying to use the code above to redirect a page. console.log is printing out the correct URL, but page gets redirected incorrectly. Any ideas why?
Demo jsFiddle
The following works fine for me, let me know if you need more.
HTML
<button id="promoCodeSubmit" onclick="ClickEvent()">Apply</button>
<input type="hidden" id="promoCodeValue" value="1"/>
JS
function ClickEvent(){
window.location.href='http://www.test.com'+document.getElementById('promoCodeValue').value;
}
Why don't simplify your code and make it readable.It will also solve your problem.
HTML
<button id="promoCodeSubmit" onclick="RedirectToLocation()">Apply</button>
<input id="promoCodeValue" type="text" value="testing123" />
Script:
function RedirectToLocation(){
window.location.href='http://www.test.com'+document.getElementById('promoCodeValue').value;
}
Js Fiddle Demo

Categories

Resources