Surfing With Code

Tricks for developers about Java, HTML5, CSS, JavaScript, XML, jSON, deployment, design …

Tip for debugging in JavaScript — May 7, 2018

Tip for debugging in JavaScript

Meanwhile you are coding and you want to stop the execution of the code at any specific point, you just add to your code the following statement:


debugger;

The debugger statement invokes any available debugging functionality, such as setting a breakpoint. If no debugging functionality is available, this statement has no effect.

Source: MDN Web Docs

iOS – Setting “Home Screen” icon name for mobile Safari — May 3, 2018

iOS – Setting “Home Screen” icon name for mobile Safari

By default, when «bookmarking» a website as an icon (by choosing to Add to Home Screen from within Safari’s «+» menu), the icon name defaults to the page’s <title>, truncated to 12 characters.

In much the same way that apple-touch-icon lets you specify your own iconified representation of the page, is there a way for the webpage to specify a default icon name other than its <title>?

In iOS 6 this is solved with a meta tag:

<meta name="apple-mobile-web-app-title" content="Short name">

For better cross-compatibility on all versions of iOS, you could use a combination of answers (inspired by https://stackoverflow.com/a/13838563/1048705):

Place this into your document for iOS 6+:

<meta name="apple-mobile-web-app-title" content="Short name">

and use this tag’s content for the title for other iOS versions that don’t support the tag directly:

if (navigator.userAgent.match(/(iPad|iPhone|iPod)/i)) {
    document.title = document.getElementsByName('apple-mobile-web-app-title')[0].content;
}

Note that the JavaScript will change the title for all iOS clients, including iOS 6+.

Source: StackOverflow

What is the difference between a URI, a URL and a URN? — octubre 14, 2017

What is the difference between a URI, a URL and a URN?

Common confusion right? Let’s see:

Source: StackOverflow

URI — Uniform Resource Identifier

URIs are a standard for identifying documents using a short string of numbers, letters, and symbols. They are defined by RFC 3986 – Uniform Resource Identifier (URI): Generic Syntax. URLs, URNs, and URCs are all types of URI.

URL — Uniform Resource Locator

Contains information about how to fetch a resource from its location. For example:

  • http://example.com/mypage.html
  • ftp://example.com/download.zip
  • mailto:user@example.com
  • file:///home/user/file.txt
  • tel:1-888-555-5555
  • http://example.com/resource?foo=bar#fragment
  • /other/link.html (A relative URL, only useful in the context of another URL)

URLs always start with a protocol (http) and usually contain information such as the network host name (example.com) and often a document path (/foo/mypage.html). URLs may have query parameters and fragment identifiers.

URN — Uniform Resource Name

Identifies a resource by a unique and persistent name, but doesn’t necessarily tell you how to locate it on the internet. It usually starts with the prefix urn: For example:

  • urn:isbn:0451450523 to identify a book by its ISBN number.
  • urn:uuid:6e8bc430-9c3a-11d9-9669-0800200c9a66 a globally unique identifier
  • urn:publishing:book – An XML namespace that identifies the document as a type of book.

URNs can identify ideas and concepts. They are not restricted to identifying documents. When a URN does represent a document, it can be translated into a URL by a «resolver». The document can then be downloaded from the URL.

URC — Uniform Resource Citation

Points to meta data about a document rather than to the document itself. An example of a URC is one that points to the HTML source code of a page like: view-source:http://example.com/

Data URI

Rather than locating it on the internet, or naming it, data can be placed directly into a URI. An example would be data:,Hello%20World.

URI/URL Venn Diagram

Generating random number in JavaScript in a specific range — octubre 13, 2017

Generating random number in JavaScript in a specific range

There are millions of times where while we are coding we need to generate a random number. The most of the times we need it as an Integer, to get some value (position) from an array.

Here you are how to generate a random number! For more info about how does the method works, visit the SO link! Enjoy!

Source: StackOverflow

@AuthorIonuț G. Stan



/**
 * Returns a random number between min (inclusive) and max (exclusive)
 */
function getRandomArbitrary(min, max) {
 return Math.random() * (max - min) + min;
}

/**
 * Returns a random integer between min (inclusive) and max (inclusive)
 * Using Math.round() will give you a non-uniform distribution!
 */
function getRandomInt(min, max) {
 return Math.floor(Math.random() * (max - min + 1)) + min;
}

web.xml deployment descriptor examples — agosto 31, 2015

web.xml deployment descriptor examples

The web.xml is a configuration file to describe how a web application should be deployed. Here’re 5 web.xml examples, just for self-reference.

1. Servlet 3.1 deployment descriptor

Java EE 7 XML schema, namespace is http://xmlns.jcp.org/xml/ns/javaee/


<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
</web-app>

2. Servlet 3.0 deployment descriptor

Java EE 6 XML schema, namespace is http://java.sun.com/xml/ns/javaee

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
 version="3.0">
</web-app>

3. Servlet 2.5 deployment descriptor

Java EE 5 XML schema, namespace is http://java.sun.com/xml/ns/javaee

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 version="2.5">
</web-app>

4. Servlet 2.4 deployment descriptor

J2EE 1.4 XML schema, namespace is http://java.sun.com/xml/ns/j2ee

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
 version="2.4">

 <display-name>Servlet 2.4 Web Application</display-name>
</web-app>

5. Servlet 2.3 deployment descriptor

J2EE 1.3 DTDs schema. This web.xml file is too old, highly recommend you to upgrade it.

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
 <display-name>Servlet 2.3 Web Application</display-name>
</web-app>

Source: Mkyong.com

MustRead – Books to become a Javascript Ninja! — junio 22, 2015

MustRead – Books to become a Javascript Ninja!

Here you have a list of «the very best» of Javascript, made by the JS expert Ustun Ozgur:

The first three are the classic ones, the other next three are free and you can download o read online. Finally, you have the big JS reference made by Mozilla.

Enjoy JavaScript ninja!

Compressing JavaScript source code — febrero 5, 2015

Compressing JavaScript source code

The JavaScript files are one of the many things that are downloaded while loading a website. So reduce size of it it will bring benefits to your website loading time.

JSCompress.com is an online javascript compressor that allows you to compress and minify your javascript files. Compressed javascript files are ideal for production environments since they typically reduce the size of the file by 30-90%. Most of the filesize reduction is achieved by removing comments and extra whitespace characters that are not needed by web browsers or visitors.

Plus, if you want to hide or mask your source code from others, this is the way to make it unreadable

There are a number of reasons why compressing your javascript files is a good idea:

  • Quicker download times for your users.
  • Reduced bandwidth consumption of your website.
  • Reduced number of HTTP requests on your server when combining many javascript files into one compressed file, thus reducing the server load and allowing more visitors to access your website.
  • Comments and whitespace are not needed for javascript execution; Removing them will reduce file size and speed up script execution times.

Source: JSCompress.com