...

Hi! I’m Starx

experienced Software Developer. And this is my blog
Start Reading About me
Blog Page
April 20, 2012

When to use CSS classes and when to not?

Question by Dude

When should you add classes to elements and style them using class selectors and when should you instead add a class/id on the parent and use the elements just as is?

Example:

<div class="warning-dialog">
    <h3>This is the title</h3>
    <p>This is the message</p>
</div>

.warning-dialog {}
.warning-dialog h3 {}
.warning-dialog p {}

vs.

<div class="warning-dialog">
    <h3 class="warning-title">This is the title</h3>
    <p class="warning-message">This is the message</p>
</div>

.warning-dialog {}
.warning-title {}
.warning-message {}

Or should you do

.warning-dialog .warning-dialog {}
.warning-dialog .warning-title {}
.warning-dialog .warning-message {}

Answer by Madara Uchiha

Ask yourself this simple question:

Do all <x> elements under this common ancestor mean the same thing?

If the answer to that is yes, then you don’t need a class name on those elements. Taking your code as an example, the following is sufficient:

<div class="warning-dialog">
    <h3>This is the title</h3>
    <p>This is the message</p>
</div>

Because inside of a .warning-dialog, all h3 elements (1) and all p elements (1) would mean the same, the title and the content of the dialog! Meaning, you don’t need to have any specific class names on them and they are easily accessible via .warning-dialog h3 or .warning-dialog p.

If however, the answer to above question is “No”, that’s a whole different story:

<div>Warning</div>
<div>Info</div>
<div>Error</div>

You can’t (easily) designate each div with a CSS, they don’t all mean the same thing, so you need to use class names to make it better!

<div class="warning-dialog">Warning</div>
<div class="info-dialog">Info</div>
<div class="error-dialog">Error</div>

Answer by Starx

The base concept is, that

  1. Unique Element to be styled, use id
  2. Collection of similar elements to be styled, use class.
Read more

jquery load to hide content

Question by Adam Strudwick

There is javascript on my webpage, but I need to hide it from my users (I don’t want them to be able to see it because it contains some answers to the game.)

So I tried using Jquery .load in order to hide the content (I load the content from an external js file with that call). But it failed to load. So I tried ajax and it failed too.

Maybe the problem comes from the fact that I’m trying to load a file located in my root directory, while the original page is located in “root/public_html/main/pages”:

<script type="text/javascript">
$(document).ready(function() {
    $.ajax({
        url : "../../../secret_code.js",
        dataType: "text",
        success : function (data) {
            $("#ajaxcontent").html(data);
        }
    });
});
</script>

1) Why can’t I load a file from the root directory with ajax or load method?
2) Is there another way around?

PS: I’m putting the file in the root directory so people can’t access it directly from their browsers…

Answer by Starx

Navigate to the URL, not the directory. Like

$.ajax({
        url : "http://domain.com/js/secret_code.js",

..
Read more

Using .htacces to run a .ics file as a .php file

Question by bhooks

I’m tyring to use .htaccess to run a .ics file as a .php file so that I can use the PHP code to create a calendar, that will then be recognized by devices like iPhones or programs like Microsoft Outlook since it’s file extension is .ics. I haven’t been able to get it to work so came here to see if someone could help.

.htacces code:
AddHandler application/x-httpd-php5 .ics

I’m not sure if it’s an error with .htaccess or with something in the PHP code, so if I need to post the PHP code please let me know. Thanks!

Answer by Starx

Like this

AddHandler application/x-httpd-php5s .ics
Read more

Radio button checked value on revisiting the page

Question by Lvcky Mercado

<div class="formfield ship">
    <input type="radio" class="ship1" name="1" >1</input>
    <input type="radio" class="ship2" name="1" >2</input>
    <input type="radio" class="ship3" name="1" >3</input>
    <div class="clear"><!-- FLOAT CLEAR --></div>
</div>

I have radio buttons one must have the default value.. when I pick one and submit and then I go back to the page the value must retain..

any idea how can I do it?

Answer by Mikey

this does what you want – although you need to modify your html slightly (I don’t think <input></input> is valid)

http://jsfiddle.net/malet/dvCpj/6/

$("input[type=radio]").each(function(){
    $(this).click(function(){     
        createCookie($(this).attr("name"), $($(this).siblings()[0]).html(),1);       
    });
});


$(document).ready(function(){
    $("input[type=radio]").each(function(){
        var selected = readCookie($(this).attr("name"));
        if(selected != null){
            if($($(this).siblings()[0]).html() == selected){
                $(this).attr("checked","checked");
            }                    
        }    
    });
});


function createCookie(name,value,days) {
    if (days) {
            var date = new Date();
            date.setTime(date.getTime()+(days*24*60*60*1000));
            var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
            var c = ca[i];
            while (c.charAt(0)==' ') c = c.substring(1,c.length);
            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name,"",-1);
}
​

Answer by Starx

Basically there is two way you can do this.

  1. Cookies
  2. Server Side Languages like php, asp

But either way, the main point is that, the default values of the radio elements have to be changed. You can use, serverside script to give the new default or use cookies to fetch the new defaults.

Read more

Jquery clone item

Question by Kobjir

I’m studying the interactive-image-vamp-up with jquery css3 and php script from http://tympanus.net/codrops/2010/03/22/interactive-image-vamp-up-with-jquery-css3-and-php/

My question, how to make items clone and get the clone position?

thanks in advance

Answer by Starx

var cloned = $("#itemtoclown").clone(); //clone an element
$("body").append(cloned); //Insert it into DOM

//Access the positions like with
var positions = cloned.position();
console.log(positions.top); //Access the top
Read more

Internal server error when uploading large files in php

Question by Kyokasuigetsu

I’m using codeIgniter and I’m getting internal server error when trying to upload large files (might be 1Mb and up)
The file upload script is working because it uploads to the folder in the server. But saving the file into the database it doesn’t work anymore. I’ve already tried changing the following but still doesn’t work:

php.ini

  • post_max_size = 50M
  • upload_max_filesize = 20M

my.ini

  • max_allowed_packet = 2048M
  • max_execution_time = 150

Where else do I have to check to ensure that the file upload to the database works.

Answer by Starx

Save the files do the directory, not on the database. You might want to save the path though in the databse.

If you want to know WHY, here are some previous post you should check.

  1. Storing Documents as Blobs in a Database – Any disadvantages?
  2. Storing Images in DB – Yea or Nay?
Read more

How to check if a folder exists using PHP

Question by edsonlp1

I have this code to check if a folder exists using PHP and it works correctly with xampp in windows, but when I try to move that page to a server, it doesn’t work.

Here is my code:

  function validate (&$valores, &$errores, $campo, $carpeta)
    {
    $valores [$campo] = $carpeta;
            if ((file_exists($carpeta) && is_dir($carpeta))==false)
            {
                    $errores[$campo] = true;                   
            }
            else
            {
                    $errores[$campo] = false;
            }
    }

There is an alternative to the code that I’m doing?

Thanks in advance

Answer by edsonlp1

Solution:
I added to PHP open_basedir folders and files I wanted to change, and then with Filezilla I’ve modified the read / write permission and fixed.

Answer by Starx

Your code should operate fine on both platforms, so its not this issue.

Usually, such problem due to the naming system. The linux based OS using strict case sensitive naming system.

So try to fix that.

Read more

Can I store some variable into the hover function?

Question by markzzz

Seems that I can’t store the linguettaCorrente variable into the hover handler :

$('.navigatore_blocco').hover(
    var linguettaCorrente=$(this).find('linguetta');

    function() {
        linguettaCorrente.animate( { height: 33 }, 600);
    },

    function() {
        linguettaCorrente.animate( { height: 23 }, 600);
    }
);

why? And how can I store it?

Answer by Vega

Declare the same var outside the hover and define it inside.

var linguettaCorrente = null;
$('.navigatore_blocco').hover(
    function() {
        linguettaCorrente = $(this).find('linguetta');
        linguettaCorrente.animate( { height: 33 }, 600);
    },
    function() {
        linguettaCorrente.animate( { height: 23 }, 600);
    }
);

.hover function takes 2 argument and those 2 are functions. It cannot be anything else.

Answer by Starx

Why dont you use .data() to store the needed data?

$('.navigatore_blocco').hover(
    var linguettaCorrente=$(this).find('linguetta');
    $(this).data('linguetaaCorrente', linguettaCorrente);
    ....
);
Read more
...

Please fill the form - I will response as fast as I can!