‘ Javascript ’ category archive

Pictures partially displayed, tinyMCE problem, lightbox2 problem because of sendfile

July 25, 08 by the programmer

I had this problem a long time, I found a post I wrote from 2007-10-27.

The symptoms are

  1. Pictures displayed from the web server are not complete, only the top part of the picture is displayed, and the rest of the picture is black. This usually happens if the picture is very big (high resolution)
  2. Weird javascript problems. Problems occur only if the Javascript is served by the apache web server. The problem can gennerally occur with every static file.

This is the configuration I have:

Apache 2.0.54 on Windows XP Professional SP2
PHP 4.4.4
MySQL 4.1.12

I noticed the problem once when I was making a picture upload. I noticed that when I try to view the uploaded picture I could see only a part of the image, only the top part and never the full image.

This was pretty weird for me at the time, so I tried to see if the same is going to happen on the production server, because that was on my local server. Everything was ok on the production server so I did not bothered to fix the problem because it did not make any difference for me.


After a while I tried to install TinyMCE, a free HTML WYSIWYG editor, version 2.1.2. I run the examples from the desktop folder before uploading them to the local web server, and they were working great. After I saw that the examples were working I wanted to put them in my project and I copied all the files to the htdocs folder on the apache web server.

After I uploaded the files I tried to run the same example scripts but I noticed that they are not working, and that there was an error in firebug.

This is what the error was:

tinyMCE is not defined
tinyMCE.init({

example_advanced…. (line 8 )

missing : after case label
function TinyMCE_Engine(){var ua;this.majorVersion=”2″;this.minorVersion=”1.2″;t…

At that time I made a post to the tinyMCE support forum. This is the post. Nobody new why this error was appearing because nobody responded to my post.

So I tried to find a solution of the problem on the net, but none was available, at least I could not find it.
I also tried to upload the scritpts to my production server and supprisingly everything worked great there, so the scripts were ok, and I tried to be suspicious of my PHP, Apache confuguration. I thought that this might be some security issue or something similar.

I could not fix the problem so I decided not to use tinyMCE and I worked without a HTML WYSIWYG editor.


2 Days ago I a friend asked me to make him a web page with picture gallery I and I decided to use the lightbox2 javascript picture gallery (version v2.04 Released 3/09/08). It is very nice. It looks great. I downloaded the javascript files on my desktop.
I run the example files from my desktop and they were working great.
Again I copied the same files in the htdocs folder and after I run the examples I got an error in the firebug console.
This is what the error was:

syntax error
if (parent == do

prototype.js (line 4417)

uncaught exception: script.aculo.us requires the Prototype JavaScript framework >= 1.6.0

Object.extend is not a function
}, window.LightboxOptions || {});


lightbox.js (line 63)



I remembered that I had problems with tinyMCE, with the pictures display and now with lightbox2 and I somehow new that all those problems must be related.
I aslo noticed that the problem happens only i the files are static, like .js files of .jpg files, and if the files are big. Both the tinyMCE and lightbox have a lot of lines of javascript code.


At that moment I went to the tinyMCE Web site and downloaded the latest version (v 3_1_0_1_2). I wanted to see if the same problem will appear with the new version again. It did. I After I run the example files from the htdocs folder I got a javascript error in the firebug console.
This was the error:

missing ) in parenthetical
var tinymce={majorVersion:’3′,minorVersion:’1.0.1′,releaseDate:’2008-06-18′,_ini…

tiny_mce.js (line 1)

tinyMCE is not defined
tinyMCE.init({

skins.html (line 10)

Displaying pictures from http
Dispaying half images


I had a filling that the poblem must be in the php, apache configuration. Maybe something with the encodings, or the file size limitation or something similar. But it had to be with some confuration. So I decided that I have to solve this problem. I started to search the net for any similar cases and I found a post that is not very related to this problem but I decided to try.

I found this post: CSS and Javascript files not showing up when running Apache under Ubuntu on a flash drive? Read on. (Many thanks to the writer for writing the solution)

This guy had some similar problem, not displaying images, javascript files problem, so I thought I would give it a try.

As I found out later from the post apache by default uses a system command from the operating system called sendfile.

This command tries to deliver the static files to the browser, but because I am using Windows XP and it is “Windows operating system”, there must be some problem with it.

So I found out that Apache by default checks if the operating system supports sendfile syscall, and if it does then it uses it from the operating system, and if not it uses his own implementation of the functionality. Aslo I found out that there is a configuration property called EnableSendfile in the http.conf file, the Apache configuration file, which by default is on, which means use operating system function.

I set this configuration property to off, restarted Apache and BINGOOOOOOOOOOOOOO.

Everythig worked.

The pictures were showing up, both versions of the tinyMCE editor were working and the lightbox2 script was working. Finally after more that 9 months the problem is resolved :)

So if you have similar problems open httpd.conf, find “EnableSendfile” and set it to off like this:
EnableSendfile off


Dont forget to restart Apache after changing the configuration file

The symptoms as I mentioned before are

  1. Pictures displayed from the web server are not complete, only the top part of the picture is displayed, and the rest of the picture is black. This usually happens if the picture is very big (high resolution)
  2. Weird javascript problems. Problems occur only if the JavaScript is served by the apache web server.



More about EnableSendfile can be found here:

http://wiki.apache.org/httpd/SendfileNotImplemented

http://httpd.apache.org/docs/2.0/mod/core.html#enablesendfile


I hope this post will be useful to somebody with similar problems

Thank you



Dynamically position DIV with JavaScript?

July 12, 08 by the programmer

If you want to position a DIV element using javascript this is how you can do it:

Catch me if you can

<div id=”movable_div” style=”position:absolute; top:0px; left: 0px; width:100px; height:100px; background:#EAF3FA;” >Catch me if you can</div>

You can position the above DIV on any coordinates you want using the bellow JavaScrpt function:

Note that the style attribute must be there in order this to work.

function move_div(div_id, x, y){
var div = document.getElementById(div_id);
if(div){
div.style.left = x+”px”;
div.style.top = y+”px”;
}
}

The bellow code will move the above DIV to coordinates x:100. y:100 pixels

<script type=”text/javascript>

move_div(’movable_div’, 100, 100);

</script>

How to get mouse pointer coordinates with JavaScript?

July 12, 08 by the programmer
Sometimes we need to get the position of the mouse cursor if we want to make some advanced JavaScripts.
The following JavaScript does exactly that, it takes the coordinates of the mouse pointer.
Check out the JavaScript in action

X: 0
Y: 0


function get_mouse_pointer_coordinates(e)
{
var posx = 0;
var posy = 0;
if (!e) var e = window.event;
if (e.pageX || e.pageY) {
posx = e.pageX;
posy = e.pageY;
}
else if (e.clientX || e.clientY) {
posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
}
//You have the coordinates in the posx and posY variables
//You can do whatever you want with them after this point
}

How to make a confirmation box in JavaScript?

July 12, 08 by the programmer

Have you ever wanted to make a confirmation box in JavasScript?

It’s very simple. For example if we want to add a confirmation box before deleting something this is how we can do it:

var yes_no = confirm(’Are you sure you want to delete this ….’);
if(
yes_no){
//The user confirmed the deletion
//This is where the code for deleting will go
}else{
//The user declined the deletion
}


Be careful when getting array length in JavaScript

June 21, 08 by the programmer

JavaScript is one of the most used script languages on the WEB.

A lot of times when we make some scripts we need to use arrays in order to store some data.

I found out a very interesting thing about arrays in JavaScript a few days ago. The think is that the array is an object and this object has a method for calculating length of the array that should return the number of elements that are stored in the array.

So lets explain this with a very simple example:

var a = [”dog”, “cat”, “hen”];

a[100] = “fox”;

var array_length=a.length;

alert(array_length);

101

As you can see the length method returns 101 which means that the array has 101 elements although it has only 4 elements.
So be careful when using the array.length method if the indexes in the array are not in order.

Remember - the length of the array is one more than the highest index.
So how to get the length of an array?

Well we can do it with a simple loop throw all of its elements, using the “for - in” syntax.

var array_length = 0;

for (i in a)

{

array_length++;

}

alert(array_length);

4

So the last JavaScript code will give us the number of elements in an array.

Checking if cookies are enabled with JavaScript

May 25, 08 by the programmer

If you want to check if cookies are enabled or disabled on a browser you can do this with this very simple javascript.

It returns a boolean variable set to true if cookies are enabled or set to false if they are disabled.

What it does is very simple, it tries to set a cookie and checks if the cookie is accepted or not.

function check_cookies() {

var c=”test_cookie=valid”;

document.cookie=c;

if(document.cookie.indexOf(c)==-1){

cookies_are_disabled = true;

}else {

cookies_are_disabled = false;

}

return cookies_are_disabled;

}

Execute server side file without using AJAX

May 22, 08 by the programmer

There are situations when you have to execute a php, asp or any other file that is on the server side from a javascript.

You can do that using AJAX, but there is a much more simpler way of doing that.

It is very simple all you have to do is create a new Image object and set the source of the image object with the url that you want to execute. You don’t have to import AJAX JavaScript files or use anything else, just three simple lines of code.

Nice a :)

var unique_number = new Date().getTime();
var i=new Image();
i.src=”server_file_that_you_want_to_call.php?parameter_1=”+parametar_1_value+ “&un=”+unique_number;

The unique number is for insuring the it is not going to be cached and it is going to execute every time you call it.

So you can use this when you want to execute server side files without a need for a result.

Cool

Invalid Label Error with JSON

May 20, 08 by the programmer

If you try do convert a JSON encoded String into a JavaScript Object by using the following syntax you will probably get an “Invalid Label Error” message.

//Wrong syntax

var object = eval(json_text);

If you want to avoid this error you should wrap the JSON string in parenthesis like this.

var returned_object = eval(’(’ + JSON_text + ‘)’);

That’s it

Problem solved

Converting a JSON text into a JavaScript object

May 20, 08 by the programmer

JSON (JavaScript Object Notation) is a data-interchange format.

It is often used with client-side JavaScript applications that make use of HTTPRequest to perform server communication functions. Most of the time it is used in conjunction with AJAX.

Data can be encoded into JSON notation for use in a client-side javascript, or decoded from incoming Javascript requests.

JSON format is native to Javascript, and can be converted to JavascriptObject directly using the eval() function.

To decode a JSON text that is passed to a JavaScript into a JavaScript Object you need to use the eval() function by passing the JSON encoded text as a parametar.

The JSON string is wrapped in parenthesis because eval is interpreting the first item in the JSON string as a JavaScript Label.

var returned_object = eval(’(’ + JSON_text + ‘)’);

Breaking out of an iframe

May 17, 08 by the programmer

This script will open a page that starts loading inside the iframe in the parent window above the iframe.

Just copy this javascript code in the page header that should break out of the iframe, and that’s it.

Everytime a page that has the bellow code in the header loads in an iframe it will automatically pop out of the iframe and start loading in the parent window.

<script language=“JavaScript” type=“text/javascript”>
function breakout_of_frame() { 
   if (top.location != location) { 
      top.location.href = document.location.href ; 
   } 
} 
breakout_of_frame();
</script>