Home Javascript
Javascript move cursor to the end Print E-mail
Friday, 14 January 2011 13:39
Move the cursor with javascript to the end of the text in a textarea (or textfield):
 
<script>
function place_cursor()
{
  document.f.textarea.focus();
  document.f.textarea.value = document.f.textarea.value;  
}
</script>
<body onload="place_cursor();">
<form name="f" id="f">
<textarea rows="3" cols="40" name="textarea" id="textarea">testing</textarea>
</form>
</body>
 
 
Javascript ScrollTo function Print E-mail
Sunday, 07 November 2010 08:12
The next script scrolls a page to the top or the end of a page or top or end of an element element:
 
function _scroll(y, e, l)
{
  if (e != "")
  {
    var element = document.getElementById(e);
    if (l == "bottom")
    {
      y = element.offsetTop + element.offsetHeight;
    }
    else
    {
      y = element.offsetTop;
    }    
  }
  window.scrollTo(0, y);
}
 
To the top of a page: _scroll(0, "", "");

To the end of a page: _scroll(10000000, "", "");

To the top of an element "elem": _scroll(0, "elem", "top");

To the end of an element "elem": _scroll(0, "elem", "bottom");
 
Javascript Drop Images (FF only) Print E-mail
Thursday, 21 October 2010 14:19

This is a script to transfer files to a folder on the server using drag 'n drop (test it yourself). It only works in Firefox. In IE it is not possible at all and Google Chrome has created something funny:  http://www.webmonkey.com/2010/04/google-turns-to-html5-for-gmails-new-drag-and-drop-attachments/

Here are the scripts for FireFox and PHP5:

The javascript part:

 
<script type="text/javascript" language="javascript1.2">
var upload = {
        setup : function() {
            var container = document.getElementById('container');
            var drop = document.getElementById('drop');            
            container.addEventListener("dragenter", function(event) {
                    event.stopPropagation();
                    event.preventDefault();
                }, 
                false
            );
            container.addEventListener("dragover", function(event) {                    
                    event.stopPropagation(); 
                    event.preventDefault();
                }, 
                false
            );
            container.addEventListener("drop", upload.uploadFiles, false);
        },        
        uploadFiles : function(event) {            
            var files = event.dataTransfer.files;
            document.getElementById("alert").innerHTML = "";
            document.getElementById("drop").innerHTML = "";
            event.stopPropagation();
            event.preventDefault();
            for (var x = 0; x < files.length; x++) {
                var file = files.item(x);                
                var xhr = new XMLHttpRequest();                
                xhr.onreadystatechange  = function()
                { 
                     if(xhr.readyState  == 4)
                     {
                          if(xhr.status  == 200)
                          {
                              if (xhr.responseText != "")
                              {
 document.getElementById("alert").innerHTML += " - Received:"  + xhr.responseText;
                              }
                          }
                          else
                          {
 document.getElementById("alert").innerHTML += " - Error code " + xhr.status + " - ";
                          }
                     }
                }; 
                fileUpload = xhr.upload,
                fileUpload.onload = function() 
                {
                    document.getElementById("alert").innerHTML += "Verzonden!";
                }
                document.getElementById("drop").innerHTML = "fileName: " +
file.fileName + "fileSize: " + file.fileSize;
                xhr.open("POST", "upload.php", true);                
                // do the AJAX request
                xhr.setRequestHeader("Cache-Control", "no-cache");
                xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
                xhr.setRequestHeader("X-File-Name", file.fileName);
                xhr.setRequestHeader("X-File-Size", file.fileSize);
                var destination = document.getElementById("destination").value;
                xhr.setRequestHeader("X-File-Destination", destination + "/");
                xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded;");
                //xhr.sendAsBinary(file.getAsBinary());
                //xhr.setRequestHeader("Content-Type", "multipart/form-data");
                xhr.send(file); 
            }
        },        
    };
    // activate upload.setup on load
    window.addEventListener("load", upload.setup, false);
</script>
 
<style>
#container
{
 min-width: 940px;
 min-height: 280px;
 border: 3px solid #F00;
 background-image:url(image/drophere.png);
 background-repeat: no-repeat;
 background-position: center bottom;
 background-color: #FFC;
}
</style>
</head>
<body>
<div id="container">
 <div id="drop"></div>
</div>
<input type="text" name="destination" id="destination" value="data" />
<div id="alert">--</div>
</body>
 
The PHP Class (does not work in PHP 4), save this script as Streamer.php, is used by upload.php (see further down):
<p>class File_Streamer
{
    private $_fileName;
    private $_contentLength;
    private $_destination;    
    public function __construct()
    {
        if (!isset($_SERVER['HTTP_X_FILE_NAME']) 
            && !isset($_SERVER['CONTENT_LENGTH'])) {
            throw new Exception("No headers found!");
        }
 
        $this->_fileName = $_SERVER['HTTP_X_FILE_NAME'];
        $this->_contentLength = $_SERVER['CONTENT_LENGTH'];
        $this->_destination = $_SERVER['HTTP_X_FILE_DESTINATION'];
    }    
    public function isValid()
    {
        if (($this->_contentLength > 0)) {
            return true;
        }
        return false;
    }    
    public function setDestination($destination)
    {
        $this->_destination = $destination;
    }    
    public function receive()
    {
        if (!$this->isValid()) {
            throw new Exception('No file uploaded!');
        }        
        file_put_contents(
$this->_destination . $this->_fileName, file_get_contents("php://input"));
        echo " Het bestand is opgeslagen";
        return true;
    }
}
</p>

The upload PHP script, save this script as upload.php:

require_once('Streamer.php');
$ft = new File_Streamer();
$ft->receive();
 
And of course the background image (displayed smaller than the original image):

drophere
 
Javascript window.open() Print E-mail
Thursday, 14 October 2010 15:01

Open a new small window using javascript:

var new_window = null;
function open_window()
{
 new_window = window.open('http://www.mobielstartpagina.nl/iphone', 'test',
 'fullscreen=no, height=600, left=200, location=no, menubar=no, resizable=no,
 scrollbars=no, status=no, titlebar=no, toolbar=no, top=200, width=350');
}
 

Returns a reference to the new window object: new_window


Close the window:

 

function close_window()
{
 if (new_window != null)
 {
  new_window.close();
 }
}
 
 
Javascript HIDE CODE Print E-mail
Thursday, 19 August 2010 09:50

Placing the javascript code between HTML comment tags (<!-- and ->) will prevent the code from displaying in older browsers without javascript.

<script language="javascript">

<!--// Start HTML Comment
function test()
{
        alert("the javascript code is hidden for older browsers without javascript support");
}
--> // End HTML Comment

</script>

 
Javascript FUNCTION for beginners Print E-mail
Thursday, 19 August 2010 09:50

It's easy to make your own function in Javascript. The following function called testfunction1 has the parameter "hello". The function prints the parameter to the screen with the JavaScript function alert ().

testfunction1("hallo");
function testfunction1(parameter1)
{
    alert(parameter1);
}
 
 
Javascript FUNCTION advanced Print E-mail
Thursday, 19 August 2010 09:50
You can connect a function definition to a variable. In this way it is possible to dynamicaly update functions in your script. The function call stays the same. 
var testfunction2 = function(parameter1)
{
    alert(parameter1);
};
testfunction2("hallo");
testfunction2 = function(parameter2)
{
    alert("same function call, but different alert: " + parameter2);
};
testfunction2("hallo");
 
 
Javascript VARIABLE SCOPE Print E-mail
Thursday, 19 August 2010 09:50
Variables defined in a function are not available outside the function.This is called the scope of a variable.
function test_function3()
{
    var test_variable = "this doesnt work";
}
alert(test_variable);
 
 
Javascript SIMPLE OBJECT Print E-mail
Thursday, 19 August 2010 09:50
In Javascript an object can be created in the following (simple) way. You cannot create an instance of such simple objects (using 'new').
var Obj = {variabele1:1, variabele2:2};
alert(Obj.variabele1 + Obj.variabele2);
var my_Obj = new Obj; // gives an error
var my_Obj = Obj // this works, but my_Obj is a pointer to Obj and no independent object
// the same for arrays
var Arr = new Array(1,2);
var my_Arr = new Arr // gives an error
 

 

 
Javascript COPY ARRAY Print E-mail
Thursday, 19 August 2010 09:50
Prototype extension of the Array object in Javascript. An instance of the Array is made with the function Array().duplicate()
function duplicate_array()
{
    return new Array().concat(this);
}
Array.prototype.duplicate = duplicate_array;
var c = new Array(1,2);
var d = c.duplicate();
d[0] = 3;
d[1] = 4;
alert(c + "  " + d);
 

 

 
Javascript CLASS AND PROTOTYPE Print E-mail
Thursday, 19 August 2010 09:50
A so-called Class can also be made with the JavaScript 'function'.
function test_class(parameter1, parameter2, parameter3) // Class constructor
{
    this.parameter1 = parameter1; // Put the parameters to the class
    this.parameter2 = parameter2;
    this.parameter3 = parameter3;
}
// make an instance of the class for your own use
var mytest_class = new test_class("a", "b", "c");
//make the method print of the test_class
function test_class_print()
{
    return (this.parameter1 + this.parameter2 + this.parameter3);
}
// link function test_class_print to test_class
test_class.prototype.print = test_class_print;
// the instance now owns the functie print
alert (mytest_class.print());
 
 


 

Mobielstartpagina.nl

Visit our mobile portal page:

http://www.mobielstartpagina.nl

or

http://www.mobielstartpagina.nl/iPhone

on your smartphone!

Tech news and updates by PCWorld. Focused on computers and consumer electronics, topics include computer hardware, software, wireless, digital entertainment, security and anti-virus and gadgets.