Internet html5
Internet html5
// The following code and adds an element node with a containing text node to
// each <book> element in the xmlDoc (note! loop is optional and only used to change the XML data)
var parentElement = xmlDoc.getElementsByTagName("book");
for (var i=0; i < parentElement.length; i++) {
var newelement = xmlDoc.createElement("edition");
var newtext = xmlDoc.createTextNode("first");
newelement.appendChild(newtext);
parentElement[i].appendChild(newelement);
}
}
// The following code put the modified XML data in a table for display
var titles = xmlDoc.getElementsByTagName("title");
var editions = xmlDoc.getElementsByTagName("edition");
var tableStr = "<table border='1' padding='3'>";
tableStr += "<tr><th>Title</th><th>Edition</th></tr>";
for (var i = 0; i < titles.length; i++) {
tableStr += "<tr><td>" + titles[i].childNodes[0].nodeValue + "</td>";
tableStr += "<td>" + editions[i].childNodes[0].nodeValue + "</td></tr>";
}
tableStr += "</table>";
document.getElementById("table").innerHTML = tableStr;
</script> add-xml-element.html
DOM Table Object with JSON 1
• The following example shows how to create a PHP JSON response
from an AJAX search request
<?php
// PDO configuration
//$dbtype = "mysql"; // XAMPP supports mysql and sqlite
searchjson.php
$dbtype = "sqlite";
$dbhost = "localhost";
$dbname = "webstore";
$dbuser = "hjo";
$dbpass = "abc123xyz";
$charset = "UTF8";
//$dsn = "$dbtype:host=$dbhost;dbname=$dbname;charset=$charset";
$dsn = "$dbtype:media.db3";
$DBH = null;
class ArtCds {
public $ArtistName="";
public $Title;
public $Date;
function __construct($Artistame="", $Title="", $Date=""){
if(!empty($Artistame))
$this->Artistame($Artistame);
if(!empty($Title))
$this->Title($Title);
if(!empty($Date))
$this->Date($Date);
}
}
//get the query parameter from URL
$query = $_GET["query"];
DOM Table Object with JSON 2
• PHP JSON AJAX response cont.
try{
if($DBH == null)
$DBH = new PDO($dsn);
searchjson.php
}
catch (PDOException $e){
echo '<b>PDOException: </b>', $e->getMessage();
die();
}
$sql = "SELECT artistname, title, date FROM artists, cds WHERE artists.artistid";
$sql .= " = cds.artistid and artists.artistname LIKE :query";
$STH = $DBH->prepare($sql);
$STH->execute(array(':query' => '%'.$query.'%'));
$STH->setFetchMode(PDO::FETCH_CLASS, 'ArtCds');
$notvisited = TRUE;
$artcdarr = array();
if ($notvisited)
$response = "No artist found with name: " . $query;
else
$response = json_encode($artcdarr); // serialize the objects
if(move_uploaded_file($tmp_name, "$uploads_dir\\$name"))
echo "The file: ". $name . " has been uploaded <br/>";
else
echo "There was an error uploading the file(s), please try again!";
}
}
?>
uploads.php
http://www.thebuzzmedia.com/html5-drag-and-drop-and-file-api-tutorial/
fileapi.html
HTML5 DnD and File API
• (DnD API) Starts with the drop event when the user releases the
mouse and the mouse-up event occurs.
• (DnD API) Get the DataTransfer object from the drop event
• (File API) Call DataTransfer.files to get a FileList, representing the
list of files that were dropped.
• (File API) Iterate over all the individual File instances and use a
FileReader object to read their content.
• (File API) Using the FileReader.readAsDataURL(file) call, every
time a file is completely read, a new “data URL” (RFC 2397)
formatted object is created and an event wrapping it is fired to the
onload handler on the FileReader object.
– The “data URL” object is Base64-encoded binary data, with a spec-defined
header sequence of chars. Browsers understand them.
• (File API) Now that we have the “data URL” object from the event,
we can do cool things with it like replace the src attribute of an
<img> tag to immediately show the value on-screen OR upload the
Base64-encoded portion of the data to a server to process as
binary data.
HTML5 File API with AJAX
• Currently only works with Firefox, Chrome and Safari?
• Asynchronous file uploads is a popular feature in modern
AJAX web-applications
– However standard XHR (XmlHttpRequest) does not have capabilities
to process or send files selected with "file dialog" (input type="file")
– Web developers have to use non-standard ways to solve it
• With the level 2 specification of XHR, the HTML5 FileReader,
the File APIs and the DataTransfer object (Drag and Drop)
this is solved
– In XHR2 the send method accepts a file object and also supports
progress event
• What this means is that we can drag files from our desktop
and drop them onto our web applications where we can use
JavaScript to read in their data or pass them off to be
uploaded
• http://www.profilepicture.co.uk/ajax-file-upload-
xmlhttprequest-level-2/ fileapi-upload.html
PHP receiving the XHR2 file
<?php
# http://www.profilepicture.co.uk/ajax-file-upload-xmlhttprequest-level-2/
$fileName;
$contentLength;
$path = 'uploads/';
fileapi-upload.html
try{
if (array_key_exists('HTTP_X_FILE_NAME', $_SERVER) &&
{
array_key_exists('CONTENT_LENGTH', $_SERVER))
upload.php
$fileName = $_SERVER['HTTP_X_FILE_NAME'];
$contentLength = $_SERVER['CONTENT_LENGTH'];
}
else
throw new Exception("Error retrieving headers");
if ($contentLength < 1)
throw new Exception('No file uploaded!');
/* Once you have a Worker object, you can send data to it with postMessage().
The value you pass to postMessage() will be cloned and the resulting copy
will be delivered to the worker via a message event: */
worker.postMessage("file.txt");
/* You can receive messages from a worker by listening for message events on
the Worker object when the worker use its postMessage function: */
worker.onmessage = function(e) {
var message = e.data; // Get message from event
console.log("Worker message contents: " + message); // Do something with it
}
}
worker = new Worker("worker.js");
webworkers.html
document.getElementById("start-worker").onclick = function () {
createWorker();
worker.postMessage(document.getElementById("worker-start-value").value);
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
</head>
<body>
<h3>jQuery Demo</h3>
<p> <a href="http://jquery.com/">Go to jQuery.com</a> </p>
<script>
$(document).ready(function(){ // same as jQuery(function(){...
$("a").click(function(event){
alert("As you can see, the link no longer take you to jquery.com");
event.preventDefault();
});
}); jquerydemo.html
</script>
</body> 6-10b.html
jQuery mobile
• Mobile framework using jQuery
• Cross-browser, cross-platform
– Most mobile web browsers are supported
• Create web apps that feel as close to native as
possible
• Markup driven, minimal code required to get up and
running
• Focused on progressive enhancement, graceful
degradation
• Tutorial in Docs section at: http://jquerymobile.com
or download the jquery.mobile-1.0.1.zip
(JavaScript, CSS, and images) file
cars-mobile.html
AngularJS
• AngularJS (or Angular) is a complete JavaScript-based open-
source front-end web application framework
– It is mainly maintained by Google and by a community of
individuals and corporations to address many of the challenges
encountered in developing single-page applications (SPAs)
• It aims to simplify both the development and the testing of
SPAs for client-side MVC and MVVM architectures
– The AngularJS framework works by first reading the HTML page,
which has embedded into it additional custom tag attributes
– Angular interprets those attributes as directives to bind input or
output parts of the page to a model that is represented by
standard JavaScript variables
– The values of those JavaScript variables can be manually set
within the code, or retrieved from static or dynamic JSON
resources
• https://en.wikipedia.org/wiki/AngularJS
• http://www.w3schools.com/angular/
Other libraries, utilities etc.
• 50 jQuery Tools for Awesome Websites
– http://www.problogdesign.com/resources/50-jquery-tools-for-awesome-
websites/
• Download ready-made JS programs at sites as
– http://www.hotscripts.com/ or http://dynamicdrive.com/
• HTML5 enabling script that enables all new elements for old IE
– http://remysharp.com/2009/01/07/html5-enabling-script/
• Bootstrap is a free and open-source front-end web framework for designing
websites and web applications. It contains HTML- and CSS-based design
templates for typography, forms, buttons, navigation and other interface
components, as well as optional JavaScript extensions. Unlike many web
frameworks, it concerns itself with front-end development only
– http://getbootstrap.com/javascript/
• Modernizr is an open-source JavaScript library that helps you build the
next generation of HTML5 and CSS3-powered websites
– http://www.modernizr.com/
• Indexed Database API
– http://en.wikipedia.org/wiki/Indexed_Database_API
– http://hacks.mozilla.org/2010/06/comparing-indexeddb-and-webdatabase/
Content Management
Systems (CMS)
• The most popular CMS:s according to web
admins are Wordpress, Drupal and Joomla
• Which one to select depends on the purpouse
with your website
• Wordpress may be better for a simple website
• Joomla is not so customizable
• Drupal is more diffucult to install and manage but may
be better for a more complex website