Showing posts with label HTML. Show all posts
Showing posts with label HTML. Show all posts

Friday, September 4, 2026

JavaScript: How to Open a New Browser Window Using window.open()

Some times its an useful job to create a window with a new requirement. But , when we are in development room/area , we always remember any search engine to fine the solutions for any type of problem. Even I , also  following some of the search engine. 

But, it is a fact that I am always try to avoid this type of habits. Be , traceable  for finding any solution. This article is one of the mail memory & also my teaching my myself , that don't guess any coding is normal and easy , so don't remember.


So, below the topic:- code line put inside script tag
window.open('','','left=0,top=0,width=1000,height=780,toolbar=0,scrollbars=0,status=0');

You can also use:

window.open();
 
All parameters are not mandatory  to put with values. If you don't need any scrollbar, toolbar, status bar then you can put 0 as the value here, either put 1. 


How to Set Print Area in JSP/HTML Page for Printing

This is one the interesting topic related to print the specific data from a jsp/html page. Directly to printer, without other browser data. I have done a quite obvious way in javascript for set the print area for printing. I faced this problem in my development team with me, for set a specific area for print.

Caution:-  Use of JavaScript may not be  always acceptable, due to client. If user/client disable the script setting in browser these may not work properly. But, every browsers are support script by default.  

As we know, window.print() is used for print in JavaScript. There are so many ways to do a printing task in jsp/html page. Out of all I have mentioned 2 process below with advantage & disadvantage.  Put the below function in script area of your page:-

Procedure-1
function setDivPrint(val){
var printdata=document.getElementById('printarea').innerHTML;
var srcOriginalContent=document.body.innerHTML;
document.body.innerHTML=printdata;
window.print();
document.body.innerHTML=
srcOriginalContent;
}
Here ‘printarea ‘ is the id of printable div. It has some limitation, with getting the cancel button click event and some more. I am not mentioning all here. But in procedure-2 is so eminent than procedure-1.

Procedure-2

function setDivPrint(val){
var printdata=document.getElementById('printarea').innerHTML;
var printwindow=window.open('','','left=0,top=0,width=1000,height=780,toolbar=0,scrollbars=0,status=0');
printwindow.document.write(printdata);
printwindow.document.close();
printwindow.focus();
printwindow.print();
printwindow.close();
}

Here ‘printarea ‘ is the id of printable div. .This method is recommended to use. In this method no need to get the cancel or print button event. Suppose the user click on cancel, then the main original page should come, but it may not coming with the Procedure-1 . So, No need to face this problem in Procedure-2.


Monday, July 1, 2019

How to Get Latitude and Longitude Using JavaScript

In this article we will see how to get the latitude and longitude. As we know java script is a high-level programming language,  it gives many libraries to implement in our program to achieve real time necessities.  Here we have used Navigator and Geolocation in our program.

We have used the Navigator.geolocation , its a read-only property returns a Geolocation object that gives Web content access to the location of the device.

sample-geolocation.html

<!DOCTYPE html>
<html>
<body>
<title>Get Current Location Sample</title>
<h4>Click the below button to get your current location coordinates.</h4>
<button onclick="getLocation()">Get My Current Location</button>
<div id="displayId"></div>
<script>
var display = document.getElementById("displayId");
function getLocation() {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(displayPosition);
  } else {
    display.innerHTML = "Geolocation is not supported by this browser.";
  }
}
function displayPosition(position) {
  display.innerHTML = "<br>Latitude: " + position.coords.latitude +
  "<br>Longitude: " + position.coords.longitude;
}
</script>
</body>
</html>

Now, we can see the output on the browser as below.