Home

Search this site:
 

Horshack’s
very
own site

 

This is Bavaria, Germany!
You will find links to Perl,
bootdisks for system administration,
a lot of Linux,
Windows administration
and some personal belongings.
Javascript, little tricks

Javascript

Switch pictures off/on and change a linktext to reflect it

See this example

Include content from other webpages by Javascript

You want to include content into your webpage from another webserver controlled by another person. This could be advertisement, news,...
<h2>My Domain</h2>
<p>bla bla</p>
<p>Advertisement:</p>
<script src="http://wwwpublish.zdf/temp/test.js">
</script>
<p>my content</p>
The script-src-element is allowed to be on another webserver! It looks like this:
document.write("<h1>Advertisement</h1>Buy this one<br /><img src="http://..." />");

Activate all checkboxes with Javascript

car
bike
ein/aus...

Setting focus to an input-field

This is useful for forms where the user should simply type in something witout having to navigate to the first field:

<head>
<!-- prototype is a helper library from http://prototype.conio.net -->
<script src="prototype.js" type="text/javascript"></script>
<!-- set focus to a field with the name "searchcontent" in my form -->
<script type="text/javascript">
    function setfocus(a_field_id) {
        $(a_field_id).focus()
    }
</script>
</head>

<body onload="setfocus('customervalue');">

Customer: <input name="customer" id="customervalue" />

</body>

Forwarding to another webpage

This is useful for forms where the user should simply type in something witout having to navigate to the first field:

<head>
    <meta http-equiv="refresh" content="5; URL=http://google.de">
</head>

<body>
Forwarding to <a href="http://www.google.de">google.de</a>...
</body>

Get data from an xml-resultset

I get data from an XML-Service like this:

<?xml version='1.0'  encoding='iso-8859-1' standalone='yes'?>
<foxy>
  <err>0</err>
  <errstr></errstr>
  <last_updates>
    <jodel>
      <dayname>Friday</dayname>
      <time>17:07:09</time>
    </jodel>
    <jodel>
      <dayname>Thursday</dayname>
      <time>19:08:36</time>
    </jodel>
    <jodel>
      <dayname>Wednesday</dayname>
      <time>19:08:35</time>
    </jodel>
  </last_updates>
</foxy>

I need to get the data of all jodel/dayname and jodel/time. The javascripts looks like this:

function go() {
    // There is a real URL from my own subdomain
    // where this script is started from in real life:
    get_xml_data_from_url('z.xml');
}

// push debug-information into <div id="debug"></div>
function debug(msg) {
    $("debug").innerHTML += msg + "<br />";
}

// you need prototype.js for this
function get_xml_data_from_url(url) {
    new Ajax.Request(url, {
        method: 'get',
        onComplete: function(req) {
            xml_callback(req);
        }
    });
}
function xml_callback(request) {
    var xmlantwort = request.responseXML;

    // there are many <jodel>...</jodel> elements in the xml-response
    var jodels =
      xmlantwort.getElementsByTagName('jodel');
    for ( i=0; i < jodels.length; i++ ) {
        // every jodel-element contains a <dayname>friday</dayname>
        var dayname_node =
            jodels[i].getElementsByTagName('dayname')[0].firstChild
        // every jodel-element contains a <time>08:12:35</time>
        var time_node =
            jodels[i].getElementsByTagName('time')[0].firstChild

        debug('update at: '
            + dayname_node.nodeValue
            + ', ' + time_node.nodeValue);
    }
    debug( msg);
    debug('end');
}

The output is this:

update at: Friday, 17:07:09
update at: Thursday, 19:08:36
update at: Wednesday, 19:08:35
update at: Monday, 19:06:33
Impressum