Sunday, August 26, 2012

jQuery toggle() not working?

I just found out that toggle() is not working with the JQuery version I've been using. I am using 1.72 from this link. After switching to version 1.80, I now can lay back a bit by enjoying the convenience of toggle() function which requires many lines of codes if using javascript to control the visibility of an element. Silly me. ;)
Read More »

Tuesday, August 21, 2012

Simple HTML/Javascript Codes to Display your Live Mouse Coordinates (Firefox Fix)

The firefox friendly version:


<html>
<head>

<script type="text/javascript">

var ff = (navigator.userAgent.indexOf("Firefox") != -1);

function showXY(e)
{
  if (ff) {
     var s = 'X=' + e.pageX + ' Y=' + e.pageY;
     document.getElementById('coordinates').textContent = s;
  }
  else {
     var s = 'X=' + window.event.clientX + ' Y=' + window.event.clientY;
     document.getElementById('coordinates').innerText = s;
  }
}

document.onmousemove = showXY;

</script>
</head>

<body>

<div id="coordinates"></div>

</body>
</html>


Here's the firefox friendly version. Instead of using onmousemove event capture in the body tag, it is initiated in javascript before the body. IF you initiate the event in the body tag, it won't work. A rare Firefox problem with a weird solution. But it works.
Read More »

Monday, August 20, 2012

Simple HTML/Javascript Codes to Display your Live Mouse Coordinates

Here they are:


<html>
<head>

<script type="text/javascript">

function showXY()
{
   var s = 'X=' + window.event.clientX + ' Y=' + window.event.clientY;
   document.getElementById('coordinates').innerText = s;
}

</script>
</head>

<body onmousemove=showXY()>

<div id="coordinates"></div>

</body>
</html>


The key component for this to work is highlighted in green. However this won't work in Firefox. The firefox friendly version can be found here. Sorry I wasn't aware of the Firefox problem earlier. Another way to fix this is to use JQuery.
Read More »

Weird Chrome Javascript Problem

I encounter this today that the Javascript var top cannot be used in Chrome. Weird. It works well in Safari and IE. In Chrome, the variable name is stored as [Object Window]. Solution: Just rename top variable to something else.
Read More »

PHP to Replace Last Character in a String

Seems like a trivial code but it's nice to put it here as I tend to forgot the syntax from time to time:

$line = preg_replace("/\n$/","",$line); // To replace last newline character

$line = preg_replace("/\r\n$/","",$line); // To replace last newline character in Windows environment

$line = preg_replace("/\s$/","",$line); // To replace last space in a string

Assuming your string is stored in $line.
Read More »

Sunday, August 5, 2012

Protect Your CSS and Javascript Codes




Update 20th of March 2013: Here's a updated version to include a simple minification before sending your Javascript / CSS codes to the browser especially Chrome:



http://webtrick101.blogspot.com/2013/03/protect-your-external-css-and.html


Actually there is no 100% way to protect your CSS and Javascript codes as if the person who intended to copy is using Google Chrome browser, your codes will be shown no matter how you protect them. But there is a good thing about protecting your CSS and Javascript codes. Google Chrome won't display the comments or 100% your original codes. Your codes will be made conformed to Chrome standard before being displayed. Without the 100% original codes, the intruder won't be able to copy your codes that easily.

Here's how you use Chrome to view any CSS and Javascript:


Simply right click on any webpage


Google Chrome's Inspect Element facility can inspect anything on a web site including your protected CSS and Javascript. Anyway, it is still a good practice to protect your CSS and Javascript if you think your hardwork should be protected and shared only someone asks you nicely for the codes. Here they are in the following.

The HTML:


.
.
.
<link rel="stylesheet" type="text/css" href="mycss.php">
<script language="JavaScript" src="myjs.php"></script>
.
.
.


The PHPs:


For mycss.php:

<?php

error_reporting(0); // make sure it is not reporting any warning that will ruin the output
if (preg_match('/xxx.xxx.xxx.xxx/',$_SERVER['REMOTE_ADDR'])) {

print<<<MYCSS
a {text-decoration:none;color:#777777;}
.
.
.
MYCSS;

}

?>



For myjs.php:

<?php

error_reporting(0); // make sure it is not reporting any warning that will ruin the output
if (preg_match('/xxx.xxx.xxx.xxx/',$_SERVER['REMOTE_ADDR'])) {

print<<<MYJS
var ie=0;
.
.
.
MYJS;

}


?>


You can also use $_SERVER['REMOTE_HOST'] if your shared server keeps on changing the IP address. You need to use your Domain Name instead of the xxx.xxx.xxx.xxx (IP Address) above.
Read More »

Saturday, August 4, 2012

Setting Permission for PHP/IIS Environment Especially When PHP Temporary File Facility Is Used

When PHP is run on IIS platform, users will run into the following problems:
  • (A) Files are not able to be written, created, or rewritten on the C:\inetpub\wwwroot\ directory.
  • (B) PHP uploaded temporary file will not be able to be written and this results in file upload problem. For example, $_FILES["FileID"]["tmp_name"] will not be able to be written.
  • (C) PHP tmpfile() command will fail.
In order to solve this problem, permission on the windows directory should be set correctly. For Linux, it's simple. For windows, it's a bit tricky. You can set the permission on IIS/Windows system using these steps:



  1. Go the folder you want the permission changed to IIS/PHP friendly settings. Right click on the folder and select Properties.



  2. Select the Security Tab.



  3. Click on Edit as indicated above. (There's no need to select Group or User at this point)



  4. Refer to the screen capture above, select Users(YourComputerName\Users). Then check the Full Control Allow and then click on Apply.
  5. Repeat Step D above but this time, select IIS_IUSRS(YourComputerName\IIS_IUSRS) (4). Then as in step D, check the Allow for Full Control (2) then Apply (3). Press OK and then OK again.
You now have set a loose permission for IIS/PHP to perform file operations on the folder you just fine-tuned above. My recommendation for the permission settings may be too loose for some. It's more meant for the PC or notebook in which you are only one who can access to the files. Now in order to solve (A)&(B) problem above, you just need to select the target folder to be granted full permission and repeat the steps above. In order to solve problem (C) above (tmpfile() not working), you need you loosen the C:\WINDOWS\Temp permission. As for problem (B), you also need to check out the phpinfo() by adjusting the PHP.ini so that you see where your temporary is:



You can use the search of 'tmp' and you'll find it in the first search result. Then set the php.ini according to your need. You can also set it to C:\WINDOWS\Temp but you need to grant the permission for PHP to store the temporary file here.
Read More »